saorsa-tui 0.4.0

Retained-mode, CSS-styled terminal UI framework
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
//! Keyboard-navigable list widget with selection highlighting and fuzzy filtering.
//!
//! Displays a list of items and highlights the currently selected one.
//! Supports vertical scrolling, keyboard navigation, selection
//! confirmation via Enter key, and fuzzy text filtering.

use crate::buffer::ScreenBuffer;
use crate::cell::Cell;
use crate::event::{Event, KeyCode, KeyEvent};
use crate::geometry::Rect;
use crate::segment::Segment;
use crate::style::Style;
use crate::text::truncate_to_display_width;
use fuzzy_matcher::FuzzyMatcher;
use fuzzy_matcher::skim::SkimMatcherV2;
use unicode_width::UnicodeWidthStr;

use super::{BorderStyle, EventResult, InteractiveWidget, Widget};

/// Type alias for the item render function.
type RenderFn<T> = Box<dyn Fn(&T) -> Vec<Segment>>;

/// Type alias for the selection callback.
type OnSelectFn<T> = Option<Box<dyn FnMut(&T)>>;

/// Type alias for the search text extraction function.
type SearchFn<T> = Option<Box<dyn Fn(&T) -> String>>;

/// A keyboard-navigable list widget that displays items with selection.
///
/// Each item of type `T` is rendered via a customizable render function
/// into a row of [`Segment`]s. The selected item is highlighted with a
/// distinct style. Supports optional fuzzy filtering.
pub struct SelectList<T> {
    /// List items.
    items: Vec<T>,
    /// Function to render an item as Segments.
    render_fn: RenderFn<T>,
    /// Index of the currently selected item.
    selected: usize,
    /// Scroll offset (first visible item index).
    scroll_offset: usize,
    /// Style for unselected items.
    item_style: Style,
    /// Style for the selected item.
    selected_style: Style,
    /// Border style.
    border: BorderStyle,
    /// Callback on selection confirmation (Enter pressed).
    on_select: OnSelectFn<T>,
    /// Function to extract search text from an item.
    search_fn: SearchFn<T>,
    /// Current filter query.
    filter_query: String,
    /// Filtered indices (maps display index -> items index).
    filtered_indices: Vec<usize>,
    /// Whether filtering is active.
    filter_active: bool,
}

impl<T> SelectList<T> {
    /// Create a new select list with the given items.
    ///
    /// By default, items render using a placeholder. Set a render function
    /// with [`with_render_fn`](Self::with_render_fn).
    pub fn new(items: Vec<T>) -> Self {
        let len = items.len();
        Self {
            items,
            render_fn: Box::new(|_| vec![Segment::new("???")]),
            selected: 0,
            scroll_offset: 0,
            item_style: Style::default(),
            selected_style: Style::default().reverse(true),
            border: BorderStyle::None,
            on_select: None,
            search_fn: None,
            filter_query: String::new(),
            filtered_indices: (0..len).collect(),
            filter_active: false,
        }
    }

    /// Set a custom render function for items.
    #[must_use]
    pub fn with_render_fn<F>(mut self, f: F) -> Self
    where
        F: Fn(&T) -> Vec<Segment> + 'static,
    {
        self.render_fn = Box::new(f);
        self
    }

    /// Set the style for the selected item.
    #[must_use]
    pub fn with_selected_style(mut self, style: Style) -> Self {
        self.selected_style = style;
        self
    }

    /// Set the style for unselected items.
    #[must_use]
    pub fn with_item_style(mut self, style: Style) -> Self {
        self.item_style = style;
        self
    }

    /// Set the border style.
    #[must_use]
    pub fn with_border(mut self, border: BorderStyle) -> Self {
        self.border = border;
        self
    }

    /// Set a callback invoked when Enter is pressed on the selected item.
    #[must_use]
    pub fn with_on_select<F>(mut self, f: F) -> Self
    where
        F: FnMut(&T) + 'static,
    {
        self.on_select = Some(Box::new(f));
        self
    }

    /// Set a function to extract searchable text from an item.
    ///
    /// This enables fuzzy filtering. Without a search function,
    /// filtering has no effect.
    #[must_use]
    pub fn with_search_fn<F>(mut self, f: F) -> Self
    where
        F: Fn(&T) -> String + 'static,
    {
        self.search_fn = Some(Box::new(f));
        self
    }

