Skip to content

refactor(tui): separate UI state from terminal input and rendering; slim TuiHost #526

Closed

Description

@ajianaz

What problem does this solve?

A second UI (GPUI desktop) can only reuse tole-tui's logic if that logic is not welded to the terminal. Findings from reading the crate:

  • lib.rs is ~2170 lines. editor, files, menu, transcript and sanitize already import no ratatui/crossterm.
  • App::on_key takes a crossterm KeyEvent (about 100 KeyEvent/KeyCode mentions) and App::draw is ratatui. State, key decoding and rendering live in one module.
  • Msg, Action, HostCmd, Footer and transcript::Kind/Out are already UI-neutral.
  • TuiHost has 11 methods, 8 of which default to unsupported() and duplicate the HostCmd enum. It has one adapter (TuiSession), so the seam is hypothetical today.

Proposed solution

Inside tole-tui first, no new crate:

  1. Introduce a UI-neutral Input enum (chars, Enter, Alt+Enter, arrows, Esc, Ctrl combos, paste); App::on_input(Input) -> Action. A thin crossterm adapter maps KeyEvent to Input. The existing keybinding table in docs/tui.md stays the behavior contract.
  2. Keep draw in a rendering module that reads App state; no state changes there.
  3. Collapse the per-command TuiHost methods into one apply(HostCmd) path (plus run_turn, footer, sessions), so a remote adapter has a small interface to implement.
  4. Model a pending approval as source-agnostic UI state (today: in-process Prompter; remote: the /approvals queue). It carries the risk tier so a UI never offers allow-always for Destructive.

Extract a tole-ui-core crate only when the GPUI client (separate issue) starts consuming this state, i.e. when there is a second real consumer. Until then the module boundary inside tole-tui is enough (AGENTS.md: adding code is a last resort).

Alternatives considered

  • Create tole-ui-core now: rejected, one consumer means a speculative seam.
  • Copy the state machine into the desktop crate: rejected, two copies of keybinding/steer/queue semantics will drift.

Acceptance criteria

  • App has no crossterm types; terminal key mapping is an adapter.
  • Behavior unchanged: all tole-tui tests pass; docs/tui.md key table still true.
  • TuiHost shrinks (target: a handful of methods) without losing a slash command.
  • A fake host in tests exercises the same interface a remote adapter will use.
  • Not blocked on the serve issue, but should land after the session-host extraction to avoid reworking TuiSession twice.

Part of #523. Related: #464 (epic: tole-tui), #463 (themes, keybindings).

Activity

  1. added
    enhancement
    on Nov 10, 2026
  2. ajianaz commented on Nov 10, 2026

    @ajianaz
    ContributorAuthor

    Acceptance status at close (all four parts merged; last one is #555):

    • App has no crossterm types: met. App::on_input(Input) is the logic entry; the crossterm mapping is the term_input adapter (App::on_key is the terminal entry point). Every key the logic does not distinguish is Key::Other.
    • Behavior unchanged: met. All tole-tui tests (76) and the 5 pty tui_e2e tests pass; the key table in docs/tui.md is the contract for both entry points.
    • TuiHost shrinks: met. 11 methods down to 3 (footer, run_turn, apply(HostCmd) -> Result<HostReply, String>); no slash command lost.
    • Fake host in tests: met. The tests drive the app through a fake host over the same apply interface a remote adapter would implement.
    • Approval as source-agnostic state: met. ApprovalRequest carries the Risk tier (can_allow_always() keeps Destructive never allow-always), Prompter::ask blocks for in-process turns, Prompter::post takes a reply closure for a remote source; an unanswered request is denied on drop.
    • Rendering split: met in refactor(tui): move rendering into a render module (#526) #555. App::draw and the styling helpers live in a private render module that only reads App.
    • Ordering vs the serve/session-host work: the session-host extraction landed first (refactor: extract one session-host module from tole-cli (merge Sessions and TuiSession) #525 parts), so TuiSession was not reworked twice.

    Not done, on purpose: no tole-ui-core crate (still no second consumer, as the issue says). A remote adapter that implements TuiHost is the next consumer and belongs to the client issues (#527 to #529).


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions