pythonprompt-toolkit

Adding a key binding on python prompt-toolkit-3.0.2 breaks the suggestion and history search


I am trying to add a different way to finish the multiline input. Should be simple, but I am getting an unexpected result: After adding the new binding the history and suggest features stop working.

I try to use the load_basic_bindings but it did not help.

If I comment on the key binding, the suggestion and historic work again.

from prompt_toolkit import PromptSession
from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
from prompt_toolkit.key_binding import KeyBindings

session = PromptSession()

# load empty binds
bindings = KeyBindings()

# reading from the basic binds did not work either
# bindings = load_basic_bindings()

# HERE IS THE PROBLEM
# After adding this the history and suggest stop working
# should just add a new way to exit
# I have tested with the eager True and False, with no changes
@bindings.add('#')
def _(event):
    event.app.exit(result=event.app.current_buffer.text)

while True:
    text = session.prompt(
        '> ',
        auto_suggest=AutoSuggestFromHistory(),
        key_bindings=bindings,     # if I comment the key bindings, the history and search work againg
        multiline=True,            # this bug just happens on multiline, if put this False the bug does not happens
        enable_history_search=True
    )
    print('You said: %s' % text)

Solution

  • If I use load_basic_bindings() I can accept command using Alt+Enter and it adds it to history.
    For # I had to add function which adds command to history

    session.history.append_string(event.app.current_buffer.text)
    

    Using arrows I can select from history. And it shows suggestion from history.

    Tested on Linux Mint 19.2 (based on Ubuntu 18.04), Python 3.7.6, Prompt Toolkit 3.0.2


    from prompt_toolkit import PromptSession
    from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
    from prompt_toolkit.key_binding import KeyBindings
    from prompt_toolkit.key_binding.bindings.basic import load_basic_bindings
    
    session = PromptSession()
    
    # load empty binds
    #bindings = KeyBindings()
    
    # reading from the basic binds did not work either
    bindings = load_basic_bindings()
    
    # HERE IS THE PROBLEM
    # After adding this the history and suggest stop working
    # should just add a new way to exit
    # I have tested with the eager True and False, with no changes
    @bindings.add('#')
    def _(event):
        session.history.append_string(event.app.current_buffer.text)
        event.app.exit(result=event.app.current_buffer.text)
    
    while True:
        text = session.prompt(
            '> ',
            auto_suggest=AutoSuggestFromHistory(),
            key_bindings=bindings,     # if I comment the key bindings, the history and search work againg
            multiline=True,            # this bug just happens on multiline, if put this False the bug does not happens
            enable_history_search=True
        )
        print('You said: %s' % text)