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-07-05 - Explicit TUI Cancellation and Prompt Options
**Learning:** When using raw terminal input, standard interrupt bytes like `\x03` (Ctrl-C) are intercepted and must be manually handled by explicitly raising `KeyboardInterrupt`. Furthermore, `Esc` (`\x1b`) should not be advertised or mapped as a cancel action because reading it in raw mode followed by a blocking read for ANSI sequences hangs the application. When wrapping `rich.prompt.Prompt.ask()`, available choices must be explicitly formatted into the prompt string with escaped brackets `\[]` to ensure they display without breaking custom case-insensitivity logic or triggering markup warnings.
**Action:** Explicitly catch `\x03` to raise `KeyboardInterrupt`, explicitly append `(Ctrl-C to cancel)` to TUI footers, and manually format prompt choices with escaped brackets in `Prompt.ask()` strings.
8 changes: 7 additions & 1 deletion libs/terminal_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def _read_key(self):
if os.name == 'nt':
import msvcrt
key = msvcrt.getwch()
if key == '\x03':
raise KeyboardInterrupt('Selection cancelled by user.')
if key in ('\x00', '\xe0'):
extended = msvcrt.getwch()
mapping = {'H': 'up', 'P': 'down', 'K': 'left', 'M': 'right'}
Expand All @@ -37,6 +39,8 @@ def _read_key(self):
try:
tty.setraw(fd)
key = sys.stdin.read(1)
if key == '\x03':
raise KeyboardInterrupt('Selection cancelled by user.')
if key == '\x1b':
next_chars = sys.stdin.read(2)
mapping = {'[A': 'up', '[B': 'down', '[D': 'left', '[C': 'right'}
Expand Down Expand Up @@ -68,6 +72,8 @@ def _render_selector(self, title, options, selected_index, help_text, default):
table.add_row(marker, label, style=style)

footer = help_text or 'Use Up/Down arrows and Enter to select.'
if 'Ctrl-C' not in footer:
footer += ' (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 +82,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