Добавление привязки ключа к python prompt-toolkit-3.0.2 нарушает поиск и историю поиска - PullRequest
0 голосов
/ 10 января 2020

Я пытаюсь добавить другой способ к окончанию sh многострочного ввода. Должно быть просто, но я получаю неожиданный результат: после добавления новой привязки перестают работать история и предлагаемые функции.

Я пытаюсь использовать load_basic_bindings, но это не помогло.

Если Я прокомментирую привязку ключа, предложение и историю c работы снова.

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)

1 Ответ

1 голос
/ 10 января 2020

Если я использую load_basic_bindings(), я могу принять команду, используя Alt+Enter, и она добавляет ее в историю.
Для # Мне пришлось добавить функцию, которая добавляет команду в историю

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

Используя стрелки, я могу выбрать из истории. И это показывает предложение из истории.

Протестировано на Linux Mint 19.2 (на основе 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)
...