本文整理汇总了Python中prompt_toolkit.key_binding.KeyBindings方法的典型用法代码示例。如果您正苦于以下问题:Python key_binding.KeyBindings方法的具体用法?Python key_binding.KeyBindings怎么用?Python key_binding.KeyBindings使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类prompt_toolkit.key_binding
的用法示例。
在下文中一共展示了key_binding.KeyBindings方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_bindings
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def get_bindings(settings):
bindings = KeyBindings()
@bindings.add(Keys.F2)
def _(event):
settings.set_next_cluster()
@bindings.add(Keys.F3)
def _(event):
settings.set_enable_fuzzy_search(not settings.enable_fuzzy_search)
@bindings.add(Keys.F9)
def _(event):
settings.set_enable_help(not settings.enable_help)
@bindings.add(Keys.F10)
def _(event):
current.get_app().exit(exception=EOFError)
return bindings
示例2: __init__
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def __init__(self, pymux):
self.pymux = pymux
def get_search_state():
" Return the currently active SearchState. (The one for the focused pane.) "
return pymux.arrangement.get_active_pane().search_state
self.custom_key_bindings = KeyBindings()
self.key_bindings = merge_key_bindings([
self._load_builtins(),
self.custom_key_bindings,
])
self._prefix = ('c-b', )
self._prefix_binding = None
# Load initial bindings.
self._load_prefix_binding()
# Custom user configured key bindings.
# { (needs_prefix, key) -> (command, handler) }
self.custom_bindings = {}
示例3: run
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def run():
# Create a `KeyBindings` that contains the default key bindings.
bindings = KeyBindings()
# Add an additional key binding for toggling this flag.
@bindings.add("f4")
def _(event):
" Toggle between Emacs and Vi mode. "
if event.app.editing_mode == EditingMode.VI:
event.app.editing_mode = EditingMode.EMACS
else:
event.app.editing_mode = EditingMode.VI
def bottom_toolbar():
" Display the current input mode. "
if get_app().editing_mode == EditingMode.VI:
return " [F4] Vi "
else:
return " [F4] Emacs "
prompt("> ", key_bindings=bindings, bottom_toolbar=bottom_toolbar)
示例4: main
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def main():
# We start with a `KeyBindings` for our extra key bindings.
bindings = KeyBindings()
# We add a custom key binding to space.
@bindings.add(" ")
def _(event):
"""
When space is pressed, we check the word before the cursor, and
autocorrect that.
"""
b = event.app.current_buffer
w = b.document.get_word_before_cursor()
if w is not None:
if w in corrections:
b.delete_before_cursor(count=len(w))
b.insert_text(corrections[w])
b.insert_text(" ")
# Read input.
text = prompt("Say something: ", key_bindings=bindings)
print("You said: %s" % text)
示例5: create_key_bindings
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def create_key_bindings() -> KeyBindings:
"""
Key bindings handled by the progress bar.
(The main thread is not supposed to handle any key bindings.)
"""
kb = KeyBindings()
@kb.add("c-l")
def _clear(event: E) -> None:
event.app.renderer.clear()
@kb.add("c-c")
def _interrupt(event: E) -> None:
# Send KeyboardInterrupt to the main thread.
os.kill(os.getpid(), signal.SIGINT)
return kb
示例6: create_dummy_layout
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def create_dummy_layout() -> Layout:
"""
Create a dummy layout for use in an 'Application' that doesn't have a
layout specified. When ENTER is pressed, the application quits.
"""
kb = KeyBindings()
@kb.add("enter")
def enter(event: E) -> None:
event.app.exit()
control = FormattedTextControl(
HTML("No layout specified. Press <reverse>ENTER</reverse> to quit."),
key_bindings=kb,
)
window = Window(content=control, height=D(min=1))
return Layout(container=window, focused_element=window)
示例7: __init__
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def __init__(self,
input_handler: Callable,
bindings: KeyBindings,
completer: Completer):
self.search_field = create_search_field()
self.input_field = create_input_field(completer=completer)
self.output_field = create_output_field()
self.log_field = create_log_field(self.search_field)
self.layout = generate_layout(self.input_field, self.output_field, self.log_field, self.search_field)
# add self.to_stop_config to know if cancel is triggered
self.to_stop_config: bool = False
self.bindings = bindings
self.input_handler = input_handler
self.input_field.accept_handler = self.accept
self.app = Application(layout=self.layout, full_screen=True, key_bindings=self.bindings, style=load_style(),
mouse_support=True, clipboard=PyperclipClipboard())
# settings
self.prompt_text = ">>> "
self.pending_input = None
self.input_event = None
self.hide_input = False
示例8: load_confirm_exit_bindings
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def load_confirm_exit_bindings(python_input):
"""
Handle yes/no key presses when the exit confirmation is shown.
"""
bindings = KeyBindings()
handle = bindings.add
confirmation_visible = Condition(lambda: python_input.show_exit_confirmation)
@handle("y", filter=confirmation_visible)
@handle("Y", filter=confirmation_visible)
@handle("enter", filter=confirmation_visible)
@handle("c-d", filter=confirmation_visible)
def _(event):
"""
Really quit.
"""
event.app.exit(exception=EOFError, style="class:exiting")
@handle(Keys.Any, filter=confirmation_visible)
def _(event):
"""
Cancel exit.
"""
python_input.show_exit_confirmation = False
return bindings
示例9: _get_bindings
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def _get_bindings(self):
bindings = KeyBindings()
bindings.add("c-c")(lambda event: self._interrupt_handler(event))
return bindings
示例10: test_bindings
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def test_bindings(self):
kb = KeyBindings()
bind_keys(Buffer(), kb)
self.assertTrue('on_backspace' in handlers_for_key(kb, Keys.Backspace))
self.assertTrue('on_tab' in handlers_for_key(kb, Keys.Tab))
示例11: main
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def main():
bottom_toolbar = HTML(
' <b>[f]</b> Print "f" <b>[q]</b> Abort <b>[x]</b> Send Control-C.'
)
# Create custom key bindings first.
kb = KeyBindings()
cancel = [False]
@kb.add("f")
def _(event):
print("You pressed `f`.")
@kb.add("q")
def _(event):
" Quit by setting cancel flag. "
cancel[0] = True
@kb.add("x")
def _(event):
" Quit by sending SIGINT to the main thread. "
os.kill(os.getpid(), signal.SIGINT)
# Use `patch_stdout`, to make sure that prints go above the
# application.
with patch_stdout():
with ProgressBar(key_bindings=kb, bottom_toolbar=bottom_toolbar) as pb:
for i in pb(range(800)):
time.sleep(0.01)
if cancel[0]:
break
示例12: main
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def main():
hidden = [True] # Nonlocal
bindings = KeyBindings()
@bindings.add("c-t")
def _(event):
" When ControlT has been pressed, toggle visibility. "
hidden[0] = not hidden[0]
print("Type Control-T to toggle password visible.")
password = prompt(
"Password: ", is_password=Condition(lambda: hidden[0]), key_bindings=bindings
)
print("You said: %s" % password)
开发者ID:prompt-toolkit,项目名称:python-prompt-toolkit,代码行数:16,代码来源:get-password-with-toggle-display-shortcut.py
示例13: __init__
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def __init__(
self,
title: AnyFormattedText = None,
formatters: Optional[Sequence[Formatter]] = None,
bottom_toolbar: AnyFormattedText = None,
style: Optional[BaseStyle] = None,
key_bindings: Optional[KeyBindings] = None,
file: Optional[TextIO] = None,
color_depth: Optional[ColorDepth] = None,
output: Optional[Output] = None,
input: Optional[Input] = None,
) -> None:
self.title = title
self.formatters = formatters or create_default_formatters()
self.bottom_toolbar = bottom_toolbar
self.counters: List[ProgressBarCounter[object]] = []
self.style = style
self.key_bindings = key_bindings
# Note that we use __stderr__ as default error output, because that
# works best with `patch_stdout`.
self.color_depth = color_depth
self.output = output or get_app_session().output
self.input = input or get_app_session().input
self._thread: Optional[threading.Thread] = None
self._loop = get_event_loop()
self._app_loop = new_event_loop()
self._previous_winch_handler = None
self._has_sigwinch = False
if TYPE_CHECKING:
# Infer type from getsignal result, as defined in typeshed. Too
# complex to repeat here.
self._previous_winch_handler = signal.getsignal(_SIGWINCH)
示例14: get_key_bindings
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def get_key_bindings(self) -> KeyBindings:
return self._key_bindings
示例15: build_propmpt
# 需要导入模块: from prompt_toolkit import key_binding [as 别名]
# 或者: from prompt_toolkit.key_binding import KeyBindings [as 别名]
def build_propmpt(self) -> pt_shortcuts.PromptSession:
history = pt_history.FileHistory(
os.path.expanduser('~/.edgedbhistory'))
bindings = pt_key_binding.KeyBindings()
handle = bindings.add
@handle('f3') # type: ignore
def _mode_toggle(event: Any) -> None:
self.context.toggle_query_mode()
@handle('f4') # type: ignore
def _implicit_toggle(event: Any) -> None:
self.context.toggle_implicit()
@handle('f5') # type: ignore
def _introspect_toggle(event: Any) -> None:
self.context.toggle_introspect_types()
if self.context.introspect_types:
self.ensure_connection()
self.introspect_db(self.connection)
else:
self.context.typenames = None
@handle('tab') # type: ignore
def _tab(event: Any) -> None:
b = prompt.app.current_buffer
before_cursor = b.document.current_line_before_cursor
if b.text and (not before_cursor or before_cursor.isspace()):
b.insert_text(' ')
prompt = pt_shortcuts.PromptSession(
lexer=pt_lexers.PygmentsLexer(eql_pygments.EdgeQLLexer),
include_default_pygments_style=False,
completer=pt_complete.DummyCompleter(),
reserve_space_for_menu=6,
message=self.get_prompt_tokens,
prompt_continuation=self.get_continuation_tokens,
bottom_toolbar=self.get_toolbar_tokens,
multiline=is_multiline,
history=history,
complete_while_typing=pt_filters.Always(),
key_bindings=bindings,
style=self.style,
editing_mode=pt_enums.EditingMode.VI,
search_ignore_case=True,
)
return prompt