| @@ -1,203 +1,274 @@ |
| 1 | 1 | | use std::time::Duration; |
| 2 | 2 | | |
| 3 | | − | use crate::app::{App, AppResult, SelectedTab}; |
| 3 | + | use crate::{ |
| 4 | + | app::{App, AppResult, SelectedTab}, |
| 5 | + | ui::InputMode, |
| 6 | + | }; |
| 4 | 7 | | use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; |
| 5 | | − | use rust_fuzzy_search; |
| 8 | + | use rust_fuzzy_search::{self, fuzzy_search_sorted}; |
| 6 | 9 | | use simple_dmenu::dmenu; |
| 7 | 10 | | |
| 8 | 11 | | pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { |
| 9 | | − | match key_event.code { |
| 10 | | − | KeyCode::Char('q') | KeyCode::Esc => app.quit(), |
| 11 | | − | KeyCode::Char('c') | KeyCode::Char('C') => { |
| 12 | | − | if key_event.modifiers == KeyModifiers::CONTROL { |
| 13 | | − | app.quit(); |
| 14 | | − | } else { |
| 15 | | − | app.conn.conn.clear()?; |
| 16 | | − | app.conn.update_status(); |
| 12 | + | if app.inputmode == InputMode::Editing { |
| 13 | + | // Live update |
| 14 | + | let list: Vec<&str> = app |
| 15 | + | .browser |
| 16 | + | .filetree |
| 17 | + | .iter() |
| 18 | + | .map(|(_, f)| f.as_str()) |
| 19 | + | .collect::<Vec<&str>>(); |
| 20 | + | |
| 21 | + | let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); |
| 22 | + | let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>(); |
| 23 | + | |
| 24 | + | for (i, (_, item)) in app.browser.filetree.iter().enumerate() { |
| 25 | + | if item.contains(res.get(0).unwrap()) { |
| 26 | + | app.browser.selected = i; |
| 17 | 27 | | } |
| 18 | 28 | | } |
| 19 | 29 | | |
| 20 | | − | KeyCode::Char('j') | KeyCode::Down => match app.selected_tab { |
| 21 | | − | SelectedTab::DirectoryBrowser => app.browser.next(), |
| 22 | | − | SelectedTab::Queue => app.queue_list.next(), |
| 23 | | − | SelectedTab::Playlists => app.pl_list.next(), |
| 24 | | − | }, |
| 30 | + | match key_event.code { |
| 31 | + | KeyCode::Esc => { |
| 32 | + | app.inputmode = InputMode::Normal; |
| 33 | + | } |
| 34 | + | KeyCode::Char(to_insert) => { |
| 35 | + | app.enter_char(to_insert); |
| 36 | + | } |
| 37 | + | KeyCode::Enter => { |
| 38 | + | let list: Vec<&str> = app |
| 39 | + | .browser |
| 40 | + | .filetree |
| 41 | + | .iter() |
| 42 | + | .map(|(_, f)| f.as_str()) |
| 43 | + | .collect::<Vec<&str>>(); |
| 44 | + | |
| 45 | + | let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); |
| 46 | + | let (res, _) = res.get(0).unwrap(); |
| 47 | + | |
| 48 | + | for (i, (_, item)) in app.browser.filetree.iter().enumerate() { |
| 49 | + | if item.contains(res) { |
| 50 | + | app.browser.selected = i; |
| 51 | + | } |
| 52 | + | } |
| 25 | 53 | | |
| 26 | | − | KeyCode::Char('k') | KeyCode::Up => match app.selected_tab { |
| 27 | | − | SelectedTab::DirectoryBrowser => app.browser.prev(), |
| 28 | | − | SelectedTab::Queue => app.queue_list.prev(), |
| 29 | | − | SelectedTab::Playlists => app.pl_list.prev(), |
| 30 | | − | }, |
| 54 | + | app.search_input.clear(); |
| 55 | + | app.inputmode = InputMode::Normal; |
| 56 | + | app.reset_cursor(); |
| 57 | + | } |
| 31 | 58 | | |
| 32 | | − | KeyCode::Enter | KeyCode::Char('l') => { |
| 33 | | − | // app.update_queue(); |
| 59 | + | KeyCode::Backspace => { |
| 60 | + | app.delete_char(); |
| 61 | + | } |
| 34 | 62 | | |
| 35 | | − | match app.selected_tab { |
| 36 | | − | SelectedTab::DirectoryBrowser => { |
| 37 | | − | app.browser.handle_enter(&mut app.conn)?; |
| 63 | + | KeyCode::Left => { |
| 64 | + | app.move_cursor_left(); |
| 65 | + | } |
| 66 | + | |
| 67 | + | KeyCode::Right => { |
| 68 | + | app.move_cursor_right(); |
| 69 | + | } |
| 70 | + | |
| 71 | + | _ => {} |
| 72 | + | } |
| 73 | + | } else { |
| 74 | + | match key_event.code { |
| 75 | + | KeyCode::Char('q') | KeyCode::Esc => app.quit(), |
| 76 | + | KeyCode::Char('c') | KeyCode::Char('C') => { |
| 77 | + | if key_event.modifiers == KeyModifiers::CONTROL { |
| 78 | + | app.quit(); |
| 79 | + | } else { |
| 80 | + | app.conn.conn.clear()?; |
| 81 | + | app.conn.update_status(); |
| 38 | 82 | | } |
| 83 | + | } |
| 39 | 84 | | |
| 40 | | − | SelectedTab::Queue => { |
| 41 | | − | let song = app.conn.get_song_with_only_filename( |
| 42 | | − | app.queue_list.list.get(app.queue_list.index).unwrap(), |
| 43 | | − | ); |
| 44 | | − | app.conn.push(&song)?; |
| 85 | + | KeyCode::Char('j') | KeyCode::Down => match app.selected_tab { |
| 86 | + | SelectedTab::DirectoryBrowser => app.browser.next(), |
| 87 | + | SelectedTab::Queue => app.queue_list.next(), |
| 88 | + | SelectedTab::Playlists => app.pl_list.next(), |
| 89 | + | }, |
| 90 | + | |
| 91 | + | KeyCode::Char('k') | KeyCode::Up => match app.selected_tab { |
| 92 | + | SelectedTab::DirectoryBrowser => app.browser.prev(), |
| 93 | + | SelectedTab::Queue => app.queue_list.prev(), |
| 94 | + | SelectedTab::Playlists => app.pl_list.prev(), |
| 95 | + | }, |
| 96 | + | |
| 97 | + | KeyCode::Enter | KeyCode::Char('l') => { |
| 98 | + | // app.update_queue(); |
| 99 | + | |
| 100 | + | match app.selected_tab { |
| 101 | + | SelectedTab::DirectoryBrowser => { |
| 102 | + | app.browser.handle_enter(&mut app.conn)?; |
| 103 | + | } |
| 104 | + | |
| 105 | + | SelectedTab::Queue => { |
| 106 | + | let song = app.conn.get_song_with_only_filename( |
| 107 | + | app.queue_list.list.get(app.queue_list.index).unwrap(), |
| 108 | + | ); |
| 109 | + | app.conn.push(&song)?; |
| 110 | + | } |
| 111 | + | SelectedTab::Playlists => { |
| 112 | + | app.conn |
| 113 | + | .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 114 | + | } |
| 45 | 115 | | } |
| 46 | | − | SelectedTab::Playlists => { |
| 47 | | − | app.conn |
| 48 | | − | .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 116 | + | app.conn.update_status(); |
| 117 | + | } |
| 118 | + | |
| 119 | + | KeyCode::Char('h') => match app.selected_tab { |
| 120 | + | SelectedTab::DirectoryBrowser => { |
| 121 | + | app.browser.handle_go_back(&mut app.conn)?; |
| 49 | 122 | | } |
| 123 | + | SelectedTab::Queue => {} |
| 124 | + | SelectedTab::Playlists => {} |
| 125 | + | }, |
| 126 | + | |
| 127 | + | // Playback controls |
| 128 | + | // Toggle Pause |
| 129 | + | KeyCode::Char('p') => { |
| 130 | + | app.conn.toggle_pause(); |
| 50 | 131 | | } |
| 51 | | − | app.conn.update_status(); |
| 52 | | − | } |
| 53 | 132 | | |
| 54 | | − | KeyCode::Char('h') => match app.selected_tab { |
| 55 | | − | SelectedTab::DirectoryBrowser => { |
| 56 | | − | app.browser.handle_go_back(&mut app.conn)?; |
| 133 | + | // Pause |
| 134 | + | KeyCode::Char('s') => { |
| 135 | + | app.conn.pause(); |
| 57 | 136 | | } |
| 58 | | − | SelectedTab::Queue => {} |
| 59 | | − | SelectedTab::Playlists => {} |
| 60 | | − | }, |
| 61 | 137 | | |
| 62 | | − | // Playback controls |
| 63 | | − | // Toggle Pause |
| 64 | | − | KeyCode::Char('p') => { |
| 65 | | − | app.conn.toggle_pause(); |
| 66 | | − | } |
| 138 | + | // Toggle rpeat |
| 139 | + | KeyCode::Char('r') => { |
| 140 | + | app.conn.toggle_repeat(); |
| 141 | + | app.conn.update_status(); |
| 142 | + | } |
| 67 | 143 | | |
| 68 | | − | // Pause |
| 69 | | − | KeyCode::Char('s') => { |
| 70 | | − | app.conn.pause(); |
| 71 | | − | } |
| 144 | + | // Toggle random |
| 145 | + | KeyCode::Char('z') => { |
| 146 | + | app.conn.toggle_random(); |
| 147 | + | app.conn.update_status(); |
| 148 | + | } |
| 72 | 149 | | |
| 73 | | − | // Toggle rpeat |
| 74 | | − | KeyCode::Char('r') => { |
| 75 | | − | app.conn.toggle_repeat(); |
| 76 | | − | app.conn.update_status(); |
| 77 | | − | } |
| 150 | + | // Dmenu prompt |
| 151 | + | KeyCode::Char('D') => { |
| 152 | + | app.conn.play_dmenu()?; |
| 153 | + | } |
| 78 | 154 | | |
| 79 | | − | // Toggle random |
| 80 | | − | KeyCode::Char('z') => { |
| 81 | | − | app.conn.toggle_random(); |
| 82 | | − | app.conn.update_status(); |
| 83 | | − | } |
| 155 | + | // add to queue |
| 156 | + | KeyCode::Char('a') => { |
| 157 | + | // let song = app.conn.get_song_with_only_filename( |
| 158 | + | // app.conn.songs_filenames.get(app.song_list.index).unwrap(), |
| 159 | + | // ); |
| 160 | + | |
| 161 | + | let list = app |
| 162 | + | .conn |
| 163 | + | .songs_filenames |
| 164 | + | .iter() |
| 165 | + | .map(|f| f.as_str()) |
| 166 | + | .collect::<Vec<&str>>(); |
| 167 | + | let (filename, _) = |
| 168 | + | rust_fuzzy_search::fuzzy_search_sorted(&app.browser.path, &list) |
| 169 | + | .get(0) |
| 170 | + | .unwrap() |
| 171 | + | .clone(); |
| 172 | + | |
| 173 | + | let song = app.conn.get_song_with_only_filename(filename); |
| 174 | + | |
| 175 | + | app.conn.conn.push(&song)?; |
| 176 | + | } |
| 84 | 177 | | |
| 85 | | − | // Dmenu prompt |
| 86 | | − | KeyCode::Char('D') => { |
| 87 | | − | app.conn.play_dmenu()?; |
| 88 | | − | } |
| 178 | + | KeyCode::Right => { |
| 179 | + | app.conn |
| 180 | + | .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 181 | + | } |
| 89 | 182 | | |
| 90 | | − | // add to queue |
| 91 | | − | KeyCode::Char('a') => { |
| 92 | | − | // let song = app.conn.get_song_with_only_filename( |
| 93 | | − | // app.conn.songs_filenames.get(app.song_list.index).unwrap(), |
| 94 | | − | // ); |
| 95 | | − | |
| 96 | | − | let list = app |
| 97 | | − | .conn |
| 98 | | − | .songs_filenames |
| 99 | | − | .iter() |
| 100 | | − | .map(|f| f.as_str()) |
| 101 | | − | .collect::<Vec<&str>>(); |
| 102 | | − | let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&app.browser.path, &list) |
| 103 | | − | .get(0) |
| 104 | | − | .unwrap() |
| 105 | | − | .clone(); |
| 106 | | − | |
| 107 | | − | let song = app.conn.get_song_with_only_filename(filename); |
| 108 | | − | |
| 109 | | − | app.conn.conn.push(&song)?; |
| 110 | | − | } |
| 183 | + | KeyCode::Char('f') => { |
| 184 | + | let place = app.conn.conn.status().unwrap().song.unwrap().pos; |
| 185 | + | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 186 | + | let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); |
| 187 | + | app.conn.conn.seek(place, pos)?; |
| 188 | + | } |
| 111 | 189 | | |
| 112 | | − | KeyCode::Right => { |
| 113 | | − | app.conn |
| 114 | | − | .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 115 | | − | } |
| 190 | + | KeyCode::Char('b') => { |
| 191 | + | let place = app.conn.conn.status().unwrap().song.unwrap().pos; |
| 192 | + | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 193 | + | let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); |
| 194 | + | app.conn.conn.seek(place, pos)?; |
| 195 | + | } |
| 116 | 196 | | |
| 117 | | − | KeyCode::Char('f') => { |
| 118 | | − | let place = app.conn.conn.status().unwrap().song.unwrap().pos; |
| 119 | | − | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 120 | | − | let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); |
| 121 | | − | app.conn.conn.seek(place, pos)?; |
| 122 | | − | } |
| 197 | + | KeyCode::Tab => { |
| 198 | + | app.cycle_tabls(); |
| 199 | + | } |
| 123 | 200 | | |
| 124 | | − | KeyCode::Char('b') => { |
| 125 | | − | let place = app.conn.conn.status().unwrap().song.unwrap().pos; |
| 126 | | − | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 127 | | − | let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); |
| 128 | | − | app.conn.conn.seek(place, pos)?; |
| 129 | | − | } |
| 201 | + | KeyCode::Char('1') => { |
| 202 | + | app.selected_tab = SelectedTab::DirectoryBrowser; |
| 203 | + | } |
| 130 | 204 | | |
| 131 | | − | KeyCode::Tab => { |
| 132 | | − | app.cycle_tabls(); |
| 133 | | − | } |
| 205 | + | KeyCode::Char('2') => { |
| 206 | + | app.selected_tab = SelectedTab::Queue; |
| 207 | + | } |
| 134 | 208 | | |
| 135 | | − | KeyCode::Char('1') => { |
| 136 | | − | app.selected_tab = SelectedTab::DirectoryBrowser; |
| 137 | | − | } |
| 209 | + | KeyCode::Char('3') => { |
| 210 | + | app.selected_tab = SelectedTab::Playlists; |
| 211 | + | } |
| 138 | 212 | | |
| 139 | | − | KeyCode::Char('2') => { |
| 140 | | − | app.selected_tab = SelectedTab::Queue; |
| 141 | | − | } |
| 213 | + | KeyCode::Char('>') => { |
| 214 | + | app.conn.conn.next()?; |
| 215 | + | } |
| 142 | 216 | | |
| 143 | | − | KeyCode::Char('3') => { |
| 144 | | − | app.selected_tab = SelectedTab::Playlists; |
| 145 | | − | } |
| 217 | + | KeyCode::Char('<') => { |
| 218 | + | app.conn.conn.prev()?; |
| 219 | + | } |
| 146 | 220 | | |
| 147 | | − | KeyCode::Char('>') => { |
| 148 | | − | app.conn.conn.next()?; |
| 149 | | − | } |
| 221 | + | // Volume controls |
| 222 | + | KeyCode::Char('=') => { |
| 223 | + | app.conn.inc_volume(2); |
| 224 | + | app.conn.update_status(); |
| 225 | + | } |
| 150 | 226 | | |
| 151 | | − | KeyCode::Char('<') => { |
| 152 | | − | app.conn.conn.prev()?; |
| 153 | | − | } |
| 227 | + | KeyCode::Char('-') => { |
| 228 | + | app.conn.dec_volume(2); |
| 229 | + | app.conn.update_status(); |
| 230 | + | } |
| 154 | 231 | | |
| 155 | | − | // Volume controls |
| 156 | | − | KeyCode::Char('=') => { |
| 157 | | − | app.conn.inc_volume(2); |
| 158 | | − | app.conn.update_status(); |
| 159 | | − | } |
| 232 | + | // Delete highlighted song from the queue |
| 233 | + | KeyCode::Char('d') => { |
| 234 | + | if app.queue_list.index >= app.queue_list.list.len() - 1 { |
| 235 | + | if app.queue_list.index != 0 { |
| 236 | + | app.queue_list.index -= 1; |
| 237 | + | } |
| 238 | + | } |
| 160 | 239 | | |
| 161 | | − | KeyCode::Char('-') => { |
| 162 | | − | app.conn.dec_volume(2); |
| 163 | | − | app.conn.update_status(); |
| 164 | | − | } |
| 240 | + | app.conn.conn.delete(app.queue_list.index as u32)?; |
| 241 | + | app.update_queue(); |
| 242 | + | } |
| 165 | 243 | | |
| 166 | | − | // Delete highlighted song from the queue |
| 167 | | − | KeyCode::Char('d') => { |
| 168 | | − | if app.queue_list.index >= app.queue_list.list.len() - 1 { |
| 169 | | − | if app.queue_list.index != 0 { |
| 170 | | − | app.queue_list.index -= 1; |
| 171 | | − | } |
| 244 | + | KeyCode::Char('U') => { |
| 245 | + | app.conn.conn.update()?; |
| 172 | 246 | | } |
| 173 | 247 | | |
| 174 | | − | app.conn.conn.delete(app.queue_list.index as u32)?; |
| 175 | | − | app.update_queue(); |
| 176 | | − | } |
| 248 | + | KeyCode::Char('L') => { |
| 249 | + | let str = dmenu!(prompt "Search: "); |
| 250 | + | let list = app |
| 251 | + | .conn |
| 252 | + | .songs_filenames |
| 253 | + | .iter() |
| 254 | + | .map(|f| f.as_str()) |
| 255 | + | .collect::<Vec<&str>>(); |
| 256 | + | let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&str, &list) |
| 257 | + | .get(0) |
| 258 | + | .unwrap() |
| 259 | + | .clone(); |
| 260 | + | |
| 261 | + | let song = app.conn.get_song_with_only_filename(filename); |
| 262 | + | app.conn.push(&song)?; |
| 263 | + | } |
| 177 | 264 | | |
| 178 | | − | KeyCode::Char('U') => { |
| 179 | | − | app.conn.conn.update()?; |
| 180 | | − | } |
| 265 | + | // Search for songs |
| 266 | + | KeyCode::Char('/') => { |
| 267 | + | app.inputmode = InputMode::toggle_editing_states(&app.inputmode); |
| 268 | + | } |
| 181 | 269 | | |
| 182 | | − | KeyCode::Char('L') => { |
| 183 | | − | let str = dmenu!(prompt "Search: "); |
| 184 | | − | let list = app |
| 185 | | − | .conn |
| 186 | | − | .songs_filenames |
| 187 | | − | .iter() |
| 188 | | − | .map(|f| f.as_str()) |
| 189 | | − | .collect::<Vec<&str>>(); |
| 190 | | − | let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&str, &list) |
| 191 | | − | .get(0) |
| 192 | | − | .unwrap() |
| 193 | | − | .clone(); |
| 194 | | − | |
| 195 | | − | let song = app.conn.get_song_with_only_filename(filename); |
| 196 | | − | app.conn.push(&song)?; |
| 270 | + | _ => {} |
| 197 | 271 | | } |
| 198 | | − | |
| 199 | | − | _ => {} |
| 200 | 272 | | } |
| 201 | | − | |
| 202 | 273 | | Ok(()) |
| 203 | 274 | | } |