tuisage 0.2.0

TUI application for interacting with CLI commands defined by usage specs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! Command tree panel component.
//!
//! Renders the command/subcommand tree as a selectable list with
//! indentation, aliases, fuzzy-match highlighting, and help overlays.
//! As a Component, owns tree state and handles navigation events.

use std::collections::HashMap;

use crossterm::event::{KeyCode, KeyEvent, MouseEvent};
use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::Style,
    text::{Line, Span},
    widgets::{List, ListItem, ListState, StatefulWidget, Widget},
};

use ratatui_interact::components::{TreeNode, TreeViewState};

use crate::app::{
    compute_tree_scores, flatten_command_tree, CmdData, FlatCommand,
    MatchScores,
};
use crate::theme::UiColors;

use super::filterable::Filterable;
use super::{
    build_help_line, find_adjacent_match, find_first_match, panel_block, panel_title,
    push_highlighted_name, push_selection_cursor, render_help_overlays, render_panel_scrollbar,
    selection_bg, Component, EventResult, ItemContext, PanelState, RenderableComponent,
};

// ── Actions ─────────────────────────────────────────────────────────

/// Actions that the command panel can emit to its parent.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CommandPanelAction {
    /// The selected command path changed — parent should sync flags/args.
    PathChanged(Vec<String>),
}

// ── CommandPanelComponent ───────────────────────────────────────────

/// A self-contained command tree panel that owns its navigation state.
pub struct CommandPanelComponent {
    /// Tree nodes representing the full command hierarchy.
    tree_nodes: Vec<TreeNode<CmdData>>,
    /// Tree view state (selection, scroll).
    tree_state: TreeViewState,
    /// Current command path derived from the tree selection.
    path: Vec<String>,

    // Display state (set by parent)
    focused: bool,
    /// Whether filter input is actively being typed (set by wrapper, used for border color).
    filter_active: bool,
    filter_text: String,
    match_scores: HashMap<String, MatchScores>,
    hovered_index: Option<usize>,
    /// Current mouse position (set by parent, used to compute hovered_index during render).
    mouse_position: Option<(u16, u16)>,
}

impl CommandPanelComponent {
    pub fn new(tree_nodes: Vec<TreeNode<CmdData>>) -> Self {
        let tree_state = TreeViewState::new();
        let mut component = Self {
            tree_nodes,
            tree_state,
            path: Vec::new(),
            focused: false,
            filter_active: false,
            filter_text: String::new(),
            match_scores: HashMap::new(),
            hovered_index: None,
            mouse_position: None,
        };
        component.sync_path();
        component
    }

    // ── Public API for parent ───────────────────────────────────────

    /// Current command path (e.g. ["config", "set"]).
    pub fn path(&self) -> &[String] {
        &self.path
    }

    #[allow(dead_code)]
    pub fn selected_index(&self) -> usize {
        self.tree_state.selected_index
    }

    pub fn scroll_offset(&self) -> usize {
        self.tree_state.scroll as usize
    }

    pub fn total_visible(&self) -> usize {
        flatten_command_tree(&self.tree_nodes).len()
    }

    #[allow(dead_code)]
    pub fn flat_commands(&self) -> Vec<FlatCommand> {
        flatten_command_tree(&self.tree_nodes)
    }

    #[allow(dead_code)]
    pub fn tree_nodes(&self) -> &[TreeNode<CmdData>] {
        &self.tree_nodes
    }

    /// Update the focused state from the parent.
    pub fn set_focused(&mut self, focused: bool) {
        self.focused = focused;
    }

    /// Update filter text and recompute match scores.
    pub fn set_filter(&mut self, text: &str) {
        self.filter_text = text.to_string();
        if text.is_empty() {
            self.match_scores.clear();
        } else {
            self.match_scores = compute_tree_scores(&self.tree_nodes, text);
        }
    }

    /// Update the hovered item index (from mouse position).
    #[allow(dead_code)]
    pub fn set_hovered_index(&mut self, idx: Option<usize>) {
        self.hovered_index = idx;
    }

    pub fn set_mouse_position(&mut self, pos: Option<(u16, u16)>) {
        self.mouse_position = pos;
    }

    /// Compute hovered_index from mouse_position and the given panel area.
    fn compute_hovered_index(&mut self, area: Rect) {
        self.hovered_index = if self.focused {
            self.hovered_index_for_area(area)
        } else {
            None
        };
    }

