diff options
Diffstat (limited to 'src/skim.rs')
-rw-r--r-- | src/skim.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/skim.rs b/src/skim.rs index 7a27c5a..f35b0eb 100644 --- a/src/skim.rs +++ b/src/skim.rs @@ -42,6 +42,8 @@ impl Skimer { .unwrap_or_else(Vec::new) } + /// Call skim on its term. + /// Returns the file whose line match a pattern from current folder using ripgrep or grep. pub fn search_line_in_file(&self) -> Vec<Arc<dyn SkimItem>> { self.skim .run_internal( @@ -53,6 +55,36 @@ impl Skimer { .map(|out| out.selected_items) .unwrap_or_else(Vec::new) } + + /// Search in a text content, splitted by line. + /// Returns the selected line. + pub fn search_in_text(&self, text: String) -> Vec<Arc<dyn SkimItem>> { + let (tx_item, rx_item): (SkimItemSender, SkimItemReceiver) = unbounded(); + for line in text.lines().rev() { + let _ = tx_item.send(Arc::new(StringWrapper { + inner: line.to_string(), + })); + } + drop(tx_item); // so that skim could know when to stop waiting for more items. + self.skim + .run_internal(Some(rx_item), "".to_owned(), None, None) + .map(|out| out.selected_items) + .unwrap_or_else(Vec::new) + } +} + +struct StringWrapper { + inner: String, +} + +impl SkimItem for StringWrapper { + fn text(&self) -> Cow<str> { + Cow::Borrowed(&self.inner) + } + + fn preview(&self, _context: PreviewContext) -> ItemPreview { + ItemPreview::Text(self.inner.clone()) + } } fn pick_first_installed<'a>(commands: &'a [&'a str]) -> Option<&'a str> { @@ -64,3 +96,18 @@ fn pick_first_installed<'a>(commands: &'a [&'a str]) -> Option<&'a str> { } None } + +/// Print an ANSI escaped with corresponding colors. +pub fn print_ansi_str( + text: &str, + term: &Arc<tuikit::term::Term>, + col: Option<usize>, + row: Option<usize>, +) -> anyhow::Result<()> { + let mut col = col.unwrap_or(0); + let row = row.unwrap_or(0); + for (chr, attr) in skim::AnsiString::parse(text).iter() { + col += term.print_with_attr(row, col, &chr.to_string(), attr)?; + } + Ok(()) +} |