SkimItem

Trait SkimItem 

Source
pub trait SkimItem:
    AsAny
    + Send
    + Sync
    + 'static {
    // Required method
    fn text(&self) -> Cow<'_, str>;

    // Provided methods
    fn display(&self, context: DisplayContext<'_>) -> AnsiString { ... }
    fn preview(&self, _context: PreviewContext<'_>) -> ItemPreview { ... }
    fn output(&self) -> Cow<'_, str> { ... }
    fn get_matching_ranges(&self) -> Option<&[(usize, usize)]> { ... }
}
Expand description

A SkimItem defines what’s been processed(fetched, matched, previewed and returned) by skim

§Downcast Example

Skim will return the item back, but in Arc<dyn SkimItem> form. We might want a reference to the concrete type instead of trait object. Skim provide a somehow “complicated” way to downcast it back to the reference of the original concrete type.

use skim::prelude::*;

struct MyItem {}
impl SkimItem for MyItem {
    fn text(&self) -> Cow<str> {
        unimplemented!()
    }
}

impl MyItem {
    pub fn mutable(&mut self) -> i32 {
        1
    }

    pub fn immutable(&self) -> i32 {
        0
    }
}

let mut ret: Arc<dyn SkimItem> = Arc::new(MyItem{});
let mutable: &mut MyItem = Arc::get_mut(&mut ret)
    .expect("item is referenced by others")
    .as_any_mut() // cast to Any
    .downcast_mut::<MyItem>() // downcast to (mut) concrete type
    .expect("something wrong with downcast");
assert_eq!(mutable.mutable(), 1);

let immutable: &MyItem = (*ret).as_any() // cast to Any
    .downcast_ref::<MyItem>() // downcast to concrete type
    .expect("something wrong with downcast");
assert_eq!(immutable.immutable(), 0)

Required Methods§

Source

fn text(&self) -> Cow<'_, str>

The string to be used for matching (without color)

Provided Methods§

Source

fn display(&self, context: DisplayContext<'_>) -> AnsiString

The content to be displayed on the item list, could contain ANSI properties

Source

fn preview(&self, _context: PreviewContext<'_>) -> ItemPreview

Custom preview content, default to ItemPreview::Global which will use global preview setting(i.e. the command set by preview option)

Source

fn output(&self) -> Cow<'_, str>

Get output text(after accept), default to text() Note that this function is intended to be used by the caller of skim and will not be used by skim. And since skim will return the item back in SkimOutput, if string is not what you want, you could still use downcast to retain the pointer to the original struct.

Examples found in repository?
examples/sample.rs (line 12)
4pub fn main() {
5    let options = SkimOptions::default();
6
7    let selected_items = Skim::run_with(&options, None)
8        .map(|out| out.selected_items)
9        .unwrap_or_else(Vec::new);
10
11    for item in selected_items.iter() {
12        println!("{}", item.output());
13    }
14}
More examples
Hide additional examples
examples/nth.rs (line 20)
8pub fn main() {
9    let input = "foo 123";
10
11    let options = SkimOptionsBuilder::default().query(Some("f")).build().unwrap();
12    let item_reader = SkimItemReader::new(SkimItemReaderOption::default().nth("2").build());
13
14    let (items, opt_ingest_handle) = item_reader.of_bufread(Box::new(Cursor::new(input)));
15    let selected_items = Skim::run_with(&options, Some(items))
16        .map(|out| out.selected_items)
17        .unwrap_or_else(Vec::new);
18
19    for item in selected_items.iter() {
20        println!("{}", item.output());
21    }
22
23    if let Some(handle) = opt_ingest_handle {
24        let _ = handle.join();
25    }
26}
examples/custom_item.rs (line 47)
22pub fn main() {
23    let options = SkimOptionsBuilder::default()
24        .height(Some("50%"))
25        .multi(true)
26        .preview(Some("")) // preview should be specified to enable preview window
27        .build()
28        .unwrap();
29
30    let (tx_item, rx_item): (SkimItemSender, SkimItemReceiver) = unbounded();
31    let _ = tx_item.send(vec![Arc::new(MyItem {
32        inner: "color aaaa".to_string(),
33    })]);
34    let _ = tx_item.send(vec![Arc::new(MyItem {
35        inner: "bbbb".to_string(),
36    })]);
37    let _ = tx_item.send(vec![Arc::new(MyItem {
38        inner: "ccc".to_string(),
39    })]);
40    drop(tx_item); // so that skim could know when to stop waiting for more items.
41
42    let selected_items = Skim::run_with(&options, Some(rx_item))
43        .map(|out| out.selected_items)
44        .unwrap_or_else(Vec::new);
45
46    for item in selected_items.iter() {
47        println!("{}", item.output());
48    }
49}
examples/option_builder.rs (line 22)
5pub fn main() {
6    let options = SkimOptionsBuilder::default()
7        .height(Some("50%"))
8        .multi(true)
9        .build()
10        .unwrap();
11    let item_reader = SkimItemReader::default();
12
13    //==================================================
14    // first run
15    let input = "aaaaa\nbbbb\nccc";
16    let (items, opt_ingest_handle) = item_reader.of_bufread(Box::new(Cursor::new(input)));
17    let selected_items = Skim::run_with(&options, Some(items))
18        .map(|out| out.selected_items)
19        .unwrap_or_else(Vec::new);
20
21    for item in selected_items.iter() {
22        println!("{}", item.output());
23    }
24
25    if let Some(handle) = opt_ingest_handle {
26        let _ = handle.join();
27    }
28
29    //==================================================
30    // second run
31    let input = "11111\n22222\n333333333";
32    let (items, opt_ingest_handle) = item_reader.of_bufread(Box::new(Cursor::new(input)));
33    let selected_items = Skim::run_with(&options, Some(items))
34        .map(|out| out.selected_items)
35        .unwrap_or_else(Vec::new);
36
37    for item in selected_items.iter() {
38        println!("{}", item.output());
39    }
40
41    if let Some(handle) = opt_ingest_handle {
42        let _ = handle.join();
43    }
44}
Source

fn get_matching_ranges(&self) -> Option<&[(usize, usize)]>

we could limit the matching ranges of the get_text of the item. providing (start_byte, end_byte) of the range

Implementors§

Source§

impl<T: AsRef<str> + Send + Sync + 'static> SkimItem for T