| 1 | use std::time::Duration; |
| 2 | |
| 3 | use crate::app::{App, AppResult, SelectedTab}; |
| 4 | use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; |
| 5 | use rust_fuzzy_search; |
| 6 | use simple_dmenu::dmenu; |
| 7 | |
| 8 | 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(); |
| 17 | } |
| 18 | } |
| 19 | |
| 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 | }, |
| 25 | |
| 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 | }, |
| 31 | |
| 32 | KeyCode::Enter | KeyCode::Char('l') => { |
| 33 | // app.update_queue(); |
| 34 | |
| 35 | match app.selected_tab { |
| 36 | SelectedTab::DirectoryBrowser => { |
| 37 | app.browser.handle_enter(&mut app.conn)?; |
| 38 | } |
| 39 | |
| 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)?; |
| 45 | } |
| 46 | SelectedTab::Playlists => { |
| 47 | app.conn |
| 48 | .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 49 | } |
| 50 | } |
| 51 | app.conn.update_status(); |
| 52 | } |
| 53 | |
| 54 | KeyCode::Char('h') => match app.selected_tab { |
| 55 | SelectedTab::DirectoryBrowser => { |
| 56 | app.browser.handle_go_back(&mut app.conn)?; |
| 57 | } |
| 58 | SelectedTab::Queue => {} |
| 59 | SelectedTab::Playlists => {} |
| 60 | }, |
| 61 | |
| 62 | // Playback controls |
| 63 | // Toggle Pause |
| 64 | KeyCode::Char('p') => { |
| 65 | app.conn.toggle_pause(); |
| 66 | } |
| 67 | |
| 68 | // Pause |
| 69 | KeyCode::Char('s') => { |
| 70 | app.conn.pause(); |
| 71 | } |
| 72 | |
| 73 | // Toggle rpeat |
| 74 | KeyCode::Char('r') => { |
| 75 | app.conn.toggle_repeat(); |
| 76 | app.conn.update_status(); |
| 77 | } |
| 78 | |
| 79 | // Toggle random |
| 80 | KeyCode::Char('z') => { |
| 81 | app.conn.toggle_random(); |
| 82 | app.conn.update_status(); |
| 83 | } |
| 84 | |
| 85 | // Dmenu prompt |
| 86 | KeyCode::Char('D') => { |
| 87 | app.conn.play_dmenu()?; |
| 88 | } |
| 89 | |
| 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 | } |
| 111 | |
| 112 | KeyCode::Right => { |
| 113 | app.conn |
| 114 | .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 115 | } |
| 116 | |
| 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 | } |
| 123 | |
| 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 | } |
| 130 | |
| 131 | KeyCode::Tab => { |
| 132 | app.cycle_tabls(); |
| 133 | } |
| 134 | |
| 135 | KeyCode::Char('1') => { |
| 136 | app.selected_tab = SelectedTab::DirectoryBrowser; |
| 137 | } |
| 138 | |
| 139 | KeyCode::Char('2') => { |
| 140 | app.selected_tab = SelectedTab::Queue; |
| 141 | } |
| 142 | |
| 143 | KeyCode::Char('3') => { |
| 144 | app.selected_tab = SelectedTab::Playlists; |
| 145 | } |
| 146 | |
| 147 | KeyCode::Char('>') => { |
| 148 | app.conn.conn.next()?; |
| 149 | } |
| 150 | |
| 151 | KeyCode::Char('<') => { |
| 152 | app.conn.conn.prev()?; |
| 153 | } |
| 154 | |
| 155 | // Volume controls |
| 156 | KeyCode::Char('=') => { |
| 157 | app.conn.inc_volume(2); |
| 158 | app.conn.update_status(); |
| 159 | } |
| 160 | |
| 161 | KeyCode::Char('-') => { |
| 162 | app.conn.dec_volume(2); |
| 163 | app.conn.update_status(); |
| 164 | } |
| 165 | |
| 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 | } |
| 172 | } |
| 173 | |
| 174 | app.conn.conn.delete(app.queue_list.index as u32)?; |
| 175 | app.update_queue(); |
| 176 | } |
| 177 | |
| 178 | KeyCode::Char('U') => { |
| 179 | app.conn.conn.update()?; |
| 180 | } |
| 181 | |
| 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)?; |
| 197 | } |
| 198 | |
| 199 | _ => {} |
| 200 | } |
| 201 | |
| 202 | Ok(()) |
| 203 | } |