Skip to content

Add tab-completion for paths#45

Open
ulken wants to merge 3 commits into
Evidlo:masterfrom
ulken:44-tab_completion
Open

Add tab-completion for paths#45
ulken wants to merge 3 commits into
Evidlo:masterfrom
ulken:44-tab_completion

Conversation

@ulken

@ulken ulken commented Nov 30, 2020

Copy link
Copy Markdown
Contributor

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

Oskar Löfgren added 2 commits November 23, 2020 23:21
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

ulken commented Nov 30, 2020

Copy link
Copy Markdown
Contributor Author

@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

ulken commented Nov 30, 2020

Copy link
Copy Markdown
Contributor Author

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

Evidlo commented Dec 2, 2020

Copy link
Copy Markdown
Owner

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