tuika 0.7.0

The application framework for Rust terminal UIs — flexbox layout, overlays, focus, keymap, components, and safe ratatui interoperability.
Documentation
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
//! Selectable list (Pi's `SelectList`).
//!
//! [`SelectState`] persists an optional highlighted index and handles
//! up/down/wrap navigation; [`SelectList`] renders the options, marking the
//! current one with a theme-default or instance selection style and a caret.
//! Enter is surfaced as [`InputOutcome::Submitted`] so the caller decides what
//! "confirm" means.

use ratatui_core::layout::Rect;
use ratatui_core::style::Style;
use ratatui_core::text::Line;

use crate::event::{Event, InputOutcome, KeyCode, MouseButton, MouseKind};
use crate::geometry::Size;
use crate::surface::Surface;
use crate::view::{RenderCtx, View};

use super::{Scrollbar, VirtualWindow};

/// Optional navigation bindings for [`SelectState`].
///
/// [`Default`] preserves the original arrow/Enter/Escape behavior. Use
/// [`common`](Self::common) for the aliases commonly expected by terminal
/// pickers, then disable individual groups as needed.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SelectNavigation {
    /// Enable `j` and `k` as Down and Up.
    pub vim: bool,
    /// Enable Ctrl+N and Ctrl+P as Down and Up.
    pub ctrl_n_p: bool,
    /// Enable Tab and Shift+Tab as Down and Up.
    pub tab: bool,
    /// Enable `1` through `9` as direct activation shortcuts.
    pub numeric: bool,
}

impl SelectNavigation {
    /// Enable all common terminal-picker aliases.
    pub const fn common() -> Self {
        Self {
            vim: true,
            ctrl_n_p: true,
            tab: true,
            numeric: true,
        }
    }
}

/// Persisted optional selection index for one list.
#[derive(Clone, Copy, Debug)]
pub struct SelectState {
    selected: Option<usize>,
}

impl Default for SelectState {
    fn default() -> Self {
        Self::new()
    }
}

impl SelectState {
    /// A fresh state with the first row highlighted.
    pub fn new() -> Self {
        Self { selected: Some(0) }
    }

    /// A fresh state with no highlighted row.
    pub fn unselected() -> Self {
        Self { selected: None }
    }

    /// The currently highlighted index, or `None` when no row is selected.
    pub fn selected(&self) -> Option<usize> {
        self.selected
    }

    /// Set or clear the highlighted index directly. Lets a host drive the
    /// selection from its own optional state.
    pub fn select(&mut self, index: Option<usize>) {
        self.selected = index;
    }

    /// Keep the index in range as the list length changes.
    pub fn clamp(&mut self, len: usize) {
        if len == 0 {
            self.selected = None;
        } else if self.selected.is_some_and(|selected| selected >= len) {
            self.selected = Some(len - 1);
        }
    }

    /// Move the highlight up one row, clamping at the top (no wrap). The
    /// non-wrapping stepping primitive; use it when a picker holds at the ends
    /// rather than wrapping the way [`handle`](Self::handle) does.
    pub fn move_up(&mut self) {
        if let Some(selected) = self.selected {
            self.selected = Some(selected.saturating_sub(1));
        }
    }

    /// Move the highlight down one row, clamping at the last of `len` rows (no
    /// wrap). The non-wrapping counterpart to [`move_up`](Self::move_up).
    pub fn move_down(&mut self, len: usize) {
        if len == 0 {
            self.selected = None;
        } else if let Some(selected) = self.selected {
            self.selected = Some((selected + 1).min(len - 1));
        }
    }

    /// Navigate with arrow keys (wrapping), confirm with Enter, cancel on Esc.
    pub fn handle(&mut self, event: &Event, len: usize) -> InputOutcome {
        self.handle_with(event, len, SelectNavigation::default())
    }

