| 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 | } |
| 17 | } |
| 18 | |
| 19 | KeyCode::Char('j') | KeyCode::Down => match app.selected_tab { |
| 20 | SelectedTab::DirectoryBrowser => app.browser.next(), |
| 21 | SelectedTab::Queue => app.queue_list.next(), |
| 22 | SelectedTab::Playlists => app.pl_list.next(), |
| 23 | }, |
| 24 | |
| 25 | KeyCode::Char('k') | KeyCode::Up => match app.selected_tab { |
| 26 | SelectedTab::DirectoryBrowser => app.browser.prev(), |
| 27 | SelectedTab::Queue => app.queue_list.prev(), |
| 28 | SelectedTab::Playlists => app.pl_list.prev(), |
| 29 | }, |
| 30 | |
| 31 | KeyCode::Enter | KeyCode::Char('l') => { |
| 32 | // app.update_queue(); |
| 33 | |
| 34 | match app.selected_tab { |
| 35 | SelectedTab::DirectoryBrowser => { |
| 36 | app.browser.handle_enter(&mut app.conn)?; |
| 37 | } |
| 38 | |
| 39 | SelectedTab::Queue => { |
| 40 | let song = app.conn.get_song_with_only_filename( |
| 41 | app.queue_list.list.get(app.queue_list.index).unwrap(), |
| 42 | ); |
| 43 | app.conn.push(&song)?; |
| 44 | } |
| 45 | SelectedTab::Playlists => { |
| 46 | app.conn |
| 47 | .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | KeyCode::Char('h') => match app.selected_tab { |
| 53 | SelectedTab::DirectoryBrowser => { |
| 54 | app.browser.handle_go_back(&mut app.conn)?; |
| 55 | } |
| 56 | SelectedTab::Queue => {} |
| 57 | SelectedTab::Playlists => {} |
| 58 | }, |
| 59 | |
| 60 | // Playback controls |
| 61 | // Toggle Pause |
| 62 | KeyCode::Char('p') => { |
| 63 | app.conn.toggle_pause(); |
| 64 | } |
| 65 | |
| 66 | // Pause |
| 67 | KeyCode::Char('s') => { |
| 68 | app.conn.pause(); |
| 69 | } |
| 70 | |
| 71 | // Dmenu prompt |
| 72 | KeyCode::Char('D') => { |
| 73 | app.conn.play_dmenu()?; |
| 74 | } |
| 75 | |
| 76 | // add to queue |
| 77 | KeyCode::Char('a') => { |
| 78 | let song = app.conn.get_song_with_only_filename( |
| 79 | app.conn.songs_filenames.get(app.song_list.index).unwrap(), |
| 80 | ); |
| 81 | app.conn.conn.push(&song)?; |
| 82 | } |
| 83 | |
| 84 | KeyCode::Right => { |
| 85 | app.conn |
| 86 | .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?; |
| 87 | } |
| 88 | |
| 89 | KeyCode::Char('f') => { |
| 90 | let place = app.conn.conn.status().unwrap().song.unwrap().pos; |
| 91 | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 92 | let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); |
| 93 | app.conn.conn.seek(place, pos)?; |
| 94 | } |
| 95 | |
| 96 | KeyCode::Char('b') => { |
| 97 | let place = app.conn.conn.status().unwrap().song.unwrap().pos; |
| 98 | let (pos, _) = app.conn.conn.status().unwrap().time.unwrap(); |
| 99 | let pos = Duration::from_secs(pos.as_secs().wrapping_add(2)); |
| 100 | app.conn.conn.seek(place, pos)?; |
| 101 | } |
| 102 | |
| 103 | KeyCode::Tab => { |
| 104 | app.cycle_tabls(); |
| 105 | } |
| 106 | |
| 107 | KeyCode::Char('1') => { |
| 108 | app.selected_tab = SelectedTab::DirectoryBrowser; |
| 109 | } |
| 110 | |
| 111 | KeyCode::Char('2') => { |
| 112 | app.selected_tab = SelectedTab::Queue; |
| 113 | } |
| 114 | |
| 115 | KeyCode::Char('3') => { |
| 116 | app.selected_tab = SelectedTab::Playlists; |
| 117 | } |
| 118 | |
| 119 | KeyCode::Char('>') => { |
| 120 | app.conn.conn.next()?; |
| 121 | } |
| 122 | |
| 123 | KeyCode::Char('<') => { |
| 124 | app.conn.conn.prev()?; |
| 125 | } |
| 126 | |
| 127 | // Volume controls |
| 128 | KeyCode::Char('=') => { |
| 129 | app.conn.inc_volume(2); |
| 130 | app.conn.update_volume(); |
| 131 | } |
| 132 | |
| 133 | KeyCode::Char('-') => { |
| 134 | app.conn.dec_volume(2); |
| 135 | app.conn.update_volume(); |
| 136 | } |
| 137 | |
| 138 | // Delete highlighted song from the queue |
| 139 | KeyCode::Char('d') => { |
| 140 | app.conn.conn.delete(app.queue_list.index as u32)?; |
| 141 | app.update_queue(); |
| 142 | } |
| 143 | |
| 144 | KeyCode::Char('U') => { |
| 145 | app.conn.conn.update()?; |
| 146 | } |
| 147 | |
| 148 | KeyCode::Char('L') => { |
| 149 | let str = dmenu!(prompt "Search: "); |
| 150 | let list = app |
| 151 | .conn |
| 152 | .songs_filenames |
| 153 | .iter() |
| 154 | .map(|f| f.as_str()) |
| 155 | .collect::<Vec<&str>>(); |
| 156 | let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&str, &list) |
| 157 | .get(0) |
| 158 | .unwrap() |
| 159 | .clone(); |
| 160 | |
| 161 | let song = app.conn.get_song_with_only_filename(filename); |
| 162 | app.conn.push(&song)?; |
| 163 | } |
| 164 | |
| 165 | _ => {} |
| 166 | } |
| 167 | |
| 168 | Ok(()) |
| 169 | } |