    fn hovered_index_for_area(&self, area: Rect) -> Option<usize> {
        let (col, row) = self.mouse_position?;
        let inner_top = area.y + 1;
        let inner_bottom = area.y + area.height.saturating_sub(1);
        if col < area.x || col >= area.x + area.width || row < inner_top || row >= inner_bottom {
            return None;
        }
        Some((row - inner_top) as usize + self.scroll_offset())
    }

    /// Ensure the selection is visible within the given viewport height.
    pub fn ensure_visible(&mut self, viewport_height: usize) {
        self.tree_state.ensure_visible(viewport_height);
    }

    /// Select a specific item by index (e.g. from mouse click).
    /// Returns true if the path changed.
    pub fn select_item(&mut self, index: usize) -> bool {
        let total = self.total_visible();
        if index < total {
            let old_path = self.path.clone();
            self.tree_state.selected_index = index;
            self.sync_path();
            self.path != old_path
        } else {
            false
        }
    }

    /// Navigate to a specific command path. Used for tests and programmatic navigation.
    #[allow(dead_code)]
    pub fn navigate_to(&mut self, target_path: &[&str]) {
        let target_id = target_path.join(" ");
        let flat = flatten_command_tree(&self.tree_nodes);
        if let Some(idx) = flat.iter().position(|cmd| cmd.id == target_id) {
            self.tree_state.selected_index = idx;
            self.sync_path();
        }
    }

    /// Auto-select the first matching item if current selection doesn't match.
    /// Returns true if the path changed.
    pub fn auto_select_next_match(&mut self) -> bool {
        if self.match_scores.is_empty() {
            return false;
        }
        let flat = flatten_command_tree(&self.tree_nodes);
        let keys: Vec<String> = flat.iter().map(|c| c.id.clone()).collect();
        let current = self.tree_state.selected_index;
        if let Some(idx) = find_first_match(&keys, &self.match_scores, current) {
            if idx != current {
                let old_path = self.path.clone();
                self.tree_state.selected_index = idx;
                self.sync_path();
                return self.path != old_path;
            }
        }
        false
    }

    /// Set the scroll offset directly (used by tests).
    #[allow(dead_code)]
    pub fn set_scroll(&mut self, scroll: usize) {
        self.tree_state.scroll = scroll as u16;
    }

    /// Expand a tree node by ID (used by tests to set up collapsed/expanded state).
    #[allow(dead_code)]
    pub fn expand(&mut self, id: &str) {
        self.tree_state.expand(id);
    }

    // ── Internal navigation ─────────────────────────────────────────

    /// Update path from the currently selected tree node (without calling App's sync_state).
    fn sync_path(&mut self) {
        if let Some(id) = self.selected_command_id() {
            if id.is_empty() {
                self.path = vec![];
            } else {
                self.path = id.split(' ').map(|s| s.to_string()).collect();
            }
        }
    }

    fn selected_command_id(&self) -> Option<String> {
        let flat = flatten_command_tree(&self.tree_nodes);
        flat.get(self.tree_state.selected_index)
            .map(|cmd| cmd.id.clone())
    }

    fn node_has_children(&self, id: &str) -> bool {
        fn find_in<T>(nodes: &[TreeNode<T>], id: &str) -> Option<bool> {
            for node in nodes {
                if node.id == id {
                    return Some(node.has_children());
                }
                if let Some(result) = find_in(&node.children, id) {
                    return Some(result);
                }
            }
            None
        }
        find_in(&self.tree_nodes, id).unwrap_or(false)
    }

    fn find_parent_index(&self) -> Option<usize> {
        let flat = flatten_command_tree(&self.tree_nodes);
        let selected_id = flat
            .get(self.tree_state.selected_index)
            .map(|cmd| cmd.id.clone())?;
        let parent = parent_id(&selected_id)?;
        flat.iter().position(|cmd| cmd.id == parent)
    }

    /// Move to first child of selected node (Right/l/Enter key).
    /// Returns true if the path changed.
    fn tree_expand_or_enter(&mut self) -> bool {
        if let Some(id) = self.selected_command_id() {
            if self.node_has_children(&id) {
                let old_path = self.path.clone();
                let total = self.total_visible();
                self.tree_state.select_next(total);
                self.sync_path();
                return self.path != old_path;
            }
        }
        false
    }

    /// Move to parent node (Left/h key).
    /// Returns true if the path changed.
    fn tree_collapse_or_parent(&mut self) -> bool {
        if let Some(parent_idx) = self.find_parent_index() {
            let old_path = self.path.clone();
            self.tree_state.selected_index = parent_idx;
            self.sync_path();
            return self.path != old_path;
        }
        false
    }

