mirror of
https://github.com/lotabout/skim.git
synced 2026-09-10 07:16:23 -04:00
* feat!: send & receive items in batches
* fix: add timeout
* Revert "fix: matcher race condition at startup"
This reverts commit 8e25217a01.
* fix: correctly init matcher_control as stopped
53 lines
1.3 KiB
Rust
53 lines
1.3 KiB
Rust
extern crate skim;
|
|
use reader::CommandCollector;
|
|
use skim::prelude::*;
|
|
|
|
struct BasicSkimItem {
|
|
value: String,
|
|
}
|
|
|
|
impl SkimItem for BasicSkimItem {
|
|
fn text(&self) -> Cow<'_, str> {
|
|
Cow::Borrowed(&self.value)
|
|
}
|
|
}
|
|
|
|
struct BasicCmdCollector {
|
|
pub items: Vec<String>,
|
|
}
|
|
|
|
impl CommandCollector for BasicCmdCollector {
|
|
fn invoke(&mut self, _cmd: &str, _components_to_stop: Arc<AtomicUsize>) -> (SkimItemReceiver, Sender<i32>) {
|
|
let (tx, rx) = unbounded();
|
|
let (tx_interrupt, _rx_interrupt) = unbounded();
|
|
let mut batch = Vec::new();
|
|
while let Some(value) = self.items.pop() {
|
|
let item = BasicSkimItem { value };
|
|
batch.push(Arc::from(item) as Arc<dyn SkimItem>);
|
|
}
|
|
if !batch.is_empty() {
|
|
tx.send(batch).unwrap();
|
|
}
|
|
|
|
(rx, tx_interrupt)
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let cmd_collector = BasicCmdCollector {
|
|
items: vec![String::from("foo"), String::from("bar"), String::from("baz")],
|
|
};
|
|
let options = SkimOptionsBuilder::default()
|
|
.cmd_collector(Rc::from(RefCell::from(cmd_collector)))
|
|
.build()
|
|
.unwrap();
|
|
|
|
let selected_items = Skim::run_with(options, None)
|
|
.map(|out| out.selected_items)
|
|
.unwrap_or_default();
|
|
|
|
for item in selected_items.iter() {
|
|
println!("{}", item.output());
|
|
}
|
|
}
|