Skip to content

key_routing_screen

key_routing_screen

Screens for managing vault SSH keys and their project links.

Two views over the same many-to-many relationship between deploy keys and projects:

  • KeyRoutingScreen — the routing matrix (keys × projects), where the operator wires and unwires links directly. A list-mode fallback (a key list beside a project checklist) covers terminals too narrow for the grid.
  • KeyInventoryScreen — the key catalog: mint a new key for a project, inspect a key's fingerprint and the projects it serves, or delete it everywhere.

Both drive the verbs in terok.lib.api.ssh_routing and share a common base for the load → mutate → reload cycle. They are reusable outside terok-tui: KeyRoutingApp runs the matrix as a standalone Textual app, mirroring terok clearance.

KeyRoutingScreen()

Bases: _BaseRoutingScreen

The routing matrix plus a master-detail list fallback.

Start in matrix mode.

Source code in src/terok/tui/key_routing_screen.py
def __init__(self) -> None:
    """Start in matrix mode."""
    super().__init__()
    self._list_mode = False

BINDINGS = [_modal_binding('escape', 'dismiss_screen', 'Back'), _modal_binding('q', 'dismiss_screen', 'Back'), _modal_binding('m', 'toggle_mode', 'Matrix / list'), _modal_binding('n', 'pick_project_to_mint', 'Mint key'), _modal_binding('p', 'show_cursor_public_key', 'Public key'), _modal_binding('f', 'set_cursor_default', 'Make default'), _modal_binding('c', 'rename_cursor_key', 'Rename key'), _modal_binding('d', 'delete_cursor_key', 'Delete key'), _modal_binding('i', 'show_inventory', 'Inventory'), _modal_binding('r', 'reload', 'Refresh'), _modal_binding('h', 'focus_keys', 'Keys pane'), _modal_binding('l', 'focus_projects', 'Projects pane')] class-attribute instance-attribute

CSS = '\n KeyRoutingScreen { layout: vertical; background: $background; }\n #kr-header { height: 1; background: $primary; color: $text; padding: 0 1; }\n #kr-hint { height: auto; color: $text-muted; padding: 0 1; }\n #kr-list { height: 1fr; }\n #kr-keys { width: 40%; border: round $primary; }\n #kr-projects { width: 1fr; border: round $primary; }\n ' class-attribute instance-attribute

compose()

Header, the matrix, and the (initially hidden) list-mode panes.

Source code in src/terok/tui/key_routing_screen.py
def compose(self) -> ComposeResult:
    """Header, the matrix, and the (initially hidden) list-mode panes."""
    yield Static(" SSH Key Routing", id="kr-header")
    yield Static(_hint(list_mode=False), id="kr-hint")
    with VerticalScroll(id="kr-matrix"):
        yield RoutingMatrix()
    with Horizontal(id="kr-list"):
        keys = ListView(id="kr-keys")
        keys.border_title = "Keys"
        yield keys
        projects: SelectionList[str] = SelectionList(id="kr-projects")
        projects.border_title = "Linked projects"
        yield projects

on_mount()

Load routing, show the matrix, hide the list panes.

Source code in src/terok/tui/key_routing_screen.py
def on_mount(self) -> None:
    """Load routing, show the matrix, hide the list panes."""
    self.query_one("#kr-list").display = False
    self.reload()
    self.query_one(RoutingMatrix).focus()

on_routing_matrix_cell_toggled(event)

Wire a link, or unwire one (confirming a last link).

Source code in src/terok/tui/key_routing_screen.py
def on_routing_matrix_cell_toggled(self, event: RoutingMatrix.CellToggled) -> None:
    """Wire a link, or unwire one (confirming a last link)."""
    if event.linked:
        self._unlink(event.scope, event.key_id)
    else:
        self._link(event.scope, event.key_id)

on_routing_matrix_mint_requested(event)

Mint a fresh key for the cursor column.

Source code in src/terok/tui/key_routing_screen.py
def on_routing_matrix_mint_requested(self, event: RoutingMatrix.MintRequested) -> None:
    """Mint a fresh key for the cursor column."""
    self._mint(event.scope)

on_list_view_highlighted(event)

Repopulate the project checklist for the highlighted key.

