Skip to content

Instantly share code, notes, and snippets.

@jonathanslenders
Created October 9, 2015 17:58
Show Gist options
  • Select an option

  • Save jonathanslenders/e4f02e44c4ea7aabfc60 to your computer and use it in GitHub Desktop.

Select an option

Save jonathanslenders/e4f02e44c4ea7aabfc60 to your computer and use it in GitHub Desktop.
Prompt with autocompletion where the enter key accepts the input.
#!/usr/bin/env python
"""
Example of a prompt with autocompletion, where pressing the Enter key accepts
the completion.
"""
from __future__ import unicode_literals
from prompt_toolkit import prompt
from prompt_toolkit.contrib.completers import WordCompleter
from prompt_toolkit.filters import HasCompletions
from prompt_toolkit.key_binding.manager import KeyBindingManager
from prompt_toolkit.keys import Keys
animal_completer = WordCompleter([
'alligator', 'ant', 'ape', 'bat', 'bear', 'beaver', 'bee', 'bison',
'butterfly', 'cat', 'chicken', 'crocodile', 'dinosaur', 'dog', 'dolphine',
'dove', 'duck', 'eagle', 'elephant', 'fish', 'goat', 'gorilla', 'kangoroo',
'leopard', 'lion', 'mouse', 'rabbit', 'rat', 'snake', 'spider', 'turkey',
'turtle',
], ignore_case=True)
def main():
key_bindings_manager = KeyBindingManager.for_prompt()
@key_bindings_manager.registry.add_binding(Keys.ControlJ, filter=HasCompletions())
def _(event):
" Accept the current completion when enter has been pressed. "
event.current_buffer.complete_state = None
text = prompt('Give some animals: ',
completer=animal_completer,
key_bindings_registry=key_bindings_manager.registry)
print('You said: %s' % text)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment