|
| 1 | +# TUI Theme System Design |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Design for runtime theme switching and the command infrastructure that supports it. |
| 6 | + |
| 7 | +## Command System Architecture |
| 8 | + |
| 9 | +### Command Trait |
| 10 | + |
| 11 | +```rust |
| 12 | +pub enum CommandResult { |
| 13 | + Ok(Option<String>), |
| 14 | + Err(String), |
| 15 | + Quit, |
| 16 | +} |
| 17 | + |
| 18 | +pub trait Command: Send + Sync { |
| 19 | + fn name(&self) -> &str; |
| 20 | + fn execute(&mut self, args: &[&str], app: &mut App) -> CommandResult; |
| 21 | + fn completions(&self, args: &[&str], app: &App) -> Vec<String> { vec![] } |
| 22 | + fn help(&self) -> &str { "" } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +### Command Registry |
| 27 | + |
| 28 | +```rust |
| 29 | +pub struct CommandRegistry { |
| 30 | + commands: Vec<Box<dyn Command>>, |
| 31 | +} |
| 32 | + |
| 33 | +impl CommandRegistry { |
| 34 | + pub fn register(&mut self, cmd: Box<dyn Command>); |
| 35 | + pub fn execute(&mut self, input: &str, app: &mut App) -> CommandResult; |
| 36 | + pub fn completions(&self, input: &str, app: &App) -> Vec<String>; |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +### Command Input State |
| 41 | + |
| 42 | +```rust |
| 43 | +pub struct CommandInput { |
| 44 | + pub buffer: String, |
| 45 | + pub cursor: usize, |
| 46 | + pub completions: Vec<String>, |
| 47 | + pub completion_idx: Option<usize>, |
| 48 | + pub message: Option<(String, bool)>, |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +## ThemeCommand |
| 53 | + |
| 54 | +Handles `:theme`, `:theme <name>`, and `:theme save`. |
| 55 | + |
| 56 | +```rust |
| 57 | +pub struct ThemeCommand { |
| 58 | + loader: ThemeLoader, |
| 59 | +} |
| 60 | + |
| 61 | +impl Command for ThemeCommand { |
| 62 | + fn name(&self) -> &str { "theme" } |
| 63 | + |
| 64 | + fn execute(&mut self, args: &[&str], app: &mut App) -> CommandResult { |
| 65 | + match args { |
| 66 | + [] => list_themes(), |
| 67 | + ["save"] => save_to_config(), |
| 68 | + [name] => switch_theme(name), |
| 69 | + _ => usage_error(), |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + fn completions(&self, args: &[&str], _app: &App) -> Vec<String> { |
| 74 | + // Theme names + "save" |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## Config Persistence |
| 80 | + |
| 81 | +`:theme save` writes to `~/.config/vibes/config.toml`: |
| 82 | + |
| 83 | +- Creates file if missing |
| 84 | +- Preserves existing content |
| 85 | +- Updates `[theme].active` field |
| 86 | + |
| 87 | +## UI Integration |
| 88 | + |
| 89 | +### Command Bar Widget |
| 90 | + |
| 91 | +Renders at bottom of screen during Command mode: |
| 92 | +- Shows `:` prompt with input buffer |
| 93 | +- Displays cursor at correct position |
| 94 | +- Shows completion dropdown when available |
| 95 | +- Displays success/error messages after execution |
| 96 | + |
| 97 | +### App Integration |
| 98 | + |
| 99 | +```rust |
| 100 | +pub struct App { |
| 101 | + pub command_input: CommandInput, |
| 102 | + pub command_registry: CommandRegistry, |
| 103 | + // ... existing fields |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +Mode::Command handling: |
| 108 | +- `:` key enters Command mode |
| 109 | +- Keys route to CommandInput methods |
| 110 | +- Enter executes via CommandRegistry |
| 111 | +- Escape clears and returns to Normal |
| 112 | + |
| 113 | +## Files |
| 114 | + |
| 115 | +### New Files |
| 116 | + |
| 117 | +| File | Purpose | |
| 118 | +|------|---------| |
| 119 | +| `commands/mod.rs` | Command trait, CommandResult | |
| 120 | +| `commands/registry.rs` | CommandRegistry | |
| 121 | +| `commands/input.rs` | CommandInput state | |
| 122 | +| `commands/theme.rs` | ThemeCommand | |
| 123 | +| `widgets/command_bar.rs` | Command bar widget | |
| 124 | + |
| 125 | +### Modified Files |
| 126 | + |
| 127 | +| File | Changes | |
| 128 | +|------|---------| |
| 129 | +| `app.rs` | Add command fields, wire Mode::Command | |
| 130 | +| `lib.rs` | Export commands module | |
| 131 | +| `widgets/mod.rs` | Export CommandBarWidget | |
0 commit comments