Source code in src/terok/tui/key_routing_screen.py
def on_list_view_highlighted(self, event: ListView.Highlighted) -> None:
    """Repopulate the project checklist for the highlighted key."""
    if self._routing is not None and event.item is not None:
        self._fill_checklist(self._routing, _item_key_id(event.item))

on_selection_list_selection_toggled(event)

Translate a checklist tick into a link, a clear into an unlink.

Source code in src/terok/tui/key_routing_screen.py
def on_selection_list_selection_toggled(
    self, event: SelectionList.SelectionToggled[str]
) -> None:
    """Translate a checklist tick into a link, a clear into an unlink."""
    key_id = self._highlighted_key_id()
    if key_id is None:
        return
    scope = event.selection.value
    if scope in event.selection_list.selected:
        self._link(scope, key_id)
    else:
        self._unlink(scope, key_id)

action_toggle_mode()

Swap between the matrix grid and the list panes.

Source code in src/terok/tui/key_routing_screen.py
def action_toggle_mode(self) -> None:
    """Swap between the matrix grid and the list panes."""
    self._list_mode = not self._list_mode
    self.query_one("#kr-matrix").display = not self._list_mode
    self.query_one("#kr-list").display = self._list_mode
    self.query_one("#kr-hint", Static).update(_hint(list_mode=self._list_mode))
    if self._routing is not None:
        self._sync_widgets(self._routing)
    self.query_one("#kr-keys" if self._list_mode else RoutingMatrix).focus()

action_focus_keys()

Move to the key list (list mode).

Source code in src/terok/tui/key_routing_screen.py
def action_focus_keys(self) -> None:
    """Move to the key list (list mode)."""
    if self._list_mode:
        self.query_one("#kr-keys").focus()

action_focus_projects()

Move to the project checklist (list mode).

Source code in src/terok/tui/key_routing_screen.py
def action_focus_projects(self) -> None:
    """Move to the project checklist (list mode)."""
    if self._list_mode:
        self.query_one("#kr-projects").focus()

action_pick_project_to_mint()

Ask for a project when the focused list cannot infer one.

Source code in src/terok/tui/key_routing_screen.py
def action_pick_project_to_mint(self) -> None:
    """Ask for a project when the focused list cannot infer one."""
    self._pick_project_to_mint()

action_rename_cursor_key()

Rename the key the cursor points at (matrix or list).

Source code in src/terok/tui/key_routing_screen.py
def action_rename_cursor_key(self) -> None:
    """Rename the key the cursor points at (matrix or list)."""
    if (key_id := self._current_key_id()) is not None:
        self._rename(key_id)

action_show_cursor_public_key()

Show the public key under the matrix cursor or list highlight.

Source code in src/terok/tui/key_routing_screen.py
def action_show_cursor_public_key(self) -> None:
    """Show the public key under the matrix cursor or list highlight."""
    if (key_id := self._current_key_id()) is not None:
        self._show_public_key(key_id)

action_set_cursor_default()

Offer the selected linked key first for the highlighted project.

Source code in src/terok/tui/key_routing_screen.py
def action_set_cursor_default(self) -> None:
    """Offer the selected linked key first for the highlighted project."""
    key_id, scope = self._current_key_id(), self._current_scope()
    if key_id is None or scope is None or self._routing is None:
        return
    if (scope, key_id) not in self._routing.links:
        self.app.notify(
            "Link this key to the project before making it default.", severity="warning"
        )
        return
    self._apply(lambda: set_default_key(scope, key_id), "Set default failed")

action_delete_cursor_key()

Delete the key the cursor points at (matrix or list).

Source code in src/terok/tui/key_routing_screen.py
def action_delete_cursor_key(self) -> None:
    """Delete the key the cursor points at (matrix or list)."""
    if (key_id := self._current_key_id()) is not None:
        self._delete(key_id)

action_show_inventory()

Open the key inventory; reload on return.

Source code in src/terok/tui/key_routing_screen.py
def action_show_inventory(self) -> None:
    """Open the key inventory; reload on return."""
    self.app.push_screen(KeyInventoryScreen(), lambda _: self.reload())

action_dismiss_screen()

Close the routing screen.

