Skip to content

trading_agent.terminal.widgets.command_menu

The menu that opens above the message box when the user types a slash, as in Claude Code.

It lists the terminal program's own commands, then the desk's commands and skills, keeping those whose names start with what was typed. After an @ it lists files and folders instead, for attaching a file. Each name appears once, with the usage and description of its first entry.

Typical usage example:

menu = CommandMenu() menu.set_entries(entries) menu.filter('/st') menu.highlighted_name() # '/status'

CommandMenu

CommandMenu()

Bases: OptionList

The command menu; hidden while nothing matches.

Creates the menu, hidden and empty.

Source code in src/trading_agent/terminal/widgets/command_menu.py
def __init__(self):
    """Creates the menu, hidden and empty."""
    super().__init__(id='command-menu')
    self._entries: list[dict[str, Any]] = []
    self._shown: list[dict[str, Any]] = []
    self.can_focus = False
    self.showing_paths = False

on_mount

on_mount()

Hides the menu until something matches.

Source code in src/trading_agent/terminal/widgets/command_menu.py
def on_mount(self) -> None:
    """Hides the menu until something matches."""
    self.display = False

set_entries

set_entries(entries)

Replaces the commands the menu offers.

Parameters:

Name Type Description Default
entries list[dict[str, Any]]

The entries, each with name, usage and description, in the order to show them.

required
Source code in src/trading_agent/terminal/widgets/command_menu.py
def set_entries(self, entries: list[dict[str, Any]]) -> None:
    """Replaces the commands the menu offers.

    Args:
        entries (list[dict[str, Any]]): The entries, each with name, usage and description, in the order to show them.
    """
    self._entries = []
    seen = set()
    for entry in entries:
        if entry['name'] in seen:
            continue
        seen.add(entry['name'])
        self._entries.append(entry)

show_paths

show_paths(paths)

Shows file paths for an @ word.

Parameters:

Name Type Description Default
paths list[str]

The paths, as they would be typed after @.

required

Returns:

Type Description
bool

True when the menu is showing.

Source code in src/trading_agent/terminal/widgets/command_menu.py
def show_paths(self, paths: list[str]) -> bool:
    """Shows file paths for an @ word.

    Args:
        paths (list[str]): The paths, as they would be typed after @.

    Returns:
        bool: True when the menu is showing.
    """
    self.showing_paths = True
    self._shown = []
    for path in paths:
        self._shown.append(
            {
                'name': path,
                'usage': f'@{path}',
                'description': '',
            }
        )
    return self._redraw()

filter

filter(draft)

Shows the commands whose names start with the draft.

Parameters:

Name Type Description Default
draft str

What is in the message box.

required

Returns:

Type Description
bool

True when the menu is showing.

Source code in src/trading_agent/terminal/widgets/command_menu.py
def filter(self, draft: str) -> bool:
    """Shows the commands whose names start with the draft.

    Args:
        draft (str): What is in the message box.

    Returns:
        bool: True when the menu is showing.
    """
    self.showing_paths = False
    self._shown = []
    if draft.startswith('/') and ' ' not in draft and '\n' not in draft:
        for entry in self._entries:
            if entry['name'].startswith(draft.lower()):
                self._shown.append(entry)
    return self._redraw()

move

move(step)

Moves the highlight, wrapping at the ends.

Parameters:

Name Type Description Default
step int

-1 for up, 1 for down.

required
Source code in src/trading_agent/terminal/widgets/command_menu.py
def move(self, step: int) -> None:
    """Moves the highlight, wrapping at the ends.

    Args:
        step (int): -1 for up, 1 for down.
    """
    if not self._shown:
        return
    current = self.highlighted or 0
    self.highlighted = (current + step) % len(self._shown)

highlighted_name

highlighted_name()

The highlighted command's name.

Returns:

Type Description
str | None

Such as "/status", or None when the menu is empty.

Source code in src/trading_agent/terminal/widgets/command_menu.py
def highlighted_name(self) -> str | None:
    """The highlighted command's name.

    Returns:
        str | None: Such as "/status", or None when the menu is empty.
    """
    if not self._shown or self.highlighted is None:
        return None
    return self._shown[self.highlighted]['name']

hide

hide()

Hides the menu.

Source code in src/trading_agent/terminal/widgets/command_menu.py
def hide(self) -> None:
    """Hides the menu."""
    self._shown = []
    self.showing_paths = False
    self.display = False