Skip to content

routing_matrix

routing_matrix

A patchbay-style grid for wiring keys to scopes.

Rows are keys, columns are scopes, and every intersection is a connection the operator can toggle — the same mental model as a MIDI routing matrix or a studio patchbay. A moving crosshair (the cursor's whole row and column light up) keeps "which key, which scope" legible even when the grid is sparse.

The widget is a pure view: it renders the routing it is handed and emits an intent when the operator toggles a cell or asks to mint a key — it never touches a vault or a backend itself. That keeps it reusable by any Textual app willing to supply MatrixKey rows and persist the CellToggled intents it posts.

MatrixKey(key_id, label) dataclass

One key on the row axis: a stable id and the label to show for it.

key_id instance-attribute

label instance-attribute

RoutingMatrix(**kwargs)

Bases: Static

An interactive keys×scopes grid that emits wiring intents.

Start empty; the screen feeds routing through set_routing.

Source code in src/terok/tui/widgets/routing_matrix.py
def __init__(self, **kwargs: Any) -> None:
    """Start empty; the screen feeds routing through ``set_routing``."""
    super().__init__(**kwargs)
    self._keys: list[MatrixKey] = []
    self._scopes: list[str] = []
    self._links: set[tuple[str, int]] = set()
    self._defaults: dict[str, int] = {}
    self._row = 0
    self._col = 0

can_focus = True class-attribute instance-attribute

DEFAULT_CSS = '\n RoutingMatrix {\n height: auto;\n padding: 1 2;\n }\n RoutingMatrix:focus {\n background: $surface;\n }\n ' class-attribute instance-attribute

BINDINGS = [Binding('up,k', 'move(-1, 0)', 'Up', show=False), Binding('down,j', 'move(1, 0)', 'Down', show=False), Binding('left,h', 'move(0, -1)', 'Left', show=False), Binding('right,l', 'move(0, 1)', 'Right', show=False), Binding('space,enter', 'toggle_cell', 'Toggle link'), Binding('n', 'mint', 'Mint key for column')] class-attribute instance-attribute

cursor_key property

The key under the cursor, or None when there are no rows.

cursor_scope property

The scope under the cursor, or None when there are no columns.

CellToggled(scope, key_id, linked)

Bases: Message

Posted when the operator toggles the cursor cell.

linked is the cell's state before the toggle, so a handler knows whether it is wiring a new link or cutting an existing one.

Source code in src/terok/tui/widgets/routing_matrix.py
def __init__(self, scope: str, key_id: int, linked: bool) -> None:
    super().__init__()
    self.scope = scope
    self.key_id = key_id
    self.linked = linked
scope = scope instance-attribute
key_id = key_id instance-attribute
linked = linked instance-attribute

MintRequested(scope)

Bases: Message

Posted when the operator asks to mint a key for the cursor column.

Source code in src/terok/tui/widgets/routing_matrix.py
def __init__(self, scope: str) -> None:
    super().__init__()
    self.scope = scope
scope = scope instance-attribute

set_routing(keys, scopes, links, defaults=None)

Replace the rendered routing and clamp the cursor into the new grid.

Source code in src/terok/tui/widgets/routing_matrix.py
def set_routing(
    self,
    keys: list[MatrixKey],
    scopes: list[str],
    links: set[tuple[str, int]],
    defaults: dict[str, int] | None = None,
) -> None:
    """Replace the rendered routing and clamp the cursor into the new grid."""
    self._keys = keys
    self._scopes = scopes
    self._links = links
    self._defaults = defaults or {}
    self._row = _clamp(self._row, len(keys))
    self._col = _clamp(self._col, len(scopes))
    self.refresh()

action_move(drow, dcol)

Step the cursor, clamped to the grid edges.

Source code in src/terok/tui/widgets/routing_matrix.py
def action_move(self, drow: int, dcol: int) -> None:
    """Step the cursor, clamped to the grid edges."""
    self._row = _clamp(self._row + drow, len(self._keys))
    self._col = _clamp(self._col + dcol, len(self._scopes))
    self.refresh()

action_toggle_cell()

Emit a toggle intent for the cursor cell.

Source code in src/terok/tui/widgets/routing_matrix.py
def action_toggle_cell(self) -> None:
    """Emit a toggle intent for the cursor cell."""
    key, scope = self.cursor_key, self.cursor_scope
    if key is None or scope is None:
        return
    self.post_message(self.CellToggled(scope, key.key_id, (scope, key.key_id) in self._links))

action_mint()

Emit a mint intent for the cursor column.

Source code in src/terok/tui/widgets/routing_matrix.py
def action_mint(self) -> None:
    """Emit a mint intent for the cursor column."""
    if (scope := self.cursor_scope) is not None:
        self.post_message(self.MintRequested(scope))

render()

Assemble the legend, the numbered header, the key rows, and a status line.

Source code in src/terok/tui/widgets/routing_matrix.py
def render(self) -> Text:
    """Assemble the legend, the numbered header, the key rows, and a status line."""
    if not self._scopes:
        return Text("No projects to route keys to.", style=_DIM_STYLE)
    out = Text()
    out.append_text(self._render_legend())
    out.append("\n")
    out.append_text(self._render_header())
    out.append("\n")
    if self._keys:
        for index, key in enumerate(self._keys):
            out.append_text(self._render_row(index, key))
            out.append("\n")
    else:
        out.append(_pad(" ", _LABEL_WIDTH))
        out.append("no keys yet — press n to mint one\n", style=_DIM_STYLE)
    out.append("\n")
    out.append_text(self._render_status())
    return out