Source code in src/terok/tui/key_routing_screen.py
def action_dismiss_screen(self) -> None:
    """Close the routing screen."""
    self.dismiss(None)

KeyInventoryScreen()

Bases: _BaseRoutingScreen

A catalog of every vault key — mint, inspect, and delete.

Source code in src/terok/tui/key_routing_screen.py
def __init__(self) -> None:
    """Begin with no routing loaded."""
    super().__init__()
    self._routing: KeyRouting | None = None

BINDINGS = [_modal_binding('escape', 'dismiss_screen', 'Back'), _modal_binding('q', 'dismiss_screen', 'Back'), _modal_binding('n', 'mint', 'Mint key'), _modal_binding('p', 'show_public_key', 'Public key'), _modal_binding('c', 'rename', 'Rename key'), _modal_binding('d', 'delete', 'Delete key'), _modal_binding('r', 'reload', 'Refresh')] class-attribute instance-attribute

CSS = '\n KeyInventoryScreen { layout: vertical; background: $background; }\n #ki-header { height: auto; background: $primary; color: $text; padding: 0 1; }\n #ki-list { height: 1fr; border: round $primary; }\n ' class-attribute instance-attribute

compose()

Header plus the scrollable key list.

Source code in src/terok/tui/key_routing_screen.py
def compose(self) -> ComposeResult:
    """Header plus the scrollable key list."""
    yield Static(
        " SSH Key Inventory   n mint · p public key · c rename · d delete · Esc back",
        id="ki-header",
    )
    listing = ListView(id="ki-list")
    listing.border_title = "Keys"
    yield listing

on_mount()

Load the catalog and focus the list.

Source code in src/terok/tui/key_routing_screen.py
def on_mount(self) -> None:
    """Load the catalog and focus the list."""
    self.reload()
    self.query_one("#ki-list", ListView).focus()

action_mint()

Pick a project, then mint a key for it.

Source code in src/terok/tui/key_routing_screen.py
def action_mint(self) -> None:
    """Pick a project, then mint a key for it."""
    self._pick_project_to_mint()

action_rename()

Rename the highlighted key's comment.

Source code in src/terok/tui/key_routing_screen.py
def action_rename(self) -> None:
    """Rename the highlighted key's comment."""
    item = self.query_one("#ki-list", ListView).highlighted_child
    if item is not None:
        self._rename(_item_key_id(item))

action_show_public_key()

Show the highlighted key's public line for copying.

Source code in src/terok/tui/key_routing_screen.py
def action_show_public_key(self) -> None:
    """Show the highlighted key's public line for copying."""
    item = self.query_one("#ki-list", ListView).highlighted_child
    if item is not None:
        self._show_public_key(_item_key_id(item))

action_delete()

Delete the highlighted key everywhere.

Source code in src/terok/tui/key_routing_screen.py
def action_delete(self) -> None:
    """Delete the highlighted key everywhere."""
    item = self.query_one("#ki-list", ListView).highlighted_child
    if item is not None:
        self._delete(_item_key_id(item))

action_dismiss_screen()

Close the inventory.

Source code in src/terok/tui/key_routing_screen.py
def action_dismiss_screen(self) -> None:
    """Close the inventory."""
    self.dismiss(None)

KeyRoutingApp

Bases: App[None]

Standalone Textual app that opens only the routing matrix.

Lets another tool — or python -m — drive the key↔project matrix without the rest of terok-tui, the same way terok clearance ships ClearanceScreen.

TITLE = 'terok ssh-key routing' class-attribute instance-attribute

compose()

Pair an app-level footer with the pushed routing screen.

Source code in src/terok/tui/key_routing_screen.py
def compose(self) -> ComposeResult:
    """Pair an app-level footer with the pushed routing screen."""
    yield Footer()

on_mount()

Push the routing screen, exiting when it is dismissed.

Source code in src/terok/tui/key_routing_screen.py
def on_mount(self) -> None:
    """Push the routing screen, exiting when it is dismissed."""
    self.push_screen(KeyRoutingScreen(), callback=lambda _: self.exit())

main()

Entry point for running the routing matrix standalone.

Source code in src/terok/tui/key_routing_screen.py
def main() -> None:
    """Entry point for running the routing matrix standalone."""
    KeyRoutingApp().run()