    /// Handle keyboard input with a configurable navigation policy.
    pub fn handle_with(
        &mut self,
        event: &Event,
        len: usize,
        navigation: SelectNavigation,
    ) -> InputOutcome {
        if len == 0 {
            return InputOutcome::Ignored;
        }
        let Event::Key(k) = event else {
            return InputOutcome::Ignored;
        };

        if navigation.ctrl_n_p && k.ctrl && !k.alt && !k.shift {
            return match k.code {
                KeyCode::Char('n') => self.step_down(len),
                KeyCode::Char('p') => self.step_up(len),
                _ => InputOutcome::Ignored,
            };
        }
        if !k.plain() {
            return InputOutcome::Ignored;
        }
        match k.code {
            KeyCode::Up => self.step_up(len),
            KeyCode::Down => self.step_down(len),
            KeyCode::Char('k') if navigation.vim => self.step_up(len),
            KeyCode::Char('j') if navigation.vim => self.step_down(len),
            KeyCode::Tab if navigation.tab => self.step_down(len),
            KeyCode::BackTab if navigation.tab => self.step_up(len),
            KeyCode::Char(digit @ '1'..='9') if navigation.numeric => {
                let index = digit as usize - '1' as usize;
                if index < len {
                    self.selected = Some(index);
                    InputOutcome::Submitted
                } else {
                    InputOutcome::Ignored
                }
            }
            KeyCode::Enter if self.selected.is_some() => InputOutcome::Submitted,
            KeyCode::Esc => InputOutcome::Cancelled,
            _ => InputOutcome::Ignored,
        }
    }

    /// Hit-test a plain left-button press against visible list rows.
    ///
    /// `bounds` is the rendered list body and `first_visible` is the item shown
    /// on its first row. Supplying the scroll offset explicitly keeps mouse
    /// selection correct for viewported lists.
    pub fn handle_mouse(
        &mut self,
        event: &Event,
        len: usize,
        bounds: Rect,
        first_visible: usize,
    ) -> InputOutcome {
        let Event::Mouse(mouse) = event else {
            return InputOutcome::Ignored;
        };
        if !mouse.plain()
            || mouse.kind != MouseKind::Down(MouseButton::Left)
            || mouse.column < bounds.x
            || mouse.column >= bounds.right()
            || mouse.row < bounds.y
            || mouse.row >= bounds.bottom()
        {
            return InputOutcome::Ignored;
        }
        let index = first_visible.saturating_add(usize::from(mouse.row - bounds.y));
        if index >= len {
            return InputOutcome::Ignored;
        }
        self.selected = Some(index);
        InputOutcome::Submitted
    }

    fn step_up(&mut self, len: usize) -> InputOutcome {
        let before = self.selected;
        self.selected = Some(match self.selected {
            Some(selected) if selected > 0 && selected < len => selected - 1,
            _ => len - 1,
        });
        if self.selected == before {
            InputOutcome::Consumed
        } else {
            InputOutcome::Changed
        }
    }

    fn step_down(&mut self, len: usize) -> InputOutcome {
        let before = self.selected;
        self.selected = Some(match self.selected {
            Some(selected) if selected < len - 1 => selected + 1,
            _ => 0,
        });
        if self.selected == before {
            InputOutcome::Consumed
        } else {
            InputOutcome::Changed
        }
    }
}

/// Cursor and checked-item state for a multiple-selection list.
#[derive(Clone, Debug, Default)]
pub struct MultiSelectState {
    cursor: SelectState,
    selected: std::collections::BTreeSet<usize>,
}

impl MultiSelectState {
    /// Create state with the first row highlighted and no checked items.
    pub fn new() -> Self {
        Self {
            cursor: SelectState::new(),
            selected: std::collections::BTreeSet::new(),
        }
    }

    /// Cursor state used to render a [`SelectList`].
    pub fn cursor(&self) -> &SelectState {
        &self.cursor
    }

    /// Mutable cursor state for direct host control.
    pub fn cursor_mut(&mut self) -> &mut SelectState {
        &mut self.cursor
    }

    /// Whether `index` is checked.
    pub fn contains(&self, index: usize) -> bool {
        self.selected.contains(&index)
    }

