Files
wehub-resource-sync 4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:10:45 +08:00

105 lines
3.5 KiB
Python

"""Prompt-toolkit key bindings for the REPL prompt."""
from __future__ import annotations
from typing import Protocol
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.completion import CompleteEvent
from prompt_toolkit.filters import has_completions
from prompt_toolkit.key_binding import KeyBindings, merge_key_bindings
from prompt_toolkit.key_binding.key_processor import KeyPressEvent
class _DispatchCancelState(Protocol):
def is_dispatch_running(self) -> bool:
raise NotImplementedError
def cancel_current_dispatch(self) -> None:
raise NotImplementedError
# Keystroke escape (xterm modifyOtherKeys for Shift+Enter), not a colour code.
_SHIFT_ENTER_SEQUENCE = "\x1b[27;2;13~"
def _tab_expand_or_menu(buffer: Buffer) -> None:
"""Apply the current completion or open the menu when several choices exist."""
if buffer.complete_state:
state = buffer.complete_state
completion = state.current_completion
if completion is None and state.completions:
completion = state.completions[0]
if completion is not None:
buffer.apply_completion(completion)
return
if buffer.completer is None:
return
completions = list(
buffer.completer.get_completions(
buffer.document,
CompleteEvent(completion_requested=True),
)
)
if len(completions) == 1:
buffer.apply_completion(completions[0])
else:
buffer.start_completion(select_first=True)
def _build_prompt_key_bindings() -> KeyBindings:
bindings = KeyBindings()
@bindings.add("c-m")
def _accept_turn(event: object) -> None:
if event.data == _SHIFT_ENTER_SEQUENCE: # type: ignore[attr-defined]
event.current_buffer.newline(copy_margin=False) # type: ignore[attr-defined]
return
event.current_buffer.validate_and_handle() # type: ignore[attr-defined]
@bindings.add("tab")
def _tab_complete(event: object) -> None:
_tab_expand_or_menu(event.current_buffer) # type: ignore[attr-defined]
@bindings.add("s-tab")
def _shift_tab_complete(event: object) -> None:
buff = event.current_buffer # type: ignore[attr-defined]
if buff.complete_state:
buff.complete_previous()
else:
buff.start_completion(select_first=False)
@bindings.add("down", filter=has_completions)
def _next_completion(event: object) -> None:
event.current_buffer.complete_next() # type: ignore[attr-defined]
@bindings.add("up", filter=has_completions)
def _previous_completion(event: object) -> None:
event.current_buffer.complete_previous() # type: ignore[attr-defined]
return bindings
def build_cancel_key_bindings(state: _DispatchCancelState) -> KeyBindings:
kb = KeyBindings()
@kb.add("escape", eager=True)
def _on_escape(event: KeyPressEvent) -> None:
if state.is_dispatch_running():
state.cancel_current_dispatch()
return
if event.current_buffer.text:
event.current_buffer.reset()
@kb.add("c-l")
def _on_ctrl_l(event: KeyPressEvent) -> None:
event.app.renderer.clear()
return kb
def install_session_key_bindings(pt_session: object, extra_kb: KeyBindings) -> None:
existing = getattr(pt_session, "key_bindings", None)
merged = merge_key_bindings([existing, extra_kb]) if existing is not None else extra_kb
pt_session.key_bindings = merged # type: ignore[attr-defined]