prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.21k stars 715 forks source link

Always display first completion #891

Open ondrejj opened 5 years ago

ondrejj commented 5 years ago

With prompt_toolkit 1 it was possible to return multiple completions and always first completion was selected. After update to PT2, this isn't possible. When I try to complete with TAB key, only partial completion is used (only "msg" string) and second TAB should be pressed to full completion. Can I somehow emulate PT1 behaviour? Here is my example code:

from prompt_toolkit import prompt
from prompt_toolkit.completion import Completer, Completion

class MyCustomCompleter(Completer):
    def get_completions(self, document, complete_event):
        yield Completion('msg test', start_position=0)
        yield Completion('msg help', start_position=0)

text1 = prompt(">> ",
  completer=MyCustomCompleter(),
  complete_while_typing=True,
  reserve_space_for_menu=0
)
print("RESULT:", repr(text1))

I need after first TAB to see "msg test" in prompt.

ondrejj commented 5 years ago

This code works, but it's a bit very hard hack:

import prompt_toolkit.buffer
orig_start_completion = prompt_toolkit.buffer.Buffer.start_completion
def buffer_start_completion(self, select_first=False,
                         select_last=False, insert_common_part=False,
                         complete_event=None):
      return orig_start_completion(self, True, False, False, complete_event)
prompt_toolkit.buffer.Buffer.start_completion = buffer_start_completion

Any change to make it possible to do this nicer?