Fix filter lambda causing TUI to fail

Use Condition wrapper with try/except to handle case where
self.app doesn't exist yet during key binding setup.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
dullfig 2026-01-11 15:12:44 -08:00
parent 10daf0ce70
commit ed02281513

View file

@ -201,8 +201,18 @@ class TUIConsole:
"""Switch focus between output and input.""" """Switch focus between output and input."""
event.app.layout.focus_previous() event.app.layout.focus_previous()
# Helper to check if output is focused
def output_focused():
try:
return self.app and self.app.layout.has_focus(self.output.buffer)
except Exception:
return False
from prompt_toolkit.filters import Condition
output_focus_filter = Condition(output_focused)
# Arrow keys for scrolling output (move cursor to scroll view) # Arrow keys for scrolling output (move cursor to scroll view)
@kb.add("up", filter=lambda: self.app.layout.has_focus(self.output.buffer)) @kb.add("up", filter=output_focus_filter)
def handle_output_up(event): def handle_output_up(event):
"""Scroll output up.""" """Scroll output up."""
buf = self.output.buffer buf = self.output.buffer
@ -212,7 +222,7 @@ class TUIConsole:
new_pos = doc.translate_row_col_to_index(doc.cursor_position_row - 1, 0) new_pos = doc.translate_row_col_to_index(doc.cursor_position_row - 1, 0)
buf.cursor_position = new_pos buf.cursor_position = new_pos
@kb.add("down", filter=lambda: self.app.layout.has_focus(self.output.buffer)) @kb.add("down", filter=output_focus_filter)
def handle_output_down(event): def handle_output_down(event):
"""Scroll output down.""" """Scroll output down."""
buf = self.output.buffer buf = self.output.buffer
@ -221,7 +231,7 @@ class TUIConsole:
new_pos = doc.translate_row_col_to_index(doc.cursor_position_row + 1, 0) new_pos = doc.translate_row_col_to_index(doc.cursor_position_row + 1, 0)
buf.cursor_position = new_pos buf.cursor_position = new_pos
@kb.add("pageup", filter=lambda: self.app.layout.has_focus(self.output.buffer)) @kb.add("pageup", filter=output_focus_filter)
def handle_output_pageup(event): def handle_output_pageup(event):
"""Scroll output up a page.""" """Scroll output up a page."""
buf = self.output.buffer buf = self.output.buffer
@ -230,7 +240,7 @@ class TUIConsole:
new_pos = doc.translate_row_col_to_index(new_row, 0) new_pos = doc.translate_row_col_to_index(new_row, 0)
buf.cursor_position = new_pos buf.cursor_position = new_pos
@kb.add("pagedown", filter=lambda: self.app.layout.has_focus(self.output.buffer)) @kb.add("pagedown", filter=output_focus_filter)
def handle_output_pagedown(event): def handle_output_pagedown(event):
"""Scroll output down a page.""" """Scroll output down a page."""
buf = self.output.buffer buf = self.output.buffer
@ -239,12 +249,12 @@ class TUIConsole:
new_pos = doc.translate_row_col_to_index(new_row, 0) new_pos = doc.translate_row_col_to_index(new_row, 0)
buf.cursor_position = new_pos buf.cursor_position = new_pos
@kb.add("home", filter=lambda: self.app.layout.has_focus(self.output.buffer)) @kb.add("home", filter=output_focus_filter)
def handle_output_home(event): def handle_output_home(event):
"""Scroll to top of output.""" """Scroll to top of output."""
self.output.buffer.cursor_position = 0 self.output.buffer.cursor_position = 0
@kb.add("end", filter=lambda: self.app.layout.has_focus(self.output.buffer)) @kb.add("end", filter=output_focus_filter)
def handle_output_end(event): def handle_output_end(event):
"""Scroll to bottom of output.""" """Scroll to bottom of output."""
self.output.buffer.cursor_position = len(self.output.buffer.text) self.output.buffer.cursor_position = len(self.output.buffer.text)