diff options
| author | krolxon <krolyxon@tutanota.com> | 2024-04-26 16:54:58 +0530 |
|---|---|---|
| committer | krolxon <krolyxon@tutanota.com> | 2024-04-26 16:54:58 +0530 |
| commit | 91a7aeab42bead688909640f746fd673ee6630a5 (patch) | |
| tree | 6408e03a2c3002b53a468ba2b8649a231c84d534 /src/handler.rs | |
| parent | 04e5d2ad28af2c0b561ed4443eabddc4ee70d1f2 (diff) | |
add search functionality
Diffstat (limited to 'src/handler.rs')
| -rwxr-xr-x | src/handler.rs | 387 |
1 files changed, 229 insertions, 158 deletions
diff --git a/src/handler.rs b/src/handler.rs index ecc9e38..21743ea 100755 --- a/src/handler.rs +++ b/src/handler.rs @@ -1,203 +1,274 @@ use std::time::Duration; -use crate::app::{App, AppResult, SelectedTab}; +use crate::{ + app::{App, AppResult, SelectedTab}, + ui::InputMode, +}; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; -use rust_fuzzy_search; +use rust_fuzzy_search::{self, fuzzy_search_sorted}; use simple_dmenu::dmenu; pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { - match key_event.code { - KeyCode::Char('q') | KeyCode::Esc => app.quit(), - KeyCode::Char('c') | KeyCode::Char('C') => { - if key_event.modifiers == KeyModifiers::CONTROL { - app.quit(); - } else { - app.conn.conn.clear()?; - app.conn.update_status(); + if app.inputmode == InputMode::Editing { + // Live update + let list: Vec<&str> = app + .browser + .filetree + .iter() + .map(|(_, f)| f.as_str()) + .collect::<Vec<&str>>(); + + let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); + let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>(); + + for (i, (_, item)) in app.browser.filetree.iter().enumerate() { + if item.contains(res.get(0).unwrap()) { + app.browser.selected = i; } } - KeyCode::Char('j') | KeyCode::Down => match app.selected_tab { - SelectedTab::DirectoryBrowser => app.browser.next(), - SelectedTab::Queue => app.queue_list.next(), - SelectedTab::Playlists => app.pl_list.next(), - }, + match key_event.code { + KeyCode::Esc => { + app.inputmode = InputMode::Normal; + } + KeyCode::Char(to_insert) => { + app.enter_char(to_insert); + } + KeyCode::Enter => { + let list: Vec<&str> = app + .browser + .filetree + .iter() + .map(|(_, f)| f.as_str()) + .collect::<Vec<&str>>(); + + let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); + let (res, _) = res.get(0).unwrap(); + + for (i, (_, item)) in app.browser.filetree.iter().enumerate() { + if item.contains(res) { + app.browser.selected = i; + } + } - KeyCode::Char('k') | KeyCode::Up => match app.selected_tab { - SelectedTab::DirectoryBrowser => app.browser.prev(), - SelectedTab::Queue => app.queue_list.prev(), - SelectedTab::Playlists => app.pl_list.prev(), - }, + app.search_input.clear(); + app.inputmode = InputMode::Normal; + app.reset_cursor(); + } - KeyCode::Enter | KeyCode::Char('l') => { - // app.update_queue(); + KeyCode::Backspace => { + app.delete_char(); + } - match app.selected_tab { - SelectedTab::DirectoryBrowser => { - app.browser.handle_enter(&mut app.conn)?; + KeyCode::Left => { + app.move_cursor_left(); + } + + KeyCode::Right => { + app.move_cursor_right(); + } + + _ => {} + } + } else { + match key_event.code { + KeyCode::Char('q') | KeyCode::Esc => app.quit(), + KeyCode::Char('c') | KeyCode::Char('C') => { + if key_event.modifiers == KeyModifiers::CONTROL { + app.quit(); + } else { + app.conn.conn.clear()?; + app.conn.update_status(); } + } - SelectedTab::Queue => { - let song = app.conn.get_song_with_only_filename( - app.queue_list.list.get(app.queue_list.index).unwrap(), - ); - app.conn.push(&song)?; + KeyCode::Char('j') | KeyCode::Down => match app.selected_tab { + SelectedTab::DirectoryBrowser => app.browser.next(), + SelectedTab::Queue => app.queue_list.next(), + SelectedTab::Playlists => app.pl_list.next(), + }, + + KeyCode::Char('k') | KeyCode::Up => match app.selected_tab { + SelectedTab::DirectoryBrowser => app.browser.prev(), + SelectedTab::Queue => app.queue_list.prev(), + SelectedTab::Playlists => app.pl_list.prev(), + }, + + KeyCode::Enter | KeyCode::Char('l') => { + // app.update_queue(); + + match app.selected_tab { + SelectedTab::DirectoryBrowser => { + app.browser.handle_enter(&mut app.conn)?; + } + + SelectedTab::Queue => { + let song = app.conn.get_song_with_only_filename( + app.queue_list.list.get(app.queue_list.index).unwrap(), + ); + app.conn.push(&song)?; + } + SelectedTab::Playlists => { + app.conn + .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; + } } - SelectedTab::Playlists => { - app.conn - .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; + app.conn.update_status(); + } + + KeyCode::Char('h') => match app.selected_tab { + SelectedTab::DirectoryBrowser => { + app.browser.handle_go_back(&mut app.conn)?; } + SelectedTab::Queue => {} + SelectedTab::Playlists => {} + }, + + // Playback controls + // Toggle Pause + KeyCode::Char('p') => { + app.conn.toggle_pause(); } - app.conn.update_status(); - } - KeyCode::Char('h') => match app.selected_tab { - SelectedTab::DirectoryBrowser => { - app.browser.handle_go_back(&mut app.conn)?; + // Pause + KeyCode::Char('s') => { + app.conn.pause(); } - SelectedTab::Queue => {} - SelectedTab::Playlists => {} - }, - // Playback controls - // Toggle Pause - KeyCode::Char('p') => { - app.conn.toggle_pause(); - } + // Toggle rpeat + KeyCode::Char('r') => { + app.conn.toggle_repeat(); + app.conn.update_status(); + } - // Pause - KeyCode::Char('s') => { - app.conn.pause(); - } + // Toggle random + KeyCode::Char('z') => { + app.conn.toggle_random(); + app.conn.update_status(); + } - // Toggle rpeat - KeyCode::Char('r') => { - app.conn.toggle_repeat(); - app.conn.update_status(); - } + // Dmenu prompt + KeyCode::Char('D') => { + app.conn.play_dmenu()?; + } - // Toggle random - KeyCode::Char('z') => { - app.conn.toggle_random(); - app.conn.update_status(); - } + // add to queue + KeyCode::Char('a') => { + // let song = app.conn.get_song_with_only_filename( + // app.conn.songs_filenames.get(app.song_list.index).unwrap(), + // ); + + let list = app + .conn + .songs_filenames + .iter() + .map(|f| f.as_str()) + .collect::<Vec<&str>>(); + let (filename, _) = + rust_fuzzy_search::fuzzy_search_sorted(&app.browser.path, &list) + .get(0) + .unwrap() + .clone(); + + let song = app.conn.get_song_with_only_filename(filename); + + app.conn.conn.push(&song)?; + } - // Dmenu prompt - KeyCode::Char('D') => { - app.conn.play_dmenu()?; - } + KeyCode::Right => { + app.conn + .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; + } - // add to queue - KeyCode::Char('a') => { - // let song = app.conn.get_song_with_only_filename( - // app.conn.songs_filenames.get(app.song_list.index).unwrap(), - // ); - - let list = app - .conn - .songs_filenames - .iter() - .map(|f| f.as_str()) - .collect::<Vec<&str>>(); - let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&app.browser.path, &list) - .get(0) - .unwrap() - .clone(); - - let song = app.conn.get_song_with_only_filename(filename); - - app.conn.conn.push(&song)?; - } + KeyCode::Char('f') => { + let place = app.conn.conn.status().unwrap().song.unwrap().pos; + let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); + let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); + app.conn.conn.seek(place, pos)?; + } - KeyCode::Right => { - app.conn - .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; - } + KeyCode::Char('b') => { + let place = app.conn.conn.status().unwrap().song.unwrap().pos; + let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); + let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); + app.conn.conn.seek(place, pos)?; + } - KeyCode::Char('f') => { - let place = app.conn.conn.status().unwrap().song.unwrap().pos; - let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); - let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); - app.conn.conn.seek(place, pos)?; - } + KeyCode::Tab => { + app.cycle_tabls(); + } - KeyCode::Char('b') => { - let place = app.conn.conn.status().unwrap().song.unwrap().pos; - let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); - let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); - app.conn.conn.seek(place, pos)?; - } + KeyCode::Char('1') => { + app.selected_tab = SelectedTab::DirectoryBrowser; + } - KeyCode::Tab => { - app.cycle_tabls(); - } + KeyCode::Char('2') => { + app.selected_tab = SelectedTab::Queue; + } - KeyCode::Char('1') => { - app.selected_tab = SelectedTab::DirectoryBrowser; - } + KeyCode::Char('3') => { + app.selected_tab = SelectedTab::Playlists; + } - KeyCode::Char('2') => { - app.selected_tab = SelectedTab::Queue; - } + KeyCode::Char('>') => { + app.conn.conn.next()?; + } - KeyCode::Char('3') => { - app.selected_tab = SelectedTab::Playlists; - } + KeyCode::Char('<') => { + app.conn.conn.prev()?; + } - KeyCode::Char('>') => { - app.conn.conn.next()?; - } + // Volume controls + KeyCode::Char('=') => { + app.conn.inc_volume(2); + app.conn.update_status(); + } - KeyCode::Char('<') => { - app.conn.conn.prev()?; - } + KeyCode::Char('-') => { + app.conn.dec_volume(2); + app.conn.update_status(); + } - // Volume controls - KeyCode::Char('=') => { - app.conn.inc_volume(2); - app.conn.update_status(); - } + // Delete highlighted song from the queue + KeyCode::Char('d') => { + if app.queue_list.index >= app.queue_list.list.len() - 1 { + if app.queue_list.index != 0 { + app.queue_list.index -= 1; + } + } - KeyCode::Char('-') => { - app.conn.dec_volume(2); - app.conn.update_status(); - } + app.conn.conn.delete(app.queue_list.index as u32)?; + app.update_queue(); + } - // Delete highlighted song from the queue - KeyCode::Char('d') => { - if app.queue_list.index >= app.queue_list.list.len() - 1 { - if app.queue_list.index != 0 { - app.queue_list.index -= 1; - } + KeyCode::Char('U') => { + app.conn.conn.update()?; } - app.conn.conn.delete(app.queue_list.index as u32)?; - app.update_queue(); - } + KeyCode::Char('L') => { + let str = dmenu!(prompt "Search: "); + let list = app + .conn + .songs_filenames + .iter() + .map(|f| f.as_str()) + .collect::<Vec<&str>>(); + let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&str, &list) + .get(0) + .unwrap() + .clone(); + + let song = app.conn.get_song_with_only_filename(filename); + app.conn.push(&song)?; + } - KeyCode::Char('U') => { - app.conn.conn.update()?; - } + // Search for songs + KeyCode::Char('/') => { + app.inputmode = InputMode::toggle_editing_states(&app.inputmode); + } - KeyCode::Char('L') => { - let str = dmenu!(prompt "Search: "); - let list = app - .conn - .songs_filenames - .iter() - .map(|f| f.as_str()) - .collect::<Vec<&str>>(); - let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&str, &list) - .get(0) - .unwrap() - .clone(); - - let song = app.conn.get_song_with_only_filename(filename); - app.conn.push(&song)?; + _ => {} } - - _ => {} } - Ok(()) } |
