2

I use prompt_toolkit to ask the user for some input:

from prompt_toolkit import prompt
from prompt_toolkit.completion import WordCompleter

prompt('Input: ', completer=WordCompleter(['abc', 'def', 'xyz']))

Is it possible to show the suggestions automatically without any user intervention (no tab key)?

example

Jonas
  • 357
  • 2
  • 8
  • why to suggest anything when input is empty? Usually autocompleter need 3 chars to open but probably you would change it. But without any char it may try to suggest too many elements. I would rather display suggestion before `Input` and then user could write what it need. – furas Sep 11 '20 at 09:12
  • probably you will have to create own completer instead of `WordCompleter` - and you will have to create own `get_completions()` - see source code [Completer.get_completions()](https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/prompt_toolkit/completion/base.py#L169) and [WordCompleter.get_completions()](https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/prompt_toolkit/completion/word_completer.py#L52) – furas Sep 11 '20 at 09:24

1 Answers1

2

You can use pre_run hook to prompt it.

from prompt_toolkit.application.current import get_app

def prompt_autocomplete():
    app = get_app()
    b = app.current_buffer
    if b.complete_state:
        b.complete_next()
    else:
        b.start_completion(select_first=False)

prompt(pre_run=prompt_autocomplete)
Louie Lu
  • 31
  • 1
  • 7