use ratatui::text::Line;
use std::{
borrow::Cow,
fmt::{Debug, Display},
};
use crate::{AsAny, DisplayContext, ItemPreview, PreviewContext};
pub trait SkimItem: AsAny + Send + Sync + 'static {
fn text(&self) -> Cow<'_, str>;
fn display<'a>(&'a self, context: DisplayContext) -> Line<'a> {
context.to_line(self.text())
}
fn preview(&self, _context: PreviewContext) -> ItemPreview {
ItemPreview::Global
}
fn output(&self) -> Cow<'_, str> {
self.text()
}
fn get_matching_ranges(&self) -> Option<&[(usize, usize)]> {
None
}
}
impl<T: AsRef<str> + Send + Sync + 'static> SkimItem for T {
fn text(&self) -> Cow<'_, str> {
Cow::Borrowed(self.as_ref())
}
}
impl Display for dyn SkimItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.text())
}
}
impl Debug for dyn SkimItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("SkimItem {{ text: {} }}", self.text(),))
}
}