Skip to content

Commit

Permalink
repl: improved Enter key behaviour when completing values
Browse files Browse the repository at this point in the history
When pressing enter in auto-complete text it will select a completion
instead of submiting the value without completion.

Signed-off-by: Pablo Palácios <ppalacios992@gmail.com>
  • Loading branch information
pablopalacios committed Aug 4, 2017
1 parent a53dbe7 commit affcc2c
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion uhu/repl/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

Expand All @@ -32,7 +33,7 @@
@manager.registry.add_binding(Keys.ControlD)
def ctrl_d(event):
"""Ctrl D quits appliaction returning 0 to sys."""
event.cli.run_in_terminal(sys.exit(0))
sys.exit(0)


@manager.registry.add_binding(Keys.ControlC)
Expand All @@ -45,6 +46,23 @@ def ctrl_c(_):
raise CancelPromptException('Cancelled operation.')


@manager.registry.add_binding(Keys.Enter, filter=HasCompletions())
def enter(event):
"""Enter selects a completion when has completions.
When there is no completion available, submit value.
"""
buffer = event.current_buffer
state = buffer.complete_state
if len(state.current_completions) == 1:
state = state.go_to_index(0)
buffer.apply_completion(state.current_completion)
elif state.current_completion is None:
buffer.complete_next()
else:
buffer.apply_completion(buffer.complete_state.current_completion)


def cancellable(func):
"""Decorator to cancell a current prompt."""
@wraps(func)
Expand Down

0 comments on commit affcc2c

Please sign in to comment.