mirror of
https://github.com/Routstr/routstrd.git
synced 2026-10-05 12:28:23 +00:00
3.6 KiB
3.6 KiB
TUI refactor plan
Goals
- Move the usage TUI implementation out of
src/cli/usage-tui.tsinto a dedicatedsrc/tui/folder. - Reduce the size and responsibility of the current monolithic file.
- Keep the existing CLI entrypoint stable so current usage does not break.
- Preserve behavior while making future TUI work easier.
Current state
src/cli/usage-tui.ts currently mixes several concerns in one file:
- TUI-specific types and constants
- ANSI/terminal helpers
- scroll/search/vim navigation state
- data fetching from the daemon
- usage aggregation/stat helpers
- rendering for all tabs
- app lifecycle and keyboard event handling
This makes the file hard to extend safely.
Refactor strategy
Do this incrementally and keep a thin compatibility wrapper in src/cli/usage-tui.ts.
Target structure
src/tui/usage/index.ts- public entrypoint:
runUsageTui()
- public entrypoint:
src/tui/usage/types.tsUsageStats, tab ids, tab metadata, derived stat types
src/tui/usage/constants.ts- tabs, colors, model/client color maps
src/tui/usage/terminal.ts- ANSI helpers, width/height helpers,
stripAnsi
- ANSI helpers, width/height helpers,
src/tui/usage/state.ts- vim/search/scroll state and state mutation helpers
src/tui/usage/data.tsfetchUsage()and usage aggregation helpers
src/tui/usage/render.ts- shared render helpers and tab renderers
src/tui/usage/app.ts- main loop, render orchestration, input handling, cleanup
src/cli/usage-tui.ts- compatibility wrapper that re-exports or calls
runUsageTui()fromsrc/tui/usage
- compatibility wrapper that re-exports or calls
Design choices
1. Keep CLI path compatibility
Do not delete the CLI file outright. Turn it into a tiny wrapper:
- minimal import from
../tui/usage/index.ts - export
runUsageTui()
This avoids breaking any existing imports or scripts.
2. Separate pure logic from side effects
Keep these pure where possible:
- aggregation helpers
- formatting helpers
- render helpers that return strings
- scroll clamping logic
Keep side effects isolated in the app layer:
- reading terminal size
- writing to stdout
- raw mode setup
- signal handling
- interval scheduling
3. Avoid over-engineering
This should be a pragmatic refactor, not a framework:
- no unnecessary classes
- keep function-based design
- only extract modules around clear responsibility boundaries
4. Preserve behavior first
No UX changes unless needed to support the extraction. That means:
- same tabs
- same keybindings
- same output format
- same fetch cadence
- same search/scroll behavior
Implementation steps
- Create
src/tui/usage/. - Extract types/constants first.
- Extract terminal helpers.
- Extract data fetching + aggregation helpers.
- Extract state/search/scroll logic.
- Extract rendering helpers + tab renderers.
- Build
app.tsusing the extracted modules. - Replace
src/cli/usage-tui.tswith a thin wrapper. - Run a TypeScript/bun check and fix imports.
- Smoke-test keyboard handling and rendering behavior.
Risks
- circular imports between render/state/constants
- broken relative import paths during extraction
- subtle behavior regressions in scroll/search state
- terminal escape handling differences if helpers are split carelessly
Validation checklist
src/cli/usage-tui.tsstill exposesrunUsageTui()- TUI starts from the same CLI path
- scroll still works for long content
- vim keys still work
- arrow keys still work
- tab switching still resets scroll
- search mode still works
- cleanup still restores cursor and alternate screen
Non-goals
- redesigning the UI
- changing tab contents
- introducing tests unless needed for safety
- adding new features unrelated to the refactor