use std::path::{Path, PathBuf}; use crossterm::event::Event; use jocalsend::error::Result; use ratatui::widgets::WidgetRef; use ratatui_explorer::FileExplorer; use simsearch::{SearchOptions, SimSearch}; use tui_input::Input; #[derive(Clone)] pub(crate) struct FileFinder { pub explorer: FileExplorer, pub fuzzy: SimSearch, pub working_dir: Option, pub input: Input, } impl FileFinder { pub fn new() -> Result { let fuzzy = SimSearch::new_with( SearchOptions::new() .stop_words(vec![std::path::MAIN_SEPARATOR_STR.to_string()]) .stop_whitespace(false) .threshold(0.0), ); Ok(Self { explorer: FileExplorer::new()?, fuzzy, working_dir: None, input: Default::default(), }) } pub fn handle(&mut self, event: &Event) -> Result<()> { self.index(); Ok(self.explorer.handle(event)?) } pub fn cwd(&self) -> &Path { self.explorer.cwd() } pub fn set_cwd(&mut self, cwd: &Path) -> Result<()> { self.explorer.set_cwd(cwd)?; self.index(); Ok(()) } pub fn widget(&self) -> impl WidgetRef { self.explorer.widget() } pub fn reset_fuzzy(&mut self) { self.fuzzy.clear(); self.input.reset(); } pub fn index(&mut self) { if let Some(owd) = self.working_dir.as_ref() && owd == self.cwd() { return; } self.working_dir = Some(self.cwd().to_path_buf()); self.reset_fuzzy(); for (i, f) in self.explorer.files().iter().enumerate() { self.fuzzy.insert(i, f.name()); } } }