vtcode-tui 0.98.2

Reusable TUI primitives and session API for VT Code-style terminal interfaces
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
use unicode_segmentation::UnicodeSegmentation;

use crate::core_tui::app::types::SlashCommandItem;
use crate::core_tui::session::list_navigator::ListNavigator;
use crate::ui::search::{fuzzy_score, normalize_query};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SlashCommandRange {
    pub start: usize,
    pub end: usize,
}

pub fn command_range(input: &str, cursor: usize) -> Option<SlashCommandRange> {
    if !input.starts_with('/') {
        return None;
    }

    let mut active_range = None;

    for (index, grapheme) in input.grapheme_indices(true) {
        if index > cursor {
            break;
        }

        if grapheme == "/" {
            active_range = Some(SlashCommandRange {
                start: index,
                end: input.len(),
            });
        } else if grapheme.chars().all(char::is_whitespace) {
            // Space terminates the current command token
            active_range = None;
        } else if let Some(range) = &mut active_range {
            range.end = index + grapheme.len();
        }
    }

    active_range.filter(|range| range.end > range.start)
}

pub fn command_prefix(input: &str, cursor: usize) -> Option<String> {
    let range = command_range(input, cursor)?;
    let end = cursor.min(range.end);
    let start = range.start + 1;
    if end < start {
        return Some(String::new());
    }
    Some(input[start..end].to_owned())
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg(test)]
pub struct SlashPaletteHighlightSegment {
    pub content: String,
    pub highlighted: bool,
}

#[cfg(test)]
impl SlashPaletteHighlightSegment {
    #[cfg(test)]
    pub fn highlighted(content: impl Into<String>) -> Self {
        Self {
            content: content.into(),
            highlighted: true,
        }
    }

    #[cfg(test)]
    pub fn plain(content: impl Into<String>) -> Self {
        Self {
            content: content.into(),
            highlighted: false,
        }
    }
}

#[derive(Debug, Clone)]
#[cfg(test)]
pub struct SlashPaletteItem {
    #[allow(dead_code)]
    pub command: Option<SlashCommandItem>,
    pub name_segments: Vec<SlashPaletteHighlightSegment>,
    #[allow(dead_code)]
    pub description: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SlashPaletteUpdate {
    NoChange,
    Cleared,
    Changed {
        suggestions_changed: bool,
        selection_changed: bool,
    },
}

#[derive(Debug, Default)]
pub struct SlashPalette {
    commands: Vec<SlashCommandItem>,
    suggestions: Vec<SlashPaletteSuggestion>,
    navigator: ListNavigator,
    filter_query: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SlashPaletteSuggestion {
    Static(SlashCommandItem),
}

impl SlashPalette {
    pub fn new() -> Self {
        Self::with_commands(Vec::new())
    }

    pub fn with_commands(commands: Vec<SlashCommandItem>) -> Self {
        Self {
            commands,
            suggestions: Vec::new(),
            navigator: ListNavigator::new(),
            filter_query: None,
        }
    }

    pub fn suggestions(&self) -> &[SlashPaletteSuggestion] {
        &self.suggestions
    }

    pub fn is_empty(&self) -> bool {
        self.suggestions.is_empty()
    }

    pub fn selected_command(&self) -> Option<&SlashCommandItem> {
        self.navigator
            .selected()
            .and_then(|index| self.suggestions.get(index))
            .map(|suggestion| match suggestion {
                SlashPaletteSuggestion::Static(info) => info,
            })
    }

    pub fn selected_index(&self) -> Option<usize> {
        self.navigator.selected()
    }

    pub fn scroll_offset(&self) -> usize {
        self.navigator.scroll_offset()
    }

    pub(super) fn set_selected(&mut self, selected: Option<usize>) {
        self.navigator.set_selected(selected);
    }

    pub(super) fn set_scroll_offset(&mut self, offset: usize) {
        self.navigator.set_scroll_offset(offset);
    }

    pub fn clear_visible_rows(&mut self) {
        self.navigator.set_visible_rows(0);
    }

    pub fn set_visible_rows(&mut self, rows: usize) {
        self.navigator.set_visible_rows(rows);
    }

    #[cfg(test)]
    pub fn visible_rows(&self) -> usize {
        self.navigator.visible_rows()
    }

