Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/palette.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-05-24 - Handle Terminal Raw Mode Keyboard Traps
**Learning:** When creating terminal UIs with raw mode, `\x03` (Ctrl-C) must be explicitly trapped and raised as `KeyboardInterrupt`, otherwise it creates a keyboard trap where standard interrupt keys are ignored. It's also important to explicitly format choices in `rich` Prompts while escaping markup to avoid fallback issues.
**Action:** Always add explicit keyboard interrupt handling for `\x03` and clear exit instructions ('Esc/Ctrl-C to cancel') in terminal UI raw modes.
9 changes: 7 additions & 2 deletions libs/terminal_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def _read_key(self):
return 'enter'
if key == '\x1b':
return 'escape'
if key == '\x03':
raise KeyboardInterrupt
return key

import termios
Expand All @@ -37,6 +39,8 @@ def _read_key(self):
try:
tty.setraw(fd)
key = sys.stdin.read(1)
if key == '\x03':
raise KeyboardInterrupt
if key == '\x1b':
next_chars = sys.stdin.read(2)
mapping = {'[A': 'up', '[B': 'down', '[D': 'left', '[C': 'right'}
Expand Down Expand Up @@ -67,7 +71,8 @@ def _render_selector(self, title, options, selected_index, help_text, default):
style = 'bold green' if index == selected_index else ''
table.add_row(marker, label, style=style)

footer = help_text or 'Use Up/Down arrows and Enter to select.'
base_text = help_text or 'Use Up/Down arrows and Enter to select.'
footer = f"{base_text} Esc/Ctrl-C to cancel."
self.console.print(Panel.fit(footer, title='Interpreter TUI', border_style='green'))
self.console.print(f"[bold cyan]{title}[/bold cyan]")
self.console.print(table)
Expand All @@ -76,7 +81,7 @@ def _render_selector(self, title, options, selected_index, help_text, default):
def _select_option(self, title, options, default, help_text=None):
if not sys.stdin.isatty():
default_choice = default if default in options else options[0]
answer = Prompt.ask(f"{title}", default=default_choice).strip()
answer = Prompt.ask(f"{title} \\[{'|'.join(options)}]", default=default_choice).strip()
if answer in options:
return answer
for option in options:
Expand Down
Loading