Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tab-completion for paths #45

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ulken
Copy link
Contributor

@ulken ulken commented Nov 30, 2020

Supported commands: all commands accepting a path argument (ls, rm, mv, edit, show).

Caveats
Completions must match exact structure of paths. As a result, it doesn't:

  • support db-prefixes
  • support case-insensitivty
  • support regexes

Additional changes

  • Add login command (for explicit caching, when zenity is missing)
  • Remove extra '' around form label in zenity
  • Remove period from error message
  • Fix incorrect name being passed to default section prompt
  • Handle more cases in decompose_path + return tuple
  • Fix info_parser comment

Closes #44

Supported commands: all commands accepting a `path` argument (ls, rm, mv, edit, show).

Caveats
Completions must match exact structure of paths. As a result, it doesn't:
- support db-prefixes
- support case-insensitivty
- support regexes

Additional changes
- Add `login` command (for explicit caching, when `zenity` is missing)
- Remove extra '' around form label in `zenity`
- Remove period from error message
- Fix incorrect name being passed to default section prompt
- Handle more cases in `decompose_path` + return tuple
- Fix `info_parser` comment
@ulken ulken changed the title 44 tab completion Add tab-completion for paths Nov 30, 2020
@ulken ulken marked this pull request as ready for review November 30, 2020 23:27
@ulken
Copy link
Contributor Author

ulken commented Nov 30, 2020

@Evidlo I've yet to update the documentation with how to set it up, but please have a look/try it out. I'll update the docs when we've settled on something.

@ulken
Copy link
Contributor Author

ulken commented Nov 30, 2020

In the meantime, if you're not familiar with argcomplete, have a look at the docs how to set things up: argcomplete - Bash tab completion for argparse

@Evidlo
Copy link
Owner

Evidlo commented Dec 2, 2020

I did some experimentation with prompting for the password during the completion to eliminate the need for login().

ph_complete

#!/bin/env python3

from getpass import getpass
import argcomplete, argparse

def path_completer(prefix, parsed_args, **kwargs):
    password = getpass('\nInput Password:')

    if password == 'password':
        return ['helloworld', 'foobar']

parser = argparse.ArgumentParser()

patharg = parser.add_argument('path', metavar='PATH', type=str, help='item path')
patharg.completer = path_completer

argcomplete.autocomplete(parser)
args = parser.parse_args()
print(f'selected item: {args.path}')

And for testing:

chmod +x ph_complete
eval "$(register-python-argcomplete ./ph_complete)"

It's not fully working, as you have to hit enter twice for some reason after entering a password and the prompt line isn't redrawn when the completion is finished. Also, argcomplete hijacks stdout so I had to abuse argcomplete.warn() for the password prompt. We could probably do some digging around and make a patched version of the argcomplete class that doesn't do this.

Ideally, tab completion when entering a password would look something like this:

$ ./ph_complete hello[TAB]
Input password:
$ ./ph_complete helloworld

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Shell completions
2 participants