| 1 | use crate::{ |
| 2 | app::{App, AppResult, SelectedTab}, |
| 3 | ui::InputMode, |
| 4 | }; |
| 5 | use crossterm::event::{KeyCode, KeyEvent}; |
| 6 | use rust_fuzzy_search::{self, fuzzy_search_sorted}; |
| 7 | |
| 8 | pub fn handle_search_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()> { |
| 9 | match app.selected_tab { |
| 10 | SelectedTab::DirectoryBrowser => { |
| 11 | let list: Vec<&str> = app |
| 12 | .browser |
| 13 | .filetree |
| 14 | .iter() |
| 15 | .map(|(_, f)| f.as_str()) |
| 16 | .collect::<Vec<&str>>(); |
| 17 | |
| 18 | let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); |
| 19 | let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>(); |
| 20 | |
| 21 | for (i, (_, item)) in app.browser.filetree.iter().enumerate() { |
| 22 | if item.contains(res.first().unwrap()) { |
| 23 | app.browser.selected = i; |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | SelectedTab::Queue => { |
| 29 | let list: Vec<&str> = app |
| 30 | .queue_list |
| 31 | .list |
| 32 | .iter() |
| 33 | .map(|f| f.file.as_str()) |
| 34 | .collect::<Vec<&str>>(); |
| 35 | let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); |
| 36 | let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>(); |
| 37 | |
| 38 | for (i, item) in app.queue_list.list.iter().enumerate() { |
| 39 | if item.file.contains(res.first().unwrap()) { |
| 40 | app.queue_list.index = i; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | SelectedTab::Playlists => { |
| 46 | let list: Vec<&str> = app |
| 47 | .pl_list |
| 48 | .list |
| 49 | .iter() |
| 50 | .map(|f| f.as_str()) |
| 51 | .collect::<Vec<&str>>(); |
| 52 | let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); |
| 53 | let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>(); |
| 54 | |
| 55 | for (i, item) in app.pl_list.list.iter().enumerate() { |
| 56 | if item.contains(res.first().unwrap()) { |
| 57 | app.pl_list.index = i; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Keybind for searching |
| 64 | // |
| 65 | // Keybinds for when the search prompt is visible |
| 66 | match key_event.code { |
| 67 | KeyCode::Esc => { |
| 68 | app.inputmode = InputMode::Normal; |
| 69 | } |
| 70 | KeyCode::Char(to_insert) => { |
| 71 | app.enter_char(to_insert); |
| 72 | } |
| 73 | KeyCode::Enter => { |
| 74 | let list: Vec<&str> = app |
| 75 | .browser |
| 76 | .filetree |
| 77 | .iter() |
| 78 | .map(|(_, f)| f.as_str()) |
| 79 | .collect::<Vec<&str>>(); |
| 80 | |
| 81 | let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); |
| 82 | let (res, _) = res.first().unwrap(); |
| 83 | |
| 84 | for (i, (_, item)) in app.browser.filetree.iter().enumerate() { |
| 85 | if item.contains(res) { |
| 86 | app.browser.selected = i; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | app.search_input.clear(); |
| 91 | app.reset_cursor(); |
| 92 | app.inputmode = InputMode::Normal; |
| 93 | } |
| 94 | |
| 95 | KeyCode::Backspace => { |
| 96 | app.delete_char(); |
| 97 | } |
| 98 | |
| 99 | KeyCode::Left => { |
| 100 | app.move_cursor_left(); |
| 101 | } |
| 102 | |
| 103 | KeyCode::Right => { |
| 104 | app.move_cursor_right(); |
| 105 | } |
| 106 | |
| 107 | _ => {} |
| 108 | } |
| 109 | Ok(()) |
| 110 | } |