diff options
Diffstat (limited to 'default-plugins/strider/src/state.rs')
-rw-r--r-- | default-plugins/strider/src/state.rs | 88 |
1 files changed, 87 insertions, 1 deletions
diff --git a/default-plugins/strider/src/state.rs b/default-plugins/strider/src/state.rs index 05ede0df7..7ced4932c 100644 --- a/default-plugins/strider/src/state.rs +++ b/default-plugins/strider/src/state.rs @@ -1,3 +1,4 @@ +use crate::search::SearchResult; use pretty_bytes::converter as pb; use std::{ collections::{HashMap, VecDeque}, @@ -7,7 +8,9 @@ use std::{ }; use zellij_tile::prelude::*; -const ROOT: &str = "/host"; +pub const ROOT: &str = "/host"; +pub const CURRENT_SEARCH_TERM: &str = "/data/current_search_term"; + #[derive(Default)] pub struct State { pub path: PathBuf, @@ -15,9 +18,66 @@ pub struct State { pub cursor_hist: HashMap<PathBuf, (usize, usize)>, pub hide_hidden_files: bool, pub ev_history: VecDeque<(Event, Instant)>, // stores last event, can be expanded in future + pub search_paths: Vec<String>, + pub search_term: Option<String>, + pub search_results: Vec<SearchResult>, + pub loading: bool, + pub loading_animation_offset: u8, + pub typing_search_term: bool, + pub exploring_search_results: bool, + pub selected_search_result: usize, } impl State { + pub fn append_to_search_term(&mut self, key: Key) { + match key { + Key::Char(character) => { + if let Some(search_term) = self.search_term.as_mut() { + search_term.push(character); + } + }, + Key::Backspace => { + if let Some(search_term) = self.search_term.as_mut() { + search_term.pop(); + if search_term.len() == 0 { + self.search_term = None; + self.typing_search_term = false; + } + } + }, + _ => {}, + } + } + pub fn accept_search_term(&mut self) { + self.typing_search_term = false; + self.exploring_search_results = true; + } + pub fn typing_search_term(&self) -> bool { + self.typing_search_term + } + pub fn exploring_search_results(&self) -> bool { + self.exploring_search_results + } + pub fn stop_exploring_search_results(&mut self) { + self.exploring_search_results = false; + } + pub fn start_typing_search_term(&mut self) { + if self.search_term.is_none() { + self.search_term = Some(String::new()); + } + self.typing_search_term = true; + } + pub fn stop_typing_search_term(&mut self) { + self.typing_search_term = true; + } + pub fn move_search_selection_up(&mut self) { + self.selected_search_result = self.selected_search_result.saturating_sub(1); + } + pub fn move_search_selection_down(&mut self) { + if self.selected_search_result < self.search_results.len() { + self.selected_search_result = self.selected_search_result.saturating_add(1); + } + } pub fn selected_mut(&mut self) -> &mut usize { &mut self.cursor_hist.entry(self.path.clone()).or_default().0 } @@ -44,6 +104,32 @@ impl State { } } } + pub fn open_search_result(&mut self) { + match self.search_results.get(self.selected_search_result) { + Some(SearchResult::File { + path, + score, + indices, + }) => { + let file_path = PathBuf::from(path); + open_file(file_path.strip_prefix(ROOT).unwrap()); + }, + Some(SearchResult::LineInFile { + path, + score, + indices, + line, + line_number, + }) => { + let file_path = PathBuf::from(path); + open_file_with_line(file_path.strip_prefix(ROOT).unwrap(), *line_number); + // open_file_with_line(&file_path, *line_number); // TODO: no!! + }, + None => { + eprintln!("Search result not found"); + }, + } + } } #[derive(PartialEq, Eq, PartialOrd, Ord, Clone)] |