    /// Get a reference to the items.
    pub fn items(&self) -> &[T] {
        &self.items
    }

    /// Replace all items, resetting the selection to 0.
    pub fn set_items(&mut self, items: Vec<T>) {
        let len = items.len();
        self.items = items;
        self.selected = 0;
        self.scroll_offset = 0;
        self.filtered_indices = (0..len).collect();
        self.filter_query.clear();
        self.filter_active = false;
    }

    /// Get the currently selected index.
    ///
    /// When filtering is active, this is the index into the filtered list.
    pub fn selected(&self) -> usize {
        self.selected
    }

    /// Set the selected index (clamped to valid range).
    pub fn set_selected(&mut self, idx: usize) {
        let count = self.visible_count();
        if count == 0 {
            self.selected = 0;
        } else {
            self.selected = idx.min(count.saturating_sub(1));
        }
    }

    /// Get a reference to the currently selected item.
    ///
    /// When filtering is active, returns the selected item from the filtered list.
    pub fn selected_item(&self) -> Option<&T> {
        if self.filter_active {
            self.filtered_indices
                .get(self.selected)
                .and_then(|&idx| self.items.get(idx))
        } else {
            self.items.get(self.selected)
        }
    }

    /// Move the selection by a delta (positive = down, negative = up).
    pub fn move_selection(&mut self, delta: isize) {
        let count = self.visible_count();
        if count == 0 {
            return;
        }
        let max_idx = count.saturating_sub(1);
        if delta < 0 {
            let abs_delta = delta.unsigned_abs();
            self.selected = self.selected.saturating_sub(abs_delta);
        } else {
            self.selected = (self.selected + delta as usize).min(max_idx);
        }
    }

    /// Get the current scroll offset.
    pub fn scroll_offset(&self) -> usize {
        self.scroll_offset
    }

    /// Get the number of items (total, not filtered).
    pub fn len(&self) -> usize {
        self.items.len()
    }

    /// Check if the list is empty (total, not filtered).
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    // --- Filtering API ---

    /// Enable filter mode.
    pub fn enable_filter(&mut self) {
        self.filter_active = true;
        self.update_filter();
    }

    /// Disable filter mode and restore the full list.
    pub fn disable_filter(&mut self) {
        self.filter_active = false;
        self.filter_query.clear();
        self.filtered_indices = (0..self.items.len()).collect();
        self.selected = 0;
        self.scroll_offset = 0;
    }

    /// Get the current filter query.
    pub fn filter_query(&self) -> &str {
        &self.filter_query
    }

    /// Set the filter query and update the filtered list.
    pub fn set_filter_query(&mut self, query: &str) {
        self.filter_query = query.to_string();
        if self.filter_active {
            self.update_filter();
        }
    }

    /// Clear the filter query (shows all items if filter is active).
    pub fn clear_filter(&mut self) {
        self.filter_query.clear();
        self.filtered_indices = (0..self.items.len()).collect();
        self.selected = 0;
        self.scroll_offset = 0;
    }

    /// Check if filtering is currently active.
    pub fn is_filter_active(&self) -> bool {
        self.filter_active
    }

    /// Get references to all items that match the current filter.
    pub fn filtered_items(&self) -> Vec<&T> {
        if self.filter_active {
            self.filtered_indices
                .iter()
                .filter_map(|&idx| self.items.get(idx))
                .collect()
        } else {
            self.items.iter().collect()
        }
    }

    /// Number of currently visible items (filtered or total).
    fn visible_count(&self) -> usize {
        if self.filter_active {
            self.filtered_indices.len()
        } else {
            self.items.len()
        }
    }

    /// Get the real item index for a display row.
    fn real_index(&self, display_idx: usize) -> Option<usize> {
        if self.filter_active {
            self.filtered_indices.get(display_idx).copied()
        } else if display_idx < self.items.len() {
            Some(display_idx)
        } else {
            None
        }
    }