    pub fn update(&mut self, prefix: Option<&str>) -> SlashPaletteUpdate {
        let Some(prefix) = prefix else {
            if self.clear_internal() {
                return SlashPaletteUpdate::Cleared;
            }
            return SlashPaletteUpdate::NoChange;
        };
        let mut new_suggestions = Vec::new();

        // Handle regular slash commands
        let static_suggestions = self.suggestions_for(prefix);
        new_suggestions.extend(
            static_suggestions
                .into_iter()
                .map(SlashPaletteSuggestion::Static),
        );

        let filter_query = {
            let normalized = normalize_query(prefix);
            if normalized.is_empty() {
                None
            } else if new_suggestions
                .iter()
                .map(|suggestion| match suggestion {
                    SlashPaletteSuggestion::Static(info) => info,
                })
                .all(|info| info.name.starts_with(normalized.as_str()))
            {
                Some(normalized.clone())
            } else {
                None
            }
        };

        let suggestions_changed = self.replace_suggestions(new_suggestions);
        self.filter_query = filter_query;
        let selection_changed = self.ensure_selection();

        if suggestions_changed || selection_changed {
            SlashPaletteUpdate::Changed {
                suggestions_changed,
                selection_changed,
            }
        } else {
            SlashPaletteUpdate::NoChange
        }
    }

    pub fn clear(&mut self) -> bool {
        self.clear_internal()
    }

    pub fn move_up(&mut self) -> bool {
        self.navigator.move_up()
    }

    pub fn move_down(&mut self) -> bool {
        self.navigator.move_down()
    }

    pub fn select_first(&mut self) -> bool {
        self.navigator.select_first()
    }

    pub fn select_last(&mut self) -> bool {
        self.navigator.select_last()
    }

    pub fn page_up(&mut self) -> bool {
        let step = self.navigator.visible_rows().max(1);
        self.navigator.page_up(step)
    }

    pub fn page_down(&mut self) -> bool {
        let step = self.navigator.visible_rows().max(1);
        self.navigator.page_down(step)
    }

    pub fn select_index(&mut self, index: usize) -> bool {
        self.navigator.select_index(index)
    }

    #[cfg(test)]
    pub fn items(&self) -> Vec<SlashPaletteItem> {
        self.suggestions
            .iter()
            .map(|suggestion| match suggestion {
                SlashPaletteSuggestion::Static(command) => SlashPaletteItem {
                    command: Some(command.clone()),
                    name_segments: self.highlight_name_segments_static(command.name.as_str()),
                    description: command.description.to_owned(),
                },
            })
            .collect()
    }

    fn clear_internal(&mut self) -> bool {
        if self.suggestions.is_empty()
            && self.navigator.selected().is_none()
            && self.navigator.visible_rows() == 0
            && self.filter_query.is_none()
        {
            return false;
        }

        self.suggestions.clear();
        self.navigator.set_item_count(0);
        self.navigator.set_visible_rows(0);
        self.filter_query = None;
        true
    }

    fn replace_suggestions(&mut self, new_suggestions: Vec<SlashPaletteSuggestion>) -> bool {
        if self.suggestions == new_suggestions {
            return false;
        }

        self.suggestions = new_suggestions;
        self.navigator.set_item_count(self.suggestions.len());
        true
    }

    fn suggestions_for(&self, prefix: &str) -> Vec<SlashCommandItem> {
        struct ScoredCommand<'a> {
            command: &'a SlashCommandItem,
            name_match: bool,
            name_prefix: bool,
            name_pos: usize,
            description_pos: usize,
            name_score: u32,
            description_score: u32,
        }

        let normalized_query = normalize_query(prefix);
        if normalized_query.is_empty() {
            return self.commands.clone();
        }

        let mut prefix_matches: Vec<&SlashCommandItem> = self
            .commands
            .iter()
            .filter(|info| info.name.starts_with(normalized_query.as_str()))
            .collect();
        if !prefix_matches.is_empty() {
            prefix_matches.sort_by(|a, b| a.name.cmp(&b.name));
            return prefix_matches.into_iter().cloned().collect();
        }

        let mut scored: Vec<ScoredCommand<'_>> = self
            .commands
            .iter()
            .filter_map(|info| {
                let name_score = fuzzy_score(&normalized_query, info.name.as_str());
                let description_score = fuzzy_score(&normalized_query, info.description.as_str());
                if name_score.is_none() && description_score.is_none() {
                    return None;
                }

                let name_lower = info.name.to_ascii_lowercase();
                let description_lower = info.description.to_ascii_lowercase();

                Some(ScoredCommand {
                    command: info,
                    name_match: name_score.is_some(),
                    name_prefix: name_lower.starts_with(normalized_query.as_str()),
                    name_pos: name_lower
                        .find(normalized_query.as_str())
                        .unwrap_or(usize::MAX),
                    description_pos: description_lower
                        .find(normalized_query.as_str())
                        .unwrap_or(usize::MAX),
                    name_score: name_score.unwrap_or(0),
                    description_score: description_score.unwrap_or(0),
                })
            })
            .collect();

