[][src]Trait skim::SkimItem

pub trait SkimItem: AsAny + Send + Sync + 'static {
    fn display(&self) -> Cow<AnsiString>;
fn text(&self) -> Cow<str>; fn preview(&self) -> ItemPreview { ... }
fn output(&self) -> Cow<str> { ... }
fn get_matching_ranges(&self) -> Cow<[(usize, usize)]> { ... } }

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

Downcast Example

Normally skim will return the item back, but in Arc<dyn SkimItem>. You 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 display(&self) -> Cow<AnsiString> {
        unimplemented!()
    }

    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

fn display(&self) -> Cow<AnsiString>

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

fn text(&self) -> Cow<str>

the string to be used for matching(without color)

Loading content...

Provided methods

fn preview(&self) -> ItemPreview

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

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.

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

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

Loading content...

Implementors

impl SkimItem for ItemWrapper[src]

delegate to inner

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

Loading content...