    /// Update filtered_indices based on current query.
    fn update_filter(&mut self) {
        if self.filter_query.is_empty() {
            self.filtered_indices = (0..self.items.len()).collect();
        } else if let Some(ref search_fn) = self.search_fn {
            let matcher = SkimMatcherV2::default();
            let mut scored: Vec<(usize, i64)> = self
                .items
                .iter()
                .enumerate()
                .filter_map(|(idx, item)| {
                    let text = search_fn(item);
                    matcher
                        .fuzzy_match(&text, &self.filter_query)
                        .map(|score| (idx, score))
                })
                .collect();
            // Sort by score descending (best match first)
            scored.sort_by(|a, b| b.1.cmp(&a.1));
            self.filtered_indices = scored.into_iter().map(|(idx, _)| idx).collect();
        } else {
            // No search function: show all items
            self.filtered_indices = (0..self.items.len()).collect();
        }

        // Reset selection
        self.selected = 0;
        self.scroll_offset = 0;
    }

    /// Ensure the selected item is visible by adjusting scroll_offset.
    fn ensure_selected_visible(&mut self, visible_height: usize) {
        if visible_height == 0 {
            return;
        }
        if self.selected < self.scroll_offset {
            self.scroll_offset = self.selected;
        }
        if self.selected >= self.scroll_offset + visible_height {
            self.scroll_offset = self
                .selected
                .saturating_sub(visible_height.saturating_sub(1));
        }
    }
}

impl<T> Widget for SelectList<T> {
    fn render(&self, area: Rect, buf: &mut ScreenBuffer) {
        if area.size.width == 0 || area.size.height == 0 {
            return;
        }

        super::border::render_border(area, self.border, self.item_style.clone(), buf);

        let inner = super::border::inner_area(area, self.border);
        if inner.size.width == 0 || inner.size.height == 0 {
            return;
        }

        let height = inner.size.height as usize;
        let width = inner.size.width as usize;
        let count = self.visible_count();

        // Clamp scroll offset
        let max_offset = count.saturating_sub(height.max(1));
        let scroll = self.scroll_offset.min(max_offset);

        let visible_end = (scroll + height).min(count);

        for (row, display_idx) in (scroll..visible_end).enumerate() {
            let y = inner.position.y + row as u16;
            if let Some(real_idx) = self.real_index(display_idx)
                && let Some(item) = self.items.get(real_idx)
            {
                let segments = (self.render_fn)(item);
                let is_selected = display_idx == self.selected;
                let style = if is_selected {
                    &self.selected_style
                } else {
                    &self.item_style
                };

                // If selected, fill the entire row with the selected style first
                if is_selected {
                    for col in 0..inner.size.width {
                        buf.set(inner.position.x + col, y, Cell::new(" ", style.clone()));
                    }
                }

                let mut col: u16 = 0;
                for segment in &segments {
                    if col as usize >= width {
                        break;
                    }
                    let remaining = width.saturating_sub(col as usize);
                    let truncated = truncate_to_display_width(&segment.text, remaining);
                    for ch in truncated.chars() {
                        let char_w = UnicodeWidthStr::width(ch.encode_utf8(&mut [0; 4]) as &str);
                        if col as usize + char_w > width {
                            break;
                        }
                        let x = inner.position.x + col;
                        buf.set(x, y, Cell::new(ch.to_string(), style.clone()));
                        col += char_w as u16;
                    }
                }
            }
        }
    }
}