        if scored.is_empty() {
            return Vec::new();
        }

        scored.sort_by(|left, right| {
            (
                !left.name_match,
                !left.name_prefix,
                left.name_pos == usize::MAX,
                std::cmp::Reverse(left.name_score),
                left.name_pos,
                left.description_pos == usize::MAX,
                std::cmp::Reverse(left.description_score),
                left.description_pos,
                left.command.name.len(),
                left.command.name.as_str(),
            )
                .cmp(&(
                    !right.name_match,
                    !right.name_prefix,
                    right.name_pos == usize::MAX,
                    std::cmp::Reverse(right.name_score),
                    right.name_pos,
                    right.description_pos == usize::MAX,
                    std::cmp::Reverse(right.description_score),
                    right.description_pos,
                    right.command.name.len(),
                    right.command.name.as_str(),
                ))
        });

        scored
            .into_iter()
            .map(|info| info.command.clone())
            .collect()
    }

    fn ensure_selection(&mut self) -> bool {
        if self.suggestions.is_empty() {
            if self.navigator.selected().is_some() {
                self.navigator.set_item_count(0);
                return true;
            }
            return false;
        }

        let previous = self.navigator.selected();
        self.navigator.set_item_count(self.suggestions.len());

        if self.navigator.selected().is_none() {
            return self.navigator.select_first();
        }

        self.navigator.selected() != previous
    }