    /// Checked indices in ascending order.
    pub fn selected(&self) -> impl Iterator<Item = usize> + '_ {
        self.selected.iter().copied()
    }

    /// Clear every checked item.
    pub fn clear(&mut self) {
        self.selected.clear();
    }

    /// Navigate and toggle with Enter or Space.
    pub fn handle(
        &mut self,
        event: &Event,
        len: usize,
        navigation: SelectNavigation,
    ) -> InputOutcome {
        if let Event::Key(key) = event
            && key.plain()
            && key.code == KeyCode::Char(' ')
        {
            return self.toggle_cursor(len);
        }
        match self.cursor.handle_with(event, len, navigation) {
            InputOutcome::Submitted => {
                let Some(index) = self.cursor.selected() else {
                    return InputOutcome::Ignored;
                };
                self.toggle(index);
                InputOutcome::Changed
            }
            outcome => outcome,
        }
    }

    /// Hit-test and toggle a visible row on a plain left click.
    pub fn handle_mouse(
        &mut self,
        event: &Event,
        len: usize,
        bounds: Rect,
        first_visible: usize,
    ) -> InputOutcome {
        match self.cursor.handle_mouse(event, len, bounds, first_visible) {
            InputOutcome::Submitted => {
                let Some(index) = self.cursor.selected() else {
                    return InputOutcome::Ignored;
                };
                self.toggle(index);
                InputOutcome::Changed
            }
            outcome => outcome,
        }
    }

    fn toggle_cursor(&mut self, len: usize) -> InputOutcome {
        self.cursor.clamp(len);
        let Some(index) = self.cursor.selected() else {
            return InputOutcome::Ignored;
        };
        self.toggle(index);
        InputOutcome::Changed
    }

    fn toggle(&mut self, index: usize) {
        if !self.selected.remove(&index) {
            self.selected.insert(index);
        }
    }
}

/// Renders `items` with the selected row highlighted. A state whose
/// [`selected`](SelectState::selected) value is `None` draws no caret or band.
/// With a [`viewport`] set,
/// a list taller than the viewport is windowed around the selection and a
/// scrollbar is drawn — the primitive for long pickers (hundreds of models).
///
/// [`viewport`]: SelectList::viewport
///
/// # Example
///
/// ```
/// use ratatui_core::text::Line;
/// use tuika::prelude::*;
/// use tuika::testing::{grid, render};
///
/// // A fresh state highlights the first row; the caret `›` marks it.
/// let state = SelectState::new();
/// let items = vec![Line::from("one"), Line::from("two")];
/// let view = SelectList::new(items, &state);
///
/// let buffer = render(&view, 5, 2, &Theme::default());
/// assert_eq!(grid(&buffer), "› one\n  two");
/// ```
///
/// ![select demo](https://raw.githubusercontent.com/everruns/tuika/main/docs/demos/select.gif)
pub struct SelectList {
    items: Vec<Line<'static>>,
    /// Host-provided source window, or `None` when `items` is the whole list.
    source_window: Option<VirtualWindow>,
    selected: Option<usize>,
    /// Max visible rows; `None` shows the whole list.
    viewport: Option<u16>,
    scrollbar: bool,
    selection_style: Option<Style>,
}

impl SelectList {
    /// A list of `items` with the row from `state` highlighted.
    pub fn new(items: Vec<Line<'static>>, state: &SelectState) -> Self {
        Self {
            items,
            source_window: None,
            selected: state.selected(),
            viewport: None,
            scrollbar: true,
            selection_style: None,
        }
    }

    /// Build from only the items in `window` rather than the whole collection.
    ///
    /// `items` correspond to `window.range()` in order, while `window.total()`
    /// preserves absolute selection and scrollbar geometry. This keeps frame
    /// construction and rendering O(visible rows) for host-backed collections.
    pub fn windowed(items: Vec<Line<'static>>, window: VirtualWindow, state: &SelectState) -> Self {
        Self {
            items,
            source_window: Some(window),
            selected: state.selected(),
            viewport: None,
            scrollbar: true,
            selection_style: None,
        }
    }

    /// Cap the visible rows to `rows`, windowing a longer list around the
    /// selection so the highlighted row stays on screen.
    pub fn viewport(mut self, rows: u16) -> Self {
        self.viewport = Some(rows.max(1));
        self
    }

    /// Show the overflow scrollbar (default true; only drawn when windowed).
    pub fn scrollbar(mut self, show: bool) -> Self {
        self.scrollbar = show;
        self
    }

    /// Override the selected row's style. By default the theme's selection
    /// style is used.
    pub fn selection_style(mut self, style: Style) -> Self {
        self.selection_style = Some(style);
        self
    }