    /// Move selection up. Filter-aware when scores are present.
    /// Returns true if the path changed.
    pub fn move_up(&mut self) -> bool {
        let old_path = self.path.clone();
        if !self.filter_text.is_empty() && !self.match_scores.is_empty() {
            let flat = flatten_command_tree(&self.tree_nodes);
            let keys: Vec<String> = flat.iter().map(|c| c.id.clone()).collect();
            let current = self.tree_state.selected_index;
            if let Some(idx) = find_adjacent_match(&keys, &self.match_scores, current, false) {
                self.tree_state.selected_index = idx;
                self.sync_path();
            }
        } else {
            self.tree_state.select_prev();
            self.sync_path();
        }
        self.path != old_path
    }

    /// Move selection down. Filter-aware when scores are present.
    /// Returns true if the path changed.
    pub fn move_down(&mut self) -> bool {
        let old_path = self.path.clone();
        if !self.filter_text.is_empty() && !self.match_scores.is_empty() {
            let flat = flatten_command_tree(&self.tree_nodes);
            let keys: Vec<String> = flat.iter().map(|c| c.id.clone()).collect();
            let current = self.tree_state.selected_index;
            if let Some(idx) = find_adjacent_match(&keys, &self.match_scores, current, true) {
                self.tree_state.selected_index = idx;
                self.sync_path();
            }
        } else {
            let total = self.total_visible();
            self.tree_state.select_next(total);
            self.sync_path();
        }
        self.path != old_path
    }

    /// Build a PanelState for rendering.
    fn panel_state(&self, colors: &UiColors) -> PanelState {
        let border_color = if self.focused || self.filter_active {
            colors.active_border
        } else {
            colors.inactive_border
        };
        PanelState {
            is_focused: self.focused,
            is_filtering: self.filter_active,
            has_filter: !self.filter_text.is_empty(),
            border_color,
            filter_text: self.filter_text.clone(),
            match_scores: self.match_scores.clone(),
        }
    }
}

impl Component for CommandPanelComponent {
    type Action = CommandPanelAction;

    fn handle_focus_gained(&mut self) -> EventResult<CommandPanelAction> {
        self.focused = true;
        EventResult::NotHandled
    }

    fn handle_focus_lost(&mut self) -> EventResult<CommandPanelAction> {
        self.focused = false;
        EventResult::NotHandled
    }

    fn handle_key(&mut self, key: KeyEvent) -> EventResult<CommandPanelAction> {
        let path_changed = match key.code {
            KeyCode::Up | KeyCode::Char('k') => self.move_up(),
            KeyCode::Down | KeyCode::Char('j') => self.move_down(),
            KeyCode::Left | KeyCode::Char('h') => self.tree_collapse_or_parent(),
            KeyCode::Right | KeyCode::Char('l') | KeyCode::Enter => self.tree_expand_or_enter(),
            _ => return EventResult::NotHandled,
        };
        if path_changed {
            EventResult::Action(CommandPanelAction::PathChanged(self.path.clone()))
        } else {
            EventResult::Consumed
        }
    }

    fn handle_mouse(&mut self, event: MouseEvent, area: Rect) -> EventResult<CommandPanelAction> {
        use crossterm::event::{MouseButton, MouseEventKind};

        let row = event.row;

        match event.kind {
            MouseEventKind::Down(MouseButton::Left) => {
                let inner_top = area.y + 1; // skip border
                if row >= inner_top
                    && row < area.y + area.height.saturating_sub(1)
                    && event.column >= area.x
                    && event.column < area.x + area.width
                {
                    let clicked_offset = (row - inner_top) as usize;
                    let item_index = self.scroll_offset() + clicked_offset;
                    if self.select_item(item_index) {
                        return EventResult::Action(CommandPanelAction::PathChanged(
                            self.path.clone(),
                        ));
                    }
                    return EventResult::Consumed;
                }
                EventResult::NotHandled
            }
            MouseEventKind::ScrollUp => {
                if self.move_up() {
                    EventResult::Action(CommandPanelAction::PathChanged(self.path.clone()))
                } else {
                    EventResult::Consumed
                }
            }
            MouseEventKind::ScrollDown => {
                if self.move_down() {
                    EventResult::Action(CommandPanelAction::PathChanged(self.path.clone()))
                } else {
                    EventResult::Consumed
                }
            }
            _ => EventResult::NotHandled,
        }
    }
}

