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
//! Fuzzy select state — dep-free filter + ranking core.
//!
//! Demonstrates:
//! - `FuzzySelectState::new(items)` — initial state, all items shown.
//! - `state.set_filter("...")` — case-insensitive substring filter; resets
//! highlight to 0 and re-ranks results.
//! - `state.filtered()` — indices into the original item slice.
//! - `state.selection()` — currently highlighted item.
//! - `state.move_down()` / `state.move_up()` — arrow-key navigation.
//!
//! Note: `--features tty-select` enables `FuzzySelect::ask()`, which adds
//! an interactive arrow-key driven TUI using crossterm raw mode.
//!
//! Run with: cargo run --example fuzzy_select
use gilt::fuzzy_select::FuzzySelectState;
fn main() {
let words = vec![
"apple",
"avocado",
"banana",
"blueberry",
"cherry",
"grape",
"grapefruit",
"lemon",
"lime",
"mango",
"papaya",
"peach",
"pear",
"pineapple",
"plum",
"raspberry",
"strawberry",
"watermelon",
];
let mut state = FuzzySelectState::new(words.clone());
println!("=== No filter (all {} items) ===", words.len());
for &idx in state.filtered() {
println!(" {}", words[idx]);
}
// --- Set a filter --------------------------------------------------------
state.set_filter("ap");
println!(
"\n=== Filter = \"ap\" ({} matches) ===",
state.filtered().len()
);
for &idx in state.filtered() {
let marker = if state.selection() == Some(&words[idx]) {
" ◀ (selected)"
} else {
""
};
println!(" {}{}", words[idx], marker);
}
// --- Navigate with arrow keys (logical) ----------------------------------
state.move_down();
println!("\nAfter move_down → selection = {:?}", state.selection());
state.move_down();
println!("After move_down → selection = {:?}", state.selection());
state.move_up();
println!("After move_up → selection = {:?}", state.selection());
// --- Exhaustive filter ---------------------------------------------------
state.set_filter("zzz_no_match");
println!(
"\n=== Filter = \"zzz_no_match\" → {} matches ===",
state.filtered().len()
);
assert_eq!(state.filtered().len(), 0);
assert_eq!(state.selection(), None, "empty list → no selection");
// --- Reset to empty filter -----------------------------------------------
state.set_filter("");
println!(
"\n=== Filter cleared → {} items (all back) ===",
state.filtered().len()
);
assert_eq!(state.filtered().len(), words.len());
println!("\n[assertions passed]");
println!("[tip] Add --features tty-select for the interactive FuzzySelect::ask() driver.");
}