line_editor.py
Headless line editor with history, auto-suggest, and grapheme-aware editing.
This module provides LineEditor for single-line input with readline-style
editing and LineHistory for command recall.
- class DisplayState(text: str = '', cursor: int = 0, suggestion: str = '', overflow_left: bool = False, overflow_right: bool = False, text_sgr: str = '', suggestion_sgr: str = '', bg_sgr: str = '', ellipsis_sgr: str = '')[source]
Current visual state of the editor for rendering.
- class LineEditResult(line: str | None = None, eof: bool = False, interrupt: bool = False, changed: bool = False, bell: str = '')[source]
Result of processing a keystroke.
- class LineHistory(max_entries: int = 5000)[source]
In-memory command history with navigation and prefix search.
- Parameters:
max_entries¶ – Maximum number of history entries retained.
Initialize history with given maximum capacity.
- search_prefix(prefix: str) str | None[source]
Return the most recent entry starting with prefix, or
None.
Begin history navigation, saving current_line.
Navigate to the previous (older) history entry.
Navigate to the next (newer) history entry.
- class LineEditor(history: LineHistory | None = None, password: bool = False, password_char: str = '✻', max_width: int = 0, ellipsis: str = '…', limit: int = 2048, limit_bell: str = '\x07', scroll_jump: float = 0.5, text_sgr: str = '\x1b[38;2;230;225;220m', suggestion_sgr: str = '\x1b[30m', bg_sgr: str = '', ellipsis_sgr: str = '', keymap: Dict[str, Callable[[...], LineEditResult] | None] | None = None)[source]
Headless single-line editor with grapheme-aware cursor movement.
Feed keystrokes via
feed_key(), read display state viadisplay. Accepts blessedKeystrokeobjects directly (dispatching on.name), or plain strings for testing.Custom keymap handlers must be callables accepting a single
LineEditorargument and returning aLineEditResult:def my_handler(editor: LineEditor) -> LineEditResult: ...
Initialize editor with optional history, display, and keymap settings.
- property history: LineHistory
Return the attached
LineHistoryinstance.
- property display: DisplayState
Return the current
DisplayStatefor rendering.
- render(term: Terminal, row: int, width: int, col: int = 0) str[source]
Build escape sequences to render the current display state.
- render_insert(term: Terminal, row: int, grapheme: str) str | None[source]
Fast-path render for a single grapheme inserted at end of buffer.
- render_backspace(term: Terminal, row: int) str | None[source]
Fast-path render after a backspace at end of buffer.
- feed_key(key: 'Keystroke' | str) LineEditResult[source]
Process one keystroke and return a
LineEditResult.
- insert_text(text: str) LineEditResult[source]
Insert text at cursor position (for bracketed paste).