Skip to content

trading_agent.terminal.widgets.history_search

A dialog for finding an earlier prompt, opened with Ctrl+R as in Claude Code.

It returns the chosen prompt, or None when the user presses Escape.

Typical usage example:

app.push_screen(HistorySearch(history), on_chosen)

HistorySearch

HistorySearch(history)

Bases: ModalScreen[str | None]

The search box and its matches.

Creates the dialog.

Parameters:

Name Type Description Default
history PromptHistory

The earlier prompts.

required
Source code in src/trading_agent/terminal/widgets/history_search.py
def __init__(self, history: prompt_history_module.PromptHistory):
    """Creates the dialog.

    Args:
        history (prompt_history_module.PromptHistory): The earlier prompts.
    """
    super().__init__(classes='picker-screen')
    self._history = history
    self._matches: list[str] = []

compose

compose()

Builds the dialog.

Yields:

Type Description
Widget

The title, the search box, the matches and the hint.

Source code in src/trading_agent/terminal/widgets/history_search.py
def compose(self):
    """Builds the dialog.

    Yields:
        Widget: The title, the search box, the matches and the hint.
    """
    with containers.Vertical(classes='picker'):
        yield widgets.Static(
            '[b]Search earlier prompts[/b]', classes='picker-title'
        )
        yield widgets.Input(placeholder='words to look for', id='search')
        yield widgets.OptionList(id='matches')
        yield widgets.Static(
            '[dim]type to search · ↑↓ to move · enter to use · esc to close[/dim]',
            classes='picker-hint',
        )

on_mount

on_mount()

Lists the newest prompts.

Source code in src/trading_agent/terminal/widgets/history_search.py
def on_mount(self) -> None:
    """Lists the newest prompts."""
    self._show('')
    self.query_one('#search', widgets.Input).focus()

on_input_changed

on_input_changed(event)

Lists the prompts that match the search.

Parameters:

Name Type Description Default
event Changed

The new search text.

required
Source code in src/trading_agent/terminal/widgets/history_search.py
def on_input_changed(self, event: widgets.Input.Changed) -> None:
    """Lists the prompts that match the search.

    Args:
        event (widgets.Input.Changed): The new search text.
    """
    self._show(event.value)

on_key

on_key(event)

Moves through the matches while the search box has the focus.

Parameters:

Name Type Description Default
event Key

The key press.

required
Source code in src/trading_agent/terminal/widgets/history_search.py
def on_key(self, event: events.Key) -> None:
    """Moves through the matches while the search box has the focus.

    Args:
        event (events.Key): The key press.
    """
    matches = self.query_one('#matches', widgets.OptionList)
    if event.key in ('up', 'down') and self._matches:
        event.stop()
        current = matches.highlighted or 0
        step = 1
        if event.key == 'up':
            step = -1
        matches.highlighted = (current + step) % len(self._matches)

on_input_submitted

on_input_submitted(event)

Uses the highlighted prompt.

Parameters:

Name Type Description Default
event Submitted

The submission.

required
Source code in src/trading_agent/terminal/widgets/history_search.py
def on_input_submitted(self, event: widgets.Input.Submitted) -> None:
    """Uses the highlighted prompt.

    Args:
        event (widgets.Input.Submitted): The submission.
    """
    matches = self.query_one('#matches', widgets.OptionList)
    if not self._matches or matches.highlighted is None:
        self.dismiss(None)
        return
    self.dismiss(self._matches[matches.highlighted])

on_option_list_option_selected

on_option_list_option_selected(event)

Uses a clicked prompt.

Parameters:

Name Type Description Default
event OptionSelected

The choice.

required
Source code in src/trading_agent/terminal/widgets/history_search.py
def on_option_list_option_selected(
    self, event: widgets.OptionList.OptionSelected
) -> None:
    """Uses a clicked prompt.

    Args:
        event (widgets.OptionList.OptionSelected): The choice.
    """
    self.dismiss(self._matches[event.option_index])

action_cancel

action_cancel()

Closes the dialog without a choice.

Source code in src/trading_agent/terminal/widgets/history_search.py
def action_cancel(self) -> None:
    """Closes the dialog without a choice."""
    self.dismiss(None)