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
//! 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.
//!
//! `--features tty-select` additionally runs the interactive `FuzzySelect::ask()`
//! picker at the end (arrow-key driven, crossterm raw mode).
//!
//! Run with:
//! cargo run --example fuzzy_select # the dep-free core
//! cargo run --example fuzzy_select --features tty-select # + interactive picker
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]");
// --- Interactive picker (only compiled with `--features tty-select`) ------
#[cfg(feature = "tty-select")]
{
use gilt::fuzzy_select::FuzzySelect;
println!(
"\n=== Interactive picker (type to filter · ↑/↓ move · Enter pick · Esc cancel) ==="
);
match FuzzySelect::new(words)
.with_prompt("Pick a fruit")
.ask()
.expect("fuzzy select failed")
{
Some(item) => println!("You picked: {item}"),
None => println!("Cancelled."),
}
}
#[cfg(not(feature = "tty-select"))]
println!(
"[tip] Re-run with --features tty-select for the interactive FuzzySelect::ask() picker."
);
}