簡體   English   中英

在 python prompt-toolkit-3.0.2 上添加鍵綁定會破壞建議和歷史搜索

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

我正在嘗試添加一種不同的方式來完成多行輸入。 應該很簡單,但我得到了一個意想不到的結果:添加新的綁定后,歷史和建議功能停止工作。

我嘗試使用 load_basic_bindings 但它沒有幫助。

如果我再次評論鍵綁定,建議和歷史性工作。

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)

如果我使用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM