Skip to content

Commit

Permalink
Merge pull request #18 from McKean/master
Browse files Browse the repository at this point in the history
included additional copy choices
  • Loading branch information
Kwpolska committed Jun 10, 2017
2 parents b496bf8 + 4159514 commit 4aa30c8
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions upass/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,13 @@ def display_selected(self, originator):
"""Display the currently selected password."""
path, pathg = self.get_selected_password(originator)
if pathg in self.passwords:
self.call_pass(originator, (path, False))
self.call_pass(originator, (path, False, None))

def copy_selected(self, originator):
"""Copy the currently selected password."""
path, pathg = self.get_selected_password(originator)
if pathg in self.passwords:
self.call_pass(originator, (path, True))
self.call_pass(originator, (path, True, False))

def search(self, originator):
"""Display the search box."""
Expand Down Expand Up @@ -379,13 +379,16 @@ def pass_load(self, originator, path):
prevdir = os.path.dirname(path) or '.'
self.box.body.append(BackButton('BACK', self.dir_load, prevdir, self))
self.box.body.append(ActionButton('DISPLAY', self.call_pass,
(path, False)))
self.box.body.append(ActionButton('COPY', self.call_pass,
(path, True)))
(path, False, None)))
self.box.body.append(ActionButton('COPY FIRST LINE', self.call_pass,
(path, True, False)))
self.box.body.append(ActionButton('COPY EVERYTHING', self.call_pass,
(path, True, True)))
self.box.body.append(urwid.Text("More copy options are available after displaying the password."))

def call_pass(self, originator, args):
"""Call pass to get a password."""
self.current, copy = args
self.current, copy, copy_key = args
self.set_header(self.current)
pargs = ['pass', self.current]
copymsg = ' and copying output afterwards' if copy else ''
Expand All @@ -400,20 +403,46 @@ def call_pass(self, originator, args):
stdout, stderr = p.communicate()
if p.returncode == 0:
self._clear_box()
try:
text = stdout.decode('utf-8')
except AttributeError:
text = stdout

copiable_entries = {}
copiable_keys = []

for index, line in enumerate(text.split('\n')):
entry = line.split(': ', 1)
if len(entry) > 1:
copiable_entries[entry[0]] = entry[1]
copiable_keys.append(entry[0])

if copy:
try:
copytarget = stdout.decode('utf-8')
except AttributeError:
copytarget = stdout
copytarget = copytarget.split('\n', 1)[0]
if copy_key is False: # False: copy first line
copytarget = text.split('\n', 1)[0]
copy_key = 'first line'
elif copy_key is True: # True: copy everything
copytarget = text
copy_key = 'everything'
else: # string: copy whatever is passed
copytarget = copiable_entries[copy_key]

pyperclip.copy(copytarget)
self.box.body.append(
urwid.AttrMap(
urwid.Text('Copied to clipboard.'), 'highlight'))
urwid.Text('Copied {0} to clipboard.'.format(copy_key)),
'highlight'))
else:
self.box.body.append(urwid.Text(stdout.strip()))
self.box.body.append(ActionButton('COPY', self.call_pass,
(self.current, True)))
self.box.body.append(urwid.Text(text.strip()))

self.box.body.append(ActionButton('COPY FIRST LINE', self.call_pass,
(self.current, True, False)))
self.box.body.append(ActionButton('COPY EVERYTHING', self.call_pass,
(self.current, True, True)))

for k in copiable_keys:
self.box.body.append(ActionButton('COPY {0}'.format(k), self.call_pass,
(self.current, True, k)))
else:
self.box.body.append(urwid.Text(('error', 'ERROR')))
self.box.body.append(
Expand Down

0 comments on commit 4aa30c8

Please sign in to comment.