impl RenderableComponent for CommandPanelComponent {
    fn render(&mut self, area: Rect, buf: &mut Buffer, colors: &UiColors) {
        let inner_height = area.height.saturating_sub(2) as usize;
        self.ensure_visible(inner_height);
        self.compute_hovered_index(area);
        let ps = self.panel_state(colors);
        let flat_commands = flatten_command_tree(&self.tree_nodes);

        let panel = CommandPanel {
            flat_commands: &flat_commands,
            selected_index: self.tree_state.selected_index,
            hovered_index: self.hovered_index,
            scroll_offset: self.tree_state.scroll as usize,
            panel_state: &ps,
            colors,
        };
        panel.render(area, buf);
    }
}

impl Filterable for CommandPanelComponent {
    fn apply_filter(&mut self, text: &str) -> Option<CommandPanelAction> {
        self.set_filter(text);
        if self.auto_select_next_match() {
            Some(CommandPanelAction::PathChanged(self.path.clone()))
        } else {
            None
        }
    }

    fn clear_filter(&mut self) -> Option<CommandPanelAction> {
        self.filter_text.clear();
        self.match_scores.clear();
        self.filter_active = false;
        None
    }

    fn set_filter_active(&mut self, active: bool) {
        self.filter_active = active;
    }

    fn has_active_filter(&self) -> bool {
        !self.filter_text.is_empty()
    }

    fn filter_text(&self) -> &str {
        &self.filter_text
    }
}

// ── CommandPanel Widget (rendering) ─────────────────────────────────

/// Props for the command tree panel rendering.
///
/// Used internally by CommandPanelComponent::render().
struct CommandPanel<'a> {
    flat_commands: &'a [FlatCommand],
    selected_index: usize,
    hovered_index: Option<usize>,
    scroll_offset: usize,
    panel_state: &'a PanelState,
    colors: &'a UiColors,
}

impl Widget for CommandPanel<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let ps = self.panel_state;
        let colors = self.colors;

        let title = panel_title("Commands", ps);
        let block = panel_block(title, ps);

        // Build list items with indentation markers
        let mut help_entries: Vec<(usize, Line<'static>)> = Vec::new();
        let items: Vec<ListItem> = self
            .flat_commands
            .iter()
            .enumerate()
            .map(|(i, cmd)| {
                let is_selected = i == self.selected_index;
                let is_hovered = self.hovered_index == Some(i) && !is_selected;
                let ctx = ItemContext::new(&cmd.id, is_selected, ps);

                let mut spans = Vec::new();

                push_selection_cursor(&mut spans, is_selected, colors);

                // Depth-based indentation with vertical line prefix
                if cmd.depth > 0 {
                    let indent = "  ".repeat(cmd.depth - 1);
                    spans.push(Span::styled(indent, Style::default().fg(colors.help)));
                    spans.push(Span::styled("", Style::default().fg(colors.help)));
                }

                // Command name (with aliases)
                let name_text = if !cmd.aliases.is_empty() {
                    format!("{} ({})", cmd.name, cmd.aliases.join(", "))
                } else {
                    cmd.name.clone()
                };

                push_highlighted_name(&mut spans, &name_text, colors.command, &ctx, ps, colors);

                // Collect help text for overlay rendering
                if let Some(help) = &cmd.help {
                    help_entries.push((i, build_help_line(help, &ctx, ps, colors)));
                }

                let mut item = ListItem::new(Line::from(spans));
                if is_selected {
                    item = item.style(selection_bg(false, colors));
                } else if is_hovered {
                    item = item.style(Style::default().bg(colors.hover_bg));
                }
                item
            })
            .collect();

        let mut state = ListState::default()
            .with_selected(if ps.is_focused {
                Some(self.selected_index)
            } else {
                None
            })
            .with_offset(self.scroll_offset);

        let list = List::new(items).block(block);
        StatefulWidget::render(list, area, buf, &mut state);

        // Scrollbar
        render_panel_scrollbar(buf, area, self.flat_commands.len(), self.scroll_offset, colors);

        // Render right-aligned help text overlays
        let inner = area.inner(ratatui::layout::Margin::new(1, 1));
        render_help_overlays(buf, &help_entries, self.scroll_offset, inner);
    }
}

// ── Helper functions ────────────────────────────────────────────────

fn parent_id(id: &str) -> Option<String> {
    if id.is_empty() {
        None
    } else if let Some(pos) = id.rfind(' ') {
        Some(id[..pos].to_string())
    } else {
        Some(String::new())
    }
}