    /// The visible window: the whole list unless a `viewport`
    /// smaller than the list is set, in which case a slice centered on the
    /// selection and clamped to the ends.
    fn window(&self) -> VirtualWindow {
        match self.source_window {
            Some(source) => VirtualWindow::new(
                source.total(),
                self.viewport
                    .map_or(source.len(), |rows| source.len().min(usize::from(rows))),
                source.start(),
            ),
            None => match self.viewport {
                Some(v) => VirtualWindow::around(self.items.len(), usize::from(v), self.selected),
                None => VirtualWindow::new(self.items.len(), self.items.len(), 0),
            },
        }
    }
}

impl View for SelectList {
    fn measure(&self, available: Size, _ctx: &RenderCtx) -> Size {
        let width = self
            .items
            .iter()
            .map(super::text::line_width)
            .max()
            .unwrap_or(0)
            .saturating_add(2); // caret + space
        let rows = self.window().len();
        Size::new(
            width.min(available.width),
            rows.min(u16::MAX as usize) as u16,
        )
    }

    fn render(&self, area: Rect, surface: &mut Surface, ctx: &RenderCtx) {
        let window = self.window();
        let start = window.start();
        let rows = window.len();
        let overflow = window.overflows();
        // Reserve the last column for the scrollbar when the list overflows.
        let row_width = if overflow && self.scrollbar {
            area.width.saturating_sub(1)
        } else {
            area.width
        };
        let row_right = area.x.saturating_add(row_width);
        let selection_style = self
            .selection_style
            .unwrap_or_else(|| ctx.theme.selection_style());
        let items_start = self.source_window.map_or(0, VirtualWindow::start);
        for i in 0..rows {
            let idx = start + i;
            let Some(local) = idx.checked_sub(items_start) else {
                continue;
            };
            let Some(item) = self.items.get(local) else {
                break;
            };
            let y = area.y.saturating_add(i as u16);
            if y >= area.bottom() {
                break;
            }
            let selected = self.selected == Some(idx);
            if selected {
                let mut line = surface.child(Rect::new(area.x, y, row_width, 1));
                line.fill(selection_style);
            }
            let caret = if selected { '' } else { ' ' };
            let caret_style = if selected {
                selection_style
            } else {
                ctx.theme.muted_style()
            };
            surface.set(area.x, y, caret, caret_style);
            let mut x = area.x.saturating_add(2);
            for span in &item.spans {
                if x >= row_right {
                    break;
                }
                let style = if selected {
                    item.style.patch(span.style).patch(selection_style)
                } else {
                    item.style.patch(span.style)
                };
                x = surface.set_string(x, y, span.content.as_ref(), style);
            }
        }
        if overflow && self.scrollbar && row_width < area.width {
            Scrollbar::vertical(window).render(
                Rect::new(
                    area.right() - 1,
                    area.y,
                    1,
                    area.height.min(rows.min(u16::MAX as usize) as u16),
                ),
                surface,
                ctx,
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::event::{Event, InputOutcome, Key, KeyCode};
    use crate::style::Theme;
    use crate::tests::support::{buffer, rainbow_theme, row};
    use crate::view::{RenderCtx, View};
    use crate::{Size, Surface};
    use ratatui_core::text::Line;

    #[test]
    fn select_navigation_wraps_and_confirms() {
        let mut s = SelectState::new();
        let down = Event::Key(Key::new(KeyCode::Down));
        let up = Event::Key(Key::new(KeyCode::Up));
        assert_eq!(s.handle(&up, 3), InputOutcome::Changed);
        assert_eq!(s.selected(), Some(2)); // wrapped from 0 to last
        assert_eq!(s.handle(&down, 3), InputOutcome::Changed);
        assert_eq!(s.selected(), Some(0)); // wrapped back
        let enter = Event::Key(Key::new(KeyCode::Enter));
        assert_eq!(s.handle(&enter, 3), InputOutcome::Submitted);
        let esc = Event::Key(Key::new(KeyCode::Esc));
        assert_eq!(s.handle(&esc, 3), InputOutcome::Cancelled);
    }

    #[test]
    fn common_navigation_supports_vim_ctrl_tab_and_numbers() {
        let policy = SelectNavigation::common();
        let mut state = SelectState::new();
        assert_eq!(
            state.handle_with(&Event::Key(Key::new(KeyCode::Char('j'))), 4, policy),
            InputOutcome::Changed
        );
        assert_eq!(state.selected(), Some(1));
        assert_eq!(
            state.handle_with(
                &Event::Key(Key {
                    code: KeyCode::Char('p'),
                    ctrl: true,
                    alt: false,
                    shift: false
                }),
                4,
                policy
            ),
            InputOutcome::Changed
        );
        assert_eq!(state.selected(), Some(0));
        assert_eq!(
            state.handle_with(&Event::Key(Key::new(KeyCode::BackTab)), 4, policy),
            InputOutcome::Changed
        );
        assert_eq!(state.selected(), Some(3));
        assert_eq!(
            state.handle_with(&Event::Key(Key::new(KeyCode::Char('2'))), 4, policy),
            InputOutcome::Submitted
        );
        assert_eq!(state.selected(), Some(1));
    }

    #[test]
    fn default_navigation_does_not_claim_optional_aliases() {
        let mut state = SelectState::new();
        for code in [KeyCode::Char('j'), KeyCode::Tab, KeyCode::Char('1')] {
            assert_eq!(
                state.handle_with(&Event::Key(Key::new(code)), 3, SelectNavigation::default()),
                InputOutcome::Ignored
            );
        }
        assert_eq!(state.selected(), Some(0));
    }

    #[test]
    fn mouse_hit_testing_respects_bounds_and_scroll_offset() {
        let mut state = SelectState::new();
        let bounds = Rect::new(10, 5, 20, 3);
        let click = Event::Mouse(crate::event::Mouse::at(
            MouseKind::Down(MouseButton::Left),
            12,
            6,
        ));
        assert_eq!(
            state.handle_mouse(&click, 10, bounds, 4),
            InputOutcome::Submitted
        );
        assert_eq!(state.selected(), Some(5));

        let outside = Event::Mouse(crate::event::Mouse::at(
            MouseKind::Down(MouseButton::Left),
            9,
            6,
        ));
        assert_eq!(
            state.handle_mouse(&outside, 10, bounds, 4),
            InputOutcome::Ignored
        );
    }

    #[test]
    fn multi_select_toggles_without_losing_cursor_navigation() {
        let mut state = MultiSelectState::new();
        let policy = SelectNavigation::common();
        assert_eq!(
            state.handle(&Event::Key(Key::new(KeyCode::Char(' '))), 3, policy),
            InputOutcome::Changed
        );
        assert!(state.contains(0));
        assert_eq!(
            state.handle(&Event::Key(Key::new(KeyCode::Char('j'))), 3, policy),
            InputOutcome::Changed
        );
        assert_eq!(
            state.handle(&Event::Key(Key::new(KeyCode::Enter)), 3, policy),
            InputOutcome::Changed
        );
        assert_eq!(state.selected().collect::<Vec<_>>(), vec![0, 1]);
        assert_eq!(
            state.handle(&Event::Key(Key::new(KeyCode::Char('1'))), 3, policy),
            InputOutcome::Changed
        );
        assert_eq!(state.selected().collect::<Vec<_>>(), vec![1]);
    }

    #[test]
    fn select_move_up_down_clamp_at_ends() {
        let mut s = SelectState::new();
        // Down steps forward, clamping at the last of `len` rows (no wrap).
        s.move_down(3);
        assert_eq!(s.selected(), Some(1));
        s.move_down(3);
        assert_eq!(s.selected(), Some(2));
        s.move_down(3);
        assert_eq!(s.selected(), Some(2)); // held at the bottom, not wrapped
        // Up steps back, clamping at the top.
        s.move_up();
        assert_eq!(s.selected(), Some(1));
        s.move_up();
        s.move_up();
        assert_eq!(s.selected(), Some(0)); // held at the top, not wrapped
        // Degenerate: an empty list stays at 0.
        s.move_down(0);
        assert_eq!(s.selected(), None);
    }

    #[test]
    fn select_state_select_sets_index_directly() {
        // A host can drive the highlight from its own state.
        let mut s = SelectState::new();
        s.select(Some(2));
        assert_eq!(s.selected(), Some(2));
    }

    #[test]
    fn select_state_and_list_support_no_selection() {
        assert_eq!(SelectState::default().selected(), Some(0));
        let mut state = SelectState::unselected();
        assert_eq!(state.selected(), None);
        assert_eq!(
            state.handle(&Event::Key(Key::new(KeyCode::Enter)), 2),
            InputOutcome::Ignored
        );

        let list = SelectList::new(vec![Line::from("a"), Line::from("b")], &state);
        let theme = Theme::default();
        let buf = crate::testing::render(&list, 5, 2, &theme);
        assert!((0..2).all(|y| buf[(0, y)].symbol() == " "));
        assert!((0..2).all(|y| buf[(0, y)].bg != theme.selection_bg));
    }

    #[test]
    fn select_list_accepts_an_instance_selection_style() {
        let state = SelectState::new();
        let style = Style::default().fg(ratatui_core::style::Color::Blue);
        let list = SelectList::new(vec![Line::from("a")], &state).selection_style(style);
        let buf = crate::testing::render(&list, 5, 1, &Theme::default());
        assert_eq!(buf[(0, 0)].fg, ratatui_core::style::Color::Blue);
        assert!(
            !buf[(0, 0)]
                .modifier
                .contains(ratatui_core::style::Modifier::BOLD)
        );
    }

    #[test]
    fn select_highlights_current_row() {
        let items = vec![Line::from("alpha"), Line::from("beta")];
        let mut state = SelectState::new();
        let _ = state.handle(&Event::Key(Key::new(KeyCode::Down)), 2); // select beta
        let list = SelectList::new(items, &state);
        let mut buf = buffer(10, 2);
        let theme = Theme::default();
        let ctx = RenderCtx::new(&theme);
        let area = buf.area;
        let mut surface = Surface::new(&mut buf, area);
        list.render(area, &mut surface, &ctx);
        assert!(row(&buf, 1).contains("beta"));
        // Selected row carries the selection background.
        assert_eq!(buf[(0, 1)].bg, theme.selection_bg);
        assert_eq!(buf[(0, 0)].bg, ratatui_core::style::Color::Reset);
    }

    #[test]
    fn select_viewport_windows_a_long_list_and_keeps_selection_visible() {
        // 20 items, viewport of 4: the selection must always be on screen.
        let items: Vec<Line> = (0..20).map(|i| Line::from(format!("item{i}"))).collect();
        let mut state = SelectState::new();
        state.select(Some(12));
        let theme = Theme::default();
        let ctx = RenderCtx::new(&theme);
        let list = SelectList::new(items.clone(), &state).viewport(4);
        // Windowed height is the viewport, not the full list.
        assert_eq!(list.measure(Size::new(20, 40), &ctx).height, 4);
        let rendered = crate::testing::render(&list, 20, 4, &theme);
        let text = crate::testing::grid(&rendered);
        assert!(
            text.contains("item12"),
            "selection should be visible:\n{text}"
        );
        assert!(
            !text.contains("item0\n") && !text.contains("item19"),
            "far items windowed out"
        );
        // A scrollbar occupies the last column on at least one row.
        let has_scrollbar = (0..4).any(|y| matches!(rendered[(19, y)].symbol(), "" | ""));
        assert!(
            has_scrollbar,
            "overflowing list should draw a scrollbar:\n{text}"
        );
    }

    #[test]
    fn select_viewport_shows_whole_list_when_it_fits() {
        let items: Vec<Line> = (0..3).map(|i| Line::from(format!("item{i}"))).collect();
        let state = SelectState::new();
        let list = SelectList::new(items, &state).viewport(8);
        let theme = Theme::default();
        // Fits within the viewport → no windowing, height is the item count.
        assert_eq!(
            list.measure(Size::new(20, 40), &RenderCtx::new(&theme))
                .height,
            3
        );
        let text = crate::testing::grid(&crate::testing::render(&list, 20, 3, &theme));
        assert!(text.contains("item0") && text.contains("item2"));
    }

    #[test]
    fn select_list_selection_uses_theme_slots() {
        let t = rainbow_theme();
        let mut state = SelectState::new();
        let _ = state.handle(&Event::Key(Key::new(KeyCode::Down)), 2); // select row 1
        let list = SelectList::new(vec![Line::from("a"), Line::from("b")], &state);
        let mut buf = buffer(10, 2);
        let area = buf.area;
        let ctx = RenderCtx::new(&t);
        let mut surface = Surface::new(&mut buf, area);
        list.render(area, &mut surface, &ctx);
        assert_eq!(buf[(0, 1)].bg, t.selection_bg, "selected row bg");
        assert_eq!(buf[(0, 1)].fg, t.selection_fg, "selected caret fg");
        assert_ne!(
            buf[(0, 0)].bg,
            t.selection_bg,
            "unselected row not highlighted"
        );
    }
}