Skip to content

Commit ab514a5

Browse files
LoricAndreSkim bot
andauthored
feat!: internally compute indexes at match time (removes get/set_index) (#1001)
* chore: remove skim::Item run_items wrapper * fix: properly trigger re-render on custom previews * feat: add AppendItems event * feat!: internally compute indexes at match time (removes get/set_index) * chore: generate completions & manpage * chore: better benchmarks --------- Co-authored-by: Skim bot <skim-bot@skim-rs.github.io>
1 parent 0ac1ac8 commit ab514a5

31 files changed

+513
-404
lines changed

benches/filter.rs

Lines changed: 232 additions & 155 deletions
Large diffs are not rendered by default.

benches/matcher_micro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use skim::fuzzy_matcher::frizbee::FrizbeeMatcher;
1212
use skim::prelude::SkimMatcherV2;
1313

1414
fn load_lines() -> Vec<String> {
15-
let data = fs::read_to_string("benches/fixtures/1M.txt").expect("1M.txt missing");
15+
let data = fs::read_to_string("benches/fixtures/100K.txt").expect("100K.txt missing");
1616
data.lines().map(|l| l.to_string()).collect()
1717
}
1818

examples/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() -> color_eyre::Result<()> {
55
let res = Skim::run_items(opts, ["hello", "world"])?;
66

77
for item in res.selected_items {
8-
println!("Selected {} (id {})", item.output(), item.get_index());
8+
println!("Selected {} (id {})", item.output(), item.rank.index);
99
}
1010

1111
Ok(())

examples/custom_action_keybinding.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ use std::io::Cursor;
77
/// This example demonstrates how to bind custom action callbacks to keyboard shortcuts.
88
///
99
/// It shows how to:
10-
/// 1. Create custom action callbacks
10+
/// 1. Create custom action callbacks (both sync and async)
1111
/// 2. Bind them to specific key combinations
1212
/// 3. Use them interactively in skim
1313
fn main() {
14-
// Create a custom callback that adds a prefix to the query
15-
let add_prefix_callback = ActionCallback::new(|app: &mut skim::tui::App| {
14+
// Create a synchronous callback that adds a prefix to the query.
15+
// Use `new_sync` for plain closures that do not need to await anything.
16+
let add_prefix_callback = ActionCallback::new_sync(|app: &mut skim::tui::App| {
1617
// Get current query and add prefix
1718
let current_query = app.input.value.clone();
1819

@@ -32,14 +33,17 @@ fn main() {
3233
Ok(events)
3334
});
3435

35-
// Create a callback that selects all and exits
36+
// Create an async callback that selects all and exits.
37+
// Use `new` for async closures or blocks that may await futures.
3638
let select_all_callback = ActionCallback::new(|app: &mut skim::tui::App| {
3739
let count = app.item_pool.len();
38-
39-
Ok(vec![
40-
Event::Action(Action::SelectAll),
41-
Event::Action(Action::Accept(Some(format!("Selected {count} items")))),
42-
])
40+
async move {
41+
// Async work could go here (e.g. HTTP requests, file I/O, …).
42+
Ok(vec![
43+
Event::Action(Action::SelectAll),
44+
Event::Action(Action::Accept(Some(format!("Selected {count} items")))),
45+
])
46+
}
4347
});
4448

4549
// Build basic options

examples/custom_item.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ fn main() {
2323
let options = SkimOptionsBuilder::default()
2424
.height("50%")
2525
.multi(true)
26-
.preview(String::new()) // preview should be specified to enable preview window
26+
.preview("") // preview should be specified to enable preview window
2727
.build()
2828
.unwrap();
2929

30+
env_logger::init();
31+
3032
let (tx_item, rx_item): (SkimItemSender, SkimItemReceiver) = unbounded();
3133
let _ = tx_item.send(vec![
3234
Arc::new(MyItem {

examples/downcast.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use skim::prelude::*;
77
#[derive(Debug, Clone)]
88
struct Item {
99
text: String,
10-
index: usize,
1110
}
1211

1312
impl SkimItem for Item {
@@ -18,14 +17,6 @@ impl SkimItem for Item {
1817
fn preview(&self, _context: PreviewContext) -> ItemPreview {
1918
ItemPreview::Text(self.text.to_owned())
2019
}
21-
22-
fn get_index(&self) -> usize {
23-
self.index
24-
}
25-
26-
fn set_index(&mut self, index: usize) {
27-
self.index = index
28-
}
2920
}
3021

3122
pub fn main() {
@@ -39,18 +30,9 @@ pub fn main() {
3930
let (tx, rx): (SkimItemSender, SkimItemReceiver) = unbounded();
4031

4132
tx.send(vec![
42-
Arc::new(Item {
43-
text: "a".into(),
44-
index: 0,
45-
}) as Arc<dyn SkimItem>,
46-
Arc::new(Item {
47-
text: "b".into(),
48-
index: 1,
49-
}) as Arc<dyn SkimItem>,
50-
Arc::new(Item {
51-
text: "c".into(),
52-
index: 2,
53-
}) as Arc<dyn SkimItem>,
33+
Arc::new(Item { text: "a".into() }) as Arc<dyn SkimItem>,
34+
Arc::new(Item { text: "b".into() }) as Arc<dyn SkimItem>,
35+
Arc::new(Item { text: "c".into() }) as Arc<dyn SkimItem>,
5436
])
5537
.unwrap();
5638

@@ -60,7 +42,7 @@ pub fn main() {
6042
.map(|out| out.selected_items)
6143
.unwrap_or_default()
6244
.iter()
63-
.map(|selected_item| (**selected_item).as_any().downcast_ref::<Item>().unwrap().to_owned())
45+
.map(|selected_item| selected_item.downcast_item::<Item>().unwrap().to_owned())
6446
.collect::<Vec<Item>>();
6547

6648
for item in selected_items {

shell/completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ _sk() {
258258
return 0
259259
;;
260260
--flags)
261-
COMPREPLY=($(compgen -W "no-preview-pty show-score" -- "${cur}"))
261+
COMPREPLY=($(compgen -W "no-preview-pty show-score show-index" -- "${cur}"))
262262
return 0
263263
;;
264264
--hscroll-off)

shell/completion.fish

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ complete -c sk -l tmux -d 'Run in a tmux popup' -r
8888
complete -c sk -l log-level -d 'Set the log level' -r
8989
complete -c sk -l log-file -d 'Pipe log output to a file' -r
9090
complete -c sk -l flags -d 'Feature flags' -r -f -a "no-preview-pty\t'Disable preview PTY on linux'
91-
show-score\t'Display the item\'s match score before its value in the item list (for matcher debugging)'"
91+
show-score\t'Display the item\'s match score before its value in the item list (for matcher debugging)'
92+
show-index\t'Display the item\'s index before its value in the item list'"
9293
complete -c sk -l hscroll-off -r
9394
complete -c sk -l jump-labels -r
9495
complete -c sk -l tail -r

shell/completion.nu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module completions {
3333
}
3434

3535
def "nu-complete sk flags" [] {
36-
[ "no-preview-pty" "show-score" ]
36+
[ "no-preview-pty" "show-score" "show-index" ]
3737
}
3838

3939
# Fuzzy Finder in rust!

shell/completion.zsh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ zsh\:"Zsh"))' \
8989
'--log-level=[Set the log level]:LOG_LEVEL:_default' \
9090
'--log-file=[Pipe log output to a file]:LOG_FILE:_default' \
9191
'*--flags=[Feature flags]:FLAGS:((no-preview-pty\:"Disable preview PTY on linux"
92-
show-score\:"Display the item'\''s match score before its value in the item list (for matcher debugging)"))' \
92+
show-score\:"Display the item'\''s match score before its value in the item list (for matcher debugging)"
93+
show-index\:"Display the item'\''s index before its value in the item list"))' \
9394
'--hscroll-off=[]:HSCROLL_OFF:_default' \
9495
'--jump-labels=[]:JUMP_LABELS:_default' \
9596
'--tail=[]:TAIL:_default' \

0 commit comments

Comments
 (0)