Krolyxon
Commit 59eed99

rmptuisrchandler.rs

326 lines · 10.5 KB · Rust

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