diff options
Diffstat (limited to 'src/app.rs')
| -rwxr-xr-x | src/app.rs | 77 |
1 files changed, 75 insertions, 2 deletions
@@ -1,8 +1,7 @@ -use std::time::Duration; - use crate::browser::FileBrowser; use crate::connection::Connection; use crate::list::ContentList; +use crate::ui::InputMode; use mpd::Client; // Application result type @@ -18,6 +17,11 @@ pub struct App { pub pl_list: ContentList<String>, pub selected_tab: SelectedTab, pub browser: FileBrowser, + + // Search + pub inputmode: InputMode, + pub search_input: String, + pub cursor_position: usize, } #[derive(Debug, PartialEq, Clone)] @@ -44,6 +48,9 @@ impl App { pl_list, selected_tab: SelectedTab::DirectoryBrowser, browser, + inputmode: InputMode::Normal, + search_input: String::new(), + cursor_position: 0, }) } @@ -96,4 +103,70 @@ impl App { SelectedTab::Playlists => SelectedTab::DirectoryBrowser, }; } + + pub fn search_song(&mut self) -> AppResult<()> { + let list = self + .conn + .songs_filenames + .iter() + .map(|f| f.as_str()) + .collect::<Vec<&str>>(); + let (filename, _) = + rust_fuzzy_search::fuzzy_search_sorted(self.search_input.as_str(), &list) + .get(0) + .unwrap() + .clone(); + + let song = self.conn.get_song_with_only_filename(filename); + self.conn.push(&song)?; + + Ok(()) + } + + // Cursor movements + pub fn move_cursor_left(&mut self) { + let cursor_moved_left = self.cursor_position.saturating_sub(1); + self.cursor_position = self.clamp_cursor(cursor_moved_left); + } + + pub fn move_cursor_right(&mut self) { + let cursor_moved_right = self.cursor_position.saturating_add(1); + self.cursor_position = self.clamp_cursor(cursor_moved_right); + } + + pub fn enter_char(&mut self, new_char: char) { + self.search_input.insert(self.cursor_position, new_char); + + self.move_cursor_right(); + } + + pub fn delete_char(&mut self) { + let is_not_cursor_leftmost = self.cursor_position != 0; + if is_not_cursor_leftmost { + // Method "remove" is not used on the saved text for deleting the selected char. + // Reason: Using remove on String works on bytes instead of the chars. + // Using remove would require special care because of char boundaries. + + let current_index = self.cursor_position; + let from_left_to_current_index = current_index - 1; + + // Getting all characters before the selected character. + let before_char_to_delete = self.search_input.chars().take(from_left_to_current_index); + // Getting all characters after selected character. + let after_char_to_delete = self.search_input.chars().skip(current_index); + + // Put all characters together except the selected one. + // By leaving the selected one out, it is forgotten and therefore deleted. + self.search_input = before_char_to_delete.chain(after_char_to_delete).collect(); + self.move_cursor_left(); + } + } + + pub fn clamp_cursor(&self, new_cursor_pos: usize) -> usize { + new_cursor_pos.clamp(0, self.search_input.len()) + } + + pub fn reset_cursor(&mut self) { + self.cursor_position = 0; + } } |