impl<T> InteractiveWidget for SelectList<T> {
    fn handle_event(&mut self, event: &Event) -> EventResult {
        let Event::Key(KeyEvent { code, .. }) = event else {
            return EventResult::Ignored;
        };

        let count = self.visible_count();

        match code {
            KeyCode::Up => {
                if self.selected > 0 {
                    self.selected -= 1;
                    self.ensure_selected_visible(20);
                }
                EventResult::Consumed
            }
            KeyCode::Down => {
                if count > 0 && self.selected < count.saturating_sub(1) {
                    self.selected += 1;
                    self.ensure_selected_visible(20);
                }
                EventResult::Consumed
            }
            KeyCode::PageUp => {
                let page = 20;
                self.selected = self.selected.saturating_sub(page);
                self.ensure_selected_visible(20);
                EventResult::Consumed
            }
            KeyCode::PageDown => {
                let page = 20;
                if count > 0 {
                    self.selected = (self.selected + page).min(count.saturating_sub(1));
                    self.ensure_selected_visible(20);
                }
                EventResult::Consumed
            }
            KeyCode::Home => {
                self.selected = 0;
                self.scroll_offset = 0;
                EventResult::Consumed
            }
            KeyCode::End => {
                if count > 0 {
                    self.selected = count.saturating_sub(1);
                    self.ensure_selected_visible(20);
                }
                EventResult::Consumed
            }
            KeyCode::Enter => {
                if let Some(real_idx) = self.real_index(self.selected)
                    && let Some(item) = self.items.get(real_idx)
                    && let Some(ref mut callback) = self.on_select
                {
                    callback(item);
                }
                EventResult::Consumed
            }
            KeyCode::Char(ch) if self.filter_active => {
                self.filter_query.push(*ch);
                self.update_filter();
                EventResult::Consumed
            }
            KeyCode::Backspace if self.filter_active => {
                self.filter_query.pop();
                self.update_filter();
                EventResult::Consumed
            }
            KeyCode::Escape if self.filter_active => {
                self.disable_filter();
                EventResult::Consumed
            }
            _ => EventResult::Ignored,
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::color::Color;
    use crate::geometry::Size;

    fn make_string_list(items: Vec<&str>) -> SelectList<String> {
        let string_items: Vec<String> = items.into_iter().map(String::from).collect();
        SelectList::new(string_items).with_render_fn(|s| vec![Segment::new(s)])
    }

    fn make_searchable_list(items: Vec<&str>) -> SelectList<String> {
        make_string_list(items).with_search_fn(|s| s.clone())
    }

    // --- Core SelectList tests (Task 2) ---

    #[test]
    fn new_list_with_items() {
        let list = make_string_list(vec!["Alpha", "Beta", "Gamma"]);
        assert_eq!(list.len(), 3);
        assert!(!list.is_empty());
        assert_eq!(list.selected(), 0);
    }

    #[test]
    fn empty_list() {
        let list: SelectList<String> = SelectList::new(vec![]);
        assert!(list.is_empty());
        assert_eq!(list.len(), 0);
        assert!(list.selected_item().is_none());
    }

    #[test]
    fn selected_item_access() {
        let list = make_string_list(vec!["Alpha", "Beta", "Gamma"]);
        match list.selected_item() {
            Some(s) => assert_eq!(s, "Alpha"),
            None => unreachable!("should have selected item"),
        }
    }

    #[test]
    fn set_selected_clamps() {
        let mut list = make_string_list(vec!["Alpha", "Beta"]);
        list.set_selected(100);
        assert_eq!(list.selected(), 1);
        list.set_selected(0);
        assert_eq!(list.selected(), 0);
    }

    #[test]
    fn move_selection_positive_and_negative() {
        let mut list = make_string_list(vec!["A", "B", "C", "D"]);
        list.move_selection(2);
        assert_eq!(list.selected(), 2);
        list.move_selection(-1);
        assert_eq!(list.selected(), 1);
        list.move_selection(-100);
        assert_eq!(list.selected(), 0);
        list.move_selection(100);
        assert_eq!(list.selected(), 3);
    }

    #[test]
    fn set_items_resets_selection() {
        let mut list = make_string_list(vec!["A", "B", "C"]);
        list.set_selected(2);
        assert_eq!(list.selected(), 2);
        list.set_items(vec!["X".into(), "Y".into()]);
        assert_eq!(list.selected(), 0);
        assert_eq!(list.len(), 2);
    }

    #[test]
    fn render_empty_list() {
        let list: SelectList<String> =
            SelectList::new(vec![]).with_render_fn(|s| vec![Segment::new(s)]);
        let mut buf = ScreenBuffer::new(Size::new(20, 5));
        list.render(Rect::new(0, 0, 20, 5), &mut buf);
        assert_eq!(buf.get(0, 0).map(|c| c.grapheme.as_str()), Some(" "));
    }

    #[test]
    fn render_with_items() {
        let list = make_string_list(vec!["Hello", "World"]);
        let mut buf = ScreenBuffer::new(Size::new(10, 5));
        list.render(Rect::new(0, 0, 10, 5), &mut buf);
        assert_eq!(buf.get(0, 0).map(|c| c.grapheme.as_str()), Some("H"));
        assert_eq!(buf.get(4, 0).map(|c| c.grapheme.as_str()), Some("o"));
        assert_eq!(buf.get(0, 1).map(|c| c.grapheme.as_str()), Some("W"));
    }

    #[test]
    fn render_selected_item_highlighted() {
        let selected_style = Style::default().bold(true);
        let item_style = Style::default();
        let mut list = make_string_list(vec!["A", "B", "C"]);
        list.selected_style = selected_style.clone();
        list.item_style = item_style.clone();
        list.set_selected(1);

        let mut buf = ScreenBuffer::new(Size::new(10, 5));
        list.render(Rect::new(0, 0, 10, 5), &mut buf);

        let cell_a = buf.get(0, 0);
        assert!(cell_a.is_some());
        assert!(!cell_a.map(|c| c.style.bold).unwrap_or(true));

        let cell_b = buf.get(0, 1);
        assert!(cell_b.is_some());
        assert!(cell_b.map(|c| c.style.bold).unwrap_or(false));
    }

    #[test]
    fn keyboard_navigation_up_down() {
        let mut list = make_string_list(vec!["A", "B", "C"]);

        let down = Event::Key(KeyEvent {
            code: KeyCode::Down,
            modifiers: crate::event::Modifiers::NONE,
        });
        let up = Event::Key(KeyEvent {
            code: KeyCode::Up,
            modifiers: crate::event::Modifiers::NONE,
        });

        assert_eq!(list.handle_event(&down), EventResult::Consumed);
        assert_eq!(list.selected(), 1);
        assert_eq!(list.handle_event(&down), EventResult::Consumed);
        assert_eq!(list.selected(), 2);
        assert_eq!(list.handle_event(&down), EventResult::Consumed);
        assert_eq!(list.selected(), 2);
        assert_eq!(list.handle_event(&up), EventResult::Consumed);
        assert_eq!(list.selected(), 1);
    }

    #[test]
    fn keyboard_home_end() {
        let mut list = make_string_list(vec!["A", "B", "C", "D", "E"]);

        let end = Event::Key(KeyEvent {
            code: KeyCode::End,
            modifiers: crate::event::Modifiers::NONE,
        });
        let home = Event::Key(KeyEvent {
            code: KeyCode::Home,
            modifiers: crate::event::Modifiers::NONE,
        });

        assert_eq!(list.handle_event(&end), EventResult::Consumed);
        assert_eq!(list.selected(), 4);
        assert_eq!(list.handle_event(&home), EventResult::Consumed);
        assert_eq!(list.selected(), 0);
    }

    #[test]
    fn keyboard_page_up_down() {
        let items: Vec<String> = (0..50).map(|i| format!("Item {i}")).collect();
        let mut list = SelectList::new(items).with_render_fn(|s| vec![Segment::new(s)]);

        let page_down = Event::Key(KeyEvent {
            code: KeyCode::PageDown,
            modifiers: crate::event::Modifiers::NONE,
        });
        let page_up = Event::Key(KeyEvent {
            code: KeyCode::PageUp,
            modifiers: crate::event::Modifiers::NONE,
        });

        assert_eq!(list.handle_event(&page_down), EventResult::Consumed);
        assert_eq!(list.selected(), 20);
        assert_eq!(list.handle_event(&page_up), EventResult::Consumed);
        assert_eq!(list.selected(), 0);
    }

    #[test]
    fn enter_triggers_on_select() {
        use std::cell::RefCell;
        use std::rc::Rc;

        let selected_value = Rc::new(RefCell::new(String::new()));
        let captured = Rc::clone(&selected_value);

        let mut list =
            make_string_list(vec!["Alpha", "Beta"]).with_on_select(move |item: &String| {
                *captured.borrow_mut() = item.clone();
            });

        list.set_selected(1);

        let enter = Event::Key(KeyEvent {
            code: KeyCode::Enter,
            modifiers: crate::event::Modifiers::NONE,
        });

        assert_eq!(list.handle_event(&enter), EventResult::Consumed);
        assert_eq!(*selected_value.borrow(), "Beta");
    }

    #[test]
    fn custom_render_fn() {
        let list = SelectList::new(vec![42, 99]).with_render_fn(|n: &i32| {
            vec![Segment::styled(
                format!("Number: {n}"),
                Style::default().italic(true),
            )]
        });

        let mut buf = ScreenBuffer::new(Size::new(20, 5));
        list.render(Rect::new(0, 0, 20, 5), &mut buf);

        assert_eq!(buf.get(0, 0).map(|c| c.grapheme.as_str()), Some("N"));
        assert_eq!(buf.get(8, 0).map(|c| c.grapheme.as_str()), Some("4"));
    }

    #[test]
    fn render_with_border() {
        let list = make_string_list(vec!["Hello"]).with_border(BorderStyle::Single);
        let mut buf = ScreenBuffer::new(Size::new(12, 5));
        list.render(Rect::new(0, 0, 12, 5), &mut buf);

        assert_eq!(buf.get(0, 0).map(|c| c.grapheme.as_str()), Some("\u{250c}"));
        assert_eq!(buf.get(1, 1).map(|c| c.grapheme.as_str()), Some("H"));
    }

    #[test]
    fn utf8_wide_chars_in_items() {
        let list =
            SelectList::new(vec!["你好世界".to_string()]).with_render_fn(|s| vec![Segment::new(s)]);

        let mut buf = ScreenBuffer::new(Size::new(6, 1));
        list.render(Rect::new(0, 0, 6, 1), &mut buf);

        assert_eq!(buf.get(0, 0).map(|c| c.grapheme.as_str()), Some(""));
        assert_eq!(buf.get(2, 0).map(|c| c.grapheme.as_str()), Some(""));
        assert_eq!(buf.get(4, 0).map(|c| c.grapheme.as_str()), Some(""));
    }

    #[test]
    fn empty_list_handles_events_gracefully() {
        let mut list: SelectList<String> =
            SelectList::new(vec![]).with_render_fn(|s| vec![Segment::new(s)]);

        let down = Event::Key(KeyEvent {
            code: KeyCode::Down,
            modifiers: crate::event::Modifiers::NONE,
        });
        let enter = Event::Key(KeyEvent {
            code: KeyCode::Enter,
            modifiers: crate::event::Modifiers::NONE,
        });

        assert_eq!(list.handle_event(&down), EventResult::Consumed);
        assert_eq!(list.handle_event(&enter), EventResult::Consumed);
        assert_eq!(list.selected(), 0);
    }

    #[test]
    fn unhandled_event_returns_ignored() {
        let mut list = make_string_list(vec!["A"]);

        let tab = Event::Key(KeyEvent {
            code: KeyCode::Tab,
            modifiers: crate::event::Modifiers::NONE,
        });

        assert_eq!(list.handle_event(&tab), EventResult::Ignored);
    }

    #[test]
    fn scroll_offset_adjusted_when_selection_out_of_view() {
        let items: Vec<String> = (0..30).map(|i| format!("Item {i}")).collect();
        let mut list = SelectList::new(items).with_render_fn(|s| vec![Segment::new(s)]);

        list.set_selected(25);
        list.ensure_selected_visible(5);
        assert!(list.scroll_offset() >= 21);
    }

    #[test]
    fn builder_pattern_chaining() {
        let list = SelectList::new(vec!["A".to_string(), "B".to_string()])
            .with_render_fn(|s| vec![Segment::new(s)])
            .with_item_style(Style::default().dim(true))
            .with_selected_style(Style::default().bold(true))
            .with_border(BorderStyle::Rounded);

        assert_eq!(list.len(), 2);
        assert!(list.selected_style.bold);
        assert!(list.item_style.dim);
    }

    #[test]
    fn items_accessor() {
        let list = make_string_list(vec!["X", "Y", "Z"]);
        let items = list.items();
        assert_eq!(items.len(), 3);
        assert_eq!(items[0], "X");
        assert_eq!(items[2], "Z");
    }

    #[test]
    fn render_with_selected_style_applies_color() {
        let selected_style = Style::default().fg(Color::Named(crate::color::NamedColor::Red));
        let list = make_string_list(vec!["First", "Second"]).with_selected_style(selected_style);

        let mut buf = ScreenBuffer::new(Size::new(10, 5));
        list.render(Rect::new(0, 0, 10, 5), &mut buf);

        let cell = buf.get(0, 0);
        assert!(cell.is_some());
        match cell.map(|c| &c.style.fg) {
            Some(Some(Color::Named(crate::color::NamedColor::Red))) => {}
            other => panic!("Expected red fg, got {other:?}"),
        }
    }

    // --- Fuzzy Filtering tests (Task 3) ---

    #[test]
    fn enable_disable_filter() {
        let mut list = make_searchable_list(vec!["Alpha", "Beta", "Gamma"]);
        assert!(!list.is_filter_active());
        list.enable_filter();
        assert!(list.is_filter_active());
        list.disable_filter();
        assert!(!list.is_filter_active());
    }

    #[test]
    fn set_filter_query_updates_indices() {
        let mut list = make_searchable_list(vec!["Apple", "Banana", "Apricot", "Cherry"]);
        list.enable_filter();
        list.set_filter_query("ap");

        let filtered = list.filtered_items();
        // "Apple" and "Apricot" should match "ap"
        assert!(filtered.len() >= 2);
        assert!(filtered.iter().any(|s| s.as_str() == "Apple"));
        assert!(filtered.iter().any(|s| s.as_str() == "Apricot"));
    }

    #[test]
    fn fuzzy_matching_works() {
        let mut list = make_searchable_list(vec!["a_b_c", "axbxc", "abc", "xyz"]);
        list.enable_filter();
        list.set_filter_query("abc");

        let filtered = list.filtered_items();
        // All of "a_b_c", "axbxc", "abc" should fuzzy-match "abc"
        assert!(filtered.len() >= 3);
        assert!(filtered.iter().any(|s| s.as_str() == "abc"));
        assert!(filtered.iter().any(|s| s.as_str() == "a_b_c"));
        assert!(filtered.iter().any(|s| s.as_str() == "axbxc"));
        // "xyz" should not match
        assert!(!filtered.iter().any(|s| s.as_str() == "xyz"));
    }

    #[test]
    fn render_filtered_list_shows_only_matches() {
        let mut list = make_searchable_list(vec!["Apple", "Banana", "Apricot"]);
        list.enable_filter();
        list.set_filter_query("ap");

        let mut buf = ScreenBuffer::new(Size::new(20, 5));
        list.render(Rect::new(0, 0, 20, 5), &mut buf);

        // Row 0 should be a matching item (Apple or Apricot)
        let c0 = buf.get(0, 0).map(|c| c.grapheme.as_str());
        assert!(c0 == Some("A")); // Both start with 'A'
    }

    #[test]
    fn selected_index_operates_on_filtered_list() {
        let mut list = make_searchable_list(vec!["Apple", "Banana", "Apricot"]);
        list.enable_filter();
        list.set_filter_query("ap");

        // Selected item should be from filtered list
        let item = list.selected_item();
        assert!(item.is_some());
        let text = item.map(|s| s.as_str()).unwrap_or("");
        assert!(text == "Apple" || text == "Apricot");
    }

    #[test]
    fn navigation_on_filtered_list() {
        let mut list = make_searchable_list(vec!["Apple", "Banana", "Apricot", "Cherry"]);
        list.enable_filter();
        list.set_filter_query("ap");

        let down = Event::Key(KeyEvent {
            code: KeyCode::Down,
            modifiers: crate::event::Modifiers::NONE,
        });

        // Move down within filtered items
        list.handle_event(&down);
        assert_eq!(list.selected(), 1);
        let item = list.selected_item();
        assert!(item.is_some());
    }

    #[test]
    fn clear_filter_restores_full_list() {
        let mut list = make_searchable_list(vec!["Apple", "Banana", "Cherry"]);
        list.enable_filter();
        list.set_filter_query("ap");

        let filtered_count = list.filtered_items().len();
        assert!(filtered_count < 3);

        list.clear_filter();
        assert_eq!(list.filtered_items().len(), 3);
    }

    #[test]
    fn backspace_removes_filter_chars() {
        let mut list = make_searchable_list(vec!["Apple", "Banana", "Cherry"]);
        list.enable_filter();
        list.set_filter_query("xyz");
        assert!(list.filtered_items().is_empty());

        // Backspace via event
        let backspace = Event::Key(KeyEvent {
            code: KeyCode::Backspace,
            modifiers: crate::event::Modifiers::NONE,
        });

        list.handle_event(&backspace); // remove 'z'
        list.handle_event(&backspace); // remove 'y'
        list.handle_event(&backspace); // remove 'x'
        assert_eq!(list.filter_query(), "");
        // Empty query shows all
        assert_eq!(list.filtered_items().len(), 3);
    }

    #[test]
    fn esc_clears_and_disables_filter() {
        let mut list = make_searchable_list(vec!["Apple", "Banana"]);
        list.enable_filter();
        list.set_filter_query("ap");
        assert!(list.is_filter_active());

        let esc = Event::Key(KeyEvent {
            code: KeyCode::Escape,
            modifiers: crate::event::Modifiers::NONE,
        });

        list.handle_event(&esc);
        assert!(!list.is_filter_active());
        assert_eq!(list.filter_query(), "");
    }

    #[test]
    fn empty_query_shows_all_items() {
        let mut list = make_searchable_list(vec!["A", "B", "C"]);
        list.enable_filter();
        list.set_filter_query("");

        assert_eq!(list.filtered_items().len(), 3);
    }

    #[test]
    fn no_matches_empty_filtered_list() {
        let mut list = make_searchable_list(vec!["Apple", "Banana"]);
        list.enable_filter();
        list.set_filter_query("zzzzz");

        assert!(list.filtered_items().is_empty());
    }

    #[test]
    fn filter_with_custom_search_fn() {
        #[derive(Clone)]
        struct Item {
            name: String,
            tag: String,
        }

        let items = vec![
            Item {
                name: "Apple".into(),
                tag: "fruit".into(),
            },
            Item {
                name: "Carrot".into(),
                tag: "veggie".into(),
            },
            Item {
                name: "Banana".into(),
                tag: "fruit".into(),
            },
        ];

        let mut list = SelectList::new(items)
            .with_render_fn(|item| vec![Segment::new(&item.name)])
            .with_search_fn(|item| item.tag.clone());

        list.enable_filter();
        list.set_filter_query("fruit");

        let filtered = list.filtered_items();
        assert_eq!(filtered.len(), 2);
        assert!(filtered.iter().all(|item| item.tag == "fruit"));
    }

    #[test]
    fn char_input_triggers_filter_update() {
        let mut list = make_searchable_list(vec!["Apple", "Banana", "Apricot"]);
        list.enable_filter();

        let char_a = Event::Key(KeyEvent {
            code: KeyCode::Char('a'),
            modifiers: crate::event::Modifiers::NONE,
        });
        let char_p = Event::Key(KeyEvent {
            code: KeyCode::Char('p'),
            modifiers: crate::event::Modifiers::NONE,
        });

        assert_eq!(list.handle_event(&char_a), EventResult::Consumed);
        assert_eq!(list.filter_query(), "a");

        assert_eq!(list.handle_event(&char_p), EventResult::Consumed);
        assert_eq!(list.filter_query(), "ap");

        // Should have filtered to items containing "ap"
        let filtered = list.filtered_items();
        assert!(filtered.len() >= 2);
    }

    #[test]
    fn utf8_safe_query_input() {
        let mut list = make_searchable_list(vec!["你好", "世界", "你世"]);
        list.enable_filter();
        list.set_filter_query("");

        let filtered = list.filtered_items();
        assert!(filtered.len() >= 2);
        assert!(filtered.iter().any(|s| s.as_str() == "你好"));
        assert!(filtered.iter().any(|s| s.as_str() == "你世"));
    }

    #[test]
    fn enter_on_filtered_list_selects_correct_item() {
        use std::cell::RefCell;
        use std::rc::Rc;

        let selected_value = Rc::new(RefCell::new(String::new()));
        let captured = Rc::clone(&selected_value);

        let mut list = make_searchable_list(vec!["Apple", "Banana", "Apricot"]).with_on_select(
            move |item: &String| {
                *captured.borrow_mut() = item.clone();
            },
        );

        list.enable_filter();
        list.set_filter_query("ap");

        // Navigate to second filtered item
        let down = Event::Key(KeyEvent {
            code: KeyCode::Down,
            modifiers: crate::event::Modifiers::NONE,
        });
        list.handle_event(&down);

        let enter = Event::Key(KeyEvent {
            code: KeyCode::Enter,
            modifiers: crate::event::Modifiers::NONE,
        });
        list.handle_event(&enter);

        // Should have selected one of the "ap" matching items
        let val = selected_value.borrow().clone();
        assert!(val == "Apple" || val == "Apricot");
    }
}