Krolyxon
Commit c61b050

rmptuisrchandler.rs

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