| 1 | use std::time::Duration; |
| 2 | |
| 3 | use crate::{ |
| 4 | app::{App, AppResult, SelectedTab}, |
| 5 | ui::InputMode, |
| 6 | }; |
| 7 | use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers}; |
| 8 | use rust_fuzzy_search::{self, fuzzy_search_sorted}; |
| 9 | use simple_dmenu::dmenu; |
| 10 | |
| 11 | pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { |
| 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; |
| 27 | } |
| 28 | } |
| 29 | |
| 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 | } |
| 53 | |
| 54 | app.search_input.clear(); |
| 55 | app.inputmode = InputMode::Normal; |
| 56 | app.reset_cursor(); |
| 57 | } |
| 58 | |
| 59 | KeyCode::Backspace => { |
| 60 | app.delete_char(); |
| 61 | } |
| 62 | |
| 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(); |
| 82 | app.queue_list.list.clear(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | KeyCode::Char('j') | KeyCode::Down => match app.selected_tab { |
| 87 | SelectedTab::DirectoryBrowser => app.browser.next(), |
| 88 | SelectedTab::Queue => app.queue_list.next(), |
| 89 | SelectedTab::Playlists => app.pl_list.next(), |
| 90 | }, |
| 91 | |
| 92 | KeyCode::Char('k') | KeyCode::Up => match app.selected_tab { |
| 93 | SelectedTab::DirectoryBrowser => app.browser.prev(), |
| 94 | SelectedTab::Queue => app.queue_list.prev(), |
| 95 | SelectedTab::Playlists => app.pl_list.prev(), |
| 96 | }, |
| 97 | |
| 98 | KeyCode::Enter | KeyCode::Char('l') => { |
| 99 | // app.update_queue(); |
| 100 | |
| 101 | match app.selected_tab { |
| 102 | SelectedTab::DirectoryBrowser => { |
| 103 | app.browser.handle_enter(&mut app.conn)?; |
| 104 | } |
| 105 | |
| 106 | SelectedTab::Queue => { |
| 107 | let song = app.conn.get_song_with_only_filename( |
| 108 | app.queue_list.list.get(app.queue_list.index).unwrap(), |
| 109 | ); |
| 110 | app.conn.push(&song)?; |
| 111 | } |
| 112 | SelectedTab::Playlists => { |
| 113 | app.conn |
| 114 | .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 115 | } |
| 116 | } |
| 117 | app.conn.update_status(); |
| 118 | } |
| 119 | |
| 120 | KeyCode::Char('h') => match app.selected_tab { |
| 121 | SelectedTab::DirectoryBrowser => { |
| 122 | app.browser.handle_go_back(&mut app.conn)?; |
| 123 | } |
| 124 | SelectedTab::Queue => {} |
| 125 | SelectedTab::Playlists => {} |
| 126 | }, |
| 127 | |
| 128 | // Playback controls |
| 129 | // Toggle Pause |
| 130 | KeyCode::Char('p') => { |
| 131 | app.conn.toggle_pause(); |
| 132 | } |
| 133 | |
| 134 | // Pause |
| 135 | KeyCode::Char('s') => { |
| 136 | app.conn.pause(); |
| 137 | } |
| 138 | |
| 139 | // Toggle rpeat |
| 140 | KeyCode::Char('r') => { |
| 141 | app.conn.toggle_repeat(); |
| 142 | app.conn.update_status(); |
| 143 | } |
| 144 | |
| 145 | // Toggle random |
| 146 | KeyCode::Char('z') => { |
| 147 | app.conn.toggle_random(); |
| 148 | app.conn.update_status(); |
| 149 | } |
| 150 | |
| 151 | // Dmenu prompt |
| 152 | KeyCode::Char('D') => { |
| 153 | app.conn.play_dmenu()?; |
| 154 | } |
| 155 | |
| 156 | // add to queue |
| 157 | KeyCode::Char('a') => { |
| 158 | let list = app |
| 159 | .conn |
| 160 | .songs_filenames |
| 161 | .iter() |
| 162 | .map(|f| f.as_str()) |
| 163 | .collect::<Vec<&str>>(); |
| 164 | |
| 165 | let files: Vec<String> = app |
| 166 | .browser |
| 167 | .filetree |
| 168 | .clone() |
| 169 | .into_iter() |
| 170 | .map(|(_, f)| f) |
| 171 | .collect(); |
| 172 | |
| 173 | let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted( |
| 174 | &files.get(app.browser.selected).unwrap(), |
| 175 | &list, |
| 176 | ) |
| 177 | .get(0) |
| 178 | .unwrap() |
| 179 | .clone(); |
| 180 | |
| 181 | let song = app.conn.get_song_with_only_filename(filename); |
| 182 | |
| 183 | app.conn.conn.push(&song)?; |
| 184 | } |
| 185 | |
| 186 | KeyCode::Right => { |
| 187 | app.conn |
| 188 | .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 189 | } |
| 190 | |
| 191 | // Fast forward |
| 192 | KeyCode::Char('f') => { |
| 193 | let place = app.conn.conn.status().unwrap().song.unwrap().pos; |
| 194 | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 195 | let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); |
| 196 | app.conn.conn.seek(place, pos)?; |
| 197 | } |
| 198 | |
| 199 | // backward |
| 200 | KeyCode::Char('b') => { |
| 201 | let place = app.conn.conn.status().unwrap().song.unwrap().pos; |
| 202 | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 203 | let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); |
| 204 | app.conn.conn.seek(place, pos)?; |
| 205 | } |
| 206 | |
| 207 | KeyCode::Tab => { |
| 208 | app.cycle_tabls(); |
| 209 | } |
| 210 | |
| 211 | KeyCode::Char('1') => { |
| 212 | app.selected_tab = SelectedTab::DirectoryBrowser; |
| 213 | } |
| 214 | |
| 215 | KeyCode::Char('2') => { |
| 216 | app.selected_tab = SelectedTab::Queue; |
| 217 | } |
| 218 | |
| 219 | KeyCode::Char('3') => { |
| 220 | app.selected_tab = SelectedTab::Playlists; |
| 221 | } |
| 222 | |
| 223 | KeyCode::Char('>') => { |
| 224 | app.conn.conn.next()?; |
| 225 | } |
| 226 | |
| 227 | KeyCode::Char('<') => { |
| 228 | app.conn.conn.prev()?; |
| 229 | } |
| 230 | |
| 231 | // Volume controls |
| 232 | KeyCode::Char('=') => { |
| 233 | app.conn.inc_volume(2); |
| 234 | app.conn.update_status(); |
| 235 | } |
| 236 | |
| 237 | KeyCode::Char('-') => { |
| 238 | app.conn.dec_volume(2); |
| 239 | app.conn.update_status(); |
| 240 | } |
| 241 | |
| 242 | // Delete highlighted song from the queue |
| 243 | KeyCode::Char('d') => { |
| 244 | if app.queue_list.index >= app.queue_list.list.len() - 1 { |
| 245 | if app.queue_list.index != 0 { |
| 246 | app.queue_list.index -= 1; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | app.conn.conn.delete(app.queue_list.index as u32)?; |
| 251 | app.update_queue(); |
| 252 | } |
| 253 | |
| 254 | KeyCode::Char('U') => { |
| 255 | app.conn.conn.update()?; |
| 256 | } |
| 257 | |
| 258 | KeyCode::Char('L') => { |
| 259 | let str = dmenu!(prompt "Search: "); |
| 260 | let list = app |
| 261 | .conn |
| 262 | .songs_filenames |
| 263 | .iter() |
| 264 | .map(|f| f.as_str()) |
| 265 | .collect::<Vec<&str>>(); |
| 266 | let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&str, &list) |
| 267 | .get(0) |
| 268 | .unwrap() |
| 269 | .clone(); |
| 270 | |
| 271 | let song = app.conn.get_song_with_only_filename(filename); |
| 272 | app.conn.push(&song)?; |
| 273 | } |
| 274 | |
| 275 | // Search for songs |
| 276 | KeyCode::Char('/') => { |
| 277 | app.inputmode = InputMode::toggle_editing_states(&app.inputmode); |
| 278 | } |
| 279 | |
| 280 | _ => {} |
| 281 | } |
| 282 | } |
| 283 | Ok(()) |
| 284 | } |