    #[cfg(test)]
    fn highlight_name_segments_static(&self, name: &str) -> Vec<SlashPaletteHighlightSegment> {
        let Some(query) = self.filter_query.as_ref().filter(|query| !query.is_empty()) else {
            return vec![SlashPaletteHighlightSegment::plain(name.to_owned())];
        };

        // For static commands, only use the part after the prompt invocation prefix if applicable
        let lowercase = name.to_ascii_lowercase();
        if !lowercase.starts_with(query) {
            return vec![SlashPaletteHighlightSegment::plain(name.to_owned())];
        }

        let query_len = query.chars().count();
        let mut highlighted = String::new();
        let mut remainder = String::new();

        for (index, ch) in name.chars().enumerate() {
            if index < query_len {
                highlighted.push(ch);
            } else {
                remainder.push(ch);
            }
        }

        let mut segments = Vec::new();
        if !highlighted.is_empty() {
            segments.push(SlashPaletteHighlightSegment::highlighted(highlighted));
        }
        if !remainder.is_empty() {
            segments.push(SlashPaletteHighlightSegment::plain(remainder));
        }
        if segments.is_empty() {
            segments.push(SlashPaletteHighlightSegment::plain(String::new()));
        }
        segments
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_commands() -> Vec<SlashCommandItem> {
        vec![
            SlashCommandItem::new("command", "Run a terminal command"),
            SlashCommandItem::new("config", "Show effective configuration"),
            SlashCommandItem::new("clear", "Clear screen"),
            SlashCommandItem::new("new", "Start new session"),
            SlashCommandItem::new("status", "Show status"),
            SlashCommandItem::new("help", "Show help"),
            SlashCommandItem::new("theme", "Switch theme"),
            SlashCommandItem::new("mode", "Switch mode"),
        ]
    }

    fn palette_with_commands() -> SlashPalette {
        let mut palette = SlashPalette::with_commands(test_commands());
        let _ = palette.update(Some(""));
        palette
    }

    #[test]
    fn update_applies_prefix_and_highlights_matches() {
        let mut palette = SlashPalette::with_commands(test_commands());

        let update = palette.update(Some("co"));
        assert!(matches!(
            update,
            SlashPaletteUpdate::Changed {
                suggestions_changed: true,
                selection_changed: true
            }
        ));

        let items = palette.items();
        assert!(!items.is_empty());
        let command = items
            .into_iter()
            .find(|item| {
                item.command
                    .as_ref()
                    .is_some_and(|cmd| cmd.name == "command")
            })
            .expect("command suggestion available");

        assert_eq!(command.name_segments.len(), 2);
        assert!(command.name_segments[0].highlighted);
        assert_eq!(command.name_segments[0].content, "co");
        assert_eq!(command.name_segments[1].content, "mmand");
    }

    #[test]
    fn update_matches_fuzzy_command_name() {
        let mut palette = SlashPalette::with_commands(test_commands());

        let update = palette.update(Some("sts"));
        assert!(matches!(update, SlashPaletteUpdate::Changed { .. }));

        let names: Vec<String> = palette
            .items()
            .into_iter()
            .filter_map(|item| item.command.map(|command| command.name))
            .collect();

        assert_eq!(names.first().map(String::as_str), Some("status"));
    }

    #[test]
    fn update_matches_command_description() {
        let mut palette = SlashPalette::with_commands(test_commands());

        let update = palette.update(Some("terminal"));
        assert!(matches!(update, SlashPaletteUpdate::Changed { .. }));

        let names: Vec<String> = palette
            .items()
            .into_iter()
            .filter_map(|item| item.command.map(|command| command.name))
            .collect();

        assert_eq!(names.first().map(String::as_str), Some("command"));
    }

    #[test]
    fn update_without_matches_resets_highlights() {
        let mut palette = SlashPalette::with_commands(test_commands());
        let _ = palette.update(Some("co"));
        assert!(!palette.items().is_empty());

        let update = palette.update(Some("zzz"));
        assert!(matches!(update, SlashPaletteUpdate::Changed { .. }));
        assert!(palette.items().is_empty());

        for item in palette.items() {
            assert!(
                item.name_segments
                    .iter()
                    .all(|segment| !segment.highlighted)
            );
        }
    }

    #[test]
    fn navigation_wraps_between_items() {
        let mut palette = palette_with_commands();

        assert!(palette.move_down());
        let first = palette.selected_index();
        assert_eq!(first, Some(1));

        let steps = palette.suggestions.len().saturating_sub(1);
        for _ in 0..steps {
            assert!(palette.move_down());
        }
        assert_eq!(palette.selected_index(), Some(0));

        assert!(palette.move_up());
        assert_eq!(
            palette.selected_index(),
            Some(palette.suggestions.len() - 1)
        );
    }

    #[test]
    fn boundary_shortcuts_jump_to_expected_items() {
        let mut palette = palette_with_commands();

        assert!(palette.select_last());
        assert_eq!(
            palette.selected_index(),
            Some(palette.suggestions.len() - 1)
        );

        assert!(palette.select_first());
        assert_eq!(palette.selected_index(), Some(0));
    }

    #[test]
    fn page_navigation_advances_by_visible_rows() {
        let mut palette = palette_with_commands();
        palette.set_visible_rows(3);

        assert!(palette.page_down());
        assert_eq!(palette.selected_index(), Some(3));

        assert!(palette.page_down());
        assert_eq!(palette.selected_index(), Some(6));

        assert!(palette.page_up());
        assert_eq!(palette.selected_index(), Some(3));

        assert!(palette.page_up());
        assert_eq!(palette.selected_index(), Some(0));
    }

    #[test]
    fn clear_resets_state() {
        let mut palette = SlashPalette::with_commands(test_commands());
        let _ = palette.update(Some("co"));
        palette.set_visible_rows(3);

        assert!(palette.clear());
        assert!(palette.suggestions().is_empty());
        assert_eq!(palette.selected_index(), None);
        assert_eq!(palette.visible_rows(), 0);
    }

    #[test]
    fn update_keeps_all_filtered_matches_scrollable() {
        let commands = (0..60)
            .map(|index| {
                SlashCommandItem::new(
                    format!("command-{index:02}"),
                    "Run a terminal command".to_string(),
                )
            })
            .collect();
        let mut palette = SlashPalette::with_commands(commands);

        let update = palette.update(Some("command"));
        assert!(matches!(update, SlashPaletteUpdate::Changed { .. }));
        assert_eq!(palette.suggestions().len(), 60);

        palette.set_visible_rows(10);
        for _ in 0..5 {
            assert!(palette.page_down());
        }

        assert_eq!(palette.selected_index(), Some(50));
        assert_eq!(
            palette
                .selected_command()
                .map(|command| command.name.as_str()),
            Some("command-50")
        );
    }

    #[test]
    fn command_range_tracks_latest_slash_before_cursor() {
        let input = "/one two /three";
        let cursor = input.len();
        let range = command_range(input, cursor).expect("range available");
        assert_eq!(range.start, 9);
        assert_eq!(range.end, input.len());
    }

    #[test]
    fn command_range_stops_at_whitespace() {
        let input = "/cmd arg";
        let cursor = input.len();
        // Previous behavior: returned Some(0..4) (last range)
        // New behavior: returns None (active range interrupted by space)
        assert!(command_range(input, cursor).is_none());
    }

    #[test]
    fn command_prefix_includes_partial_match() {
        let input = "/hel";
        let prefix = command_prefix(input, input.len()).expect("prefix available");
        assert_eq!(prefix, "hel");
    }

    #[test]
    fn command_prefix_is_empty_when_cursor_immediately_after_slash() {
        let input = "/";
        let prefix = command_prefix(input, 1).expect("prefix available");
        assert!(prefix.is_empty());
    }

    #[test]
    fn command_prefix_returns_none_when_not_in_command() {
        let input = "say hello";
        assert!(command_prefix(input, input.len()).is_none());
    }
}