Jakub Wilk avatar Jakub Wilk committed c14537d

Add option to pick only some characters from a password.

Comments (0)

Files changed (1)

         description=textwrap.dedent(do_show.__doc__),
     )
     c_show.add_argument('--omit', action='store_true', help='omit passwords')
+    c_show.add_argument('--pick', metavar='N[,N...]', help='pick only some characters')
     c_show.add_argument('-x', '--x-clipboard', action='store_true', help='copy the password to clipboard')
     c_show.add_argument('keyword', metavar='KEYWORD')
     c_show.set_defaults(action=do_show)
     options = parser.parse_args()
     return options.action(options)
 
+def parse_pick(s):
+    if s is None:
+        return str
+    lst = []
+    for item in s.split(','):
+        if '-' in item:
+            l, r = (int(x, 10) for x in item.split('-', 1))
+        else:
+            l = r = int(item, 10)
+        if l < 1:
+            raise IndexError('index should be a positive integer: {idx}'.format(idx=l))
+        if l > r:
+            raise IndexError('empty range: {l} > {r}'.format(l=l, r=r))
+        lst += range(l - 1, r)
+    return lambda p: ''.join(p[i] for i in lst)
+
 def do_show(options):
     '''
     Read password file from standard input and output (some) passwords.
          password: cUUZ4oUXFv
 
     '''
+    pick_filter = parse_pick(options.pick)
     gpg = ipc.Popen(['gpg', '-q', '-d'], stdout=ipc.PIPE)
     lines = yaml.load(gpg.stdout)
     for item in lines or []:
         if not options.keyword in site:
             continue
         password = str(item['password'])
+        password = pick_filter(password)
         if '#' in password:
             continue
         user = str(item.get('user', '<none>'))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.