Krolyxon
Commit 255687b

rmptuisrchandler.rs

291 lines · 9.4 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') => app.conn.toggle_pause(),
181
182 // Pause
183 KeyCode::Char('s') => app.conn.pause(),
184
185 // Toggle rpeat
186 KeyCode::Char('r') => {
187 app.conn.toggle_repeat();
188 app.conn.update_status();
189 }
190
191 // Toggle random
192 KeyCode::Char('z') => {
193 app.conn.toggle_random();
194 app.conn.update_status();
195 }
196
197 // Dmenu prompt
198 KeyCode::Char('D') => app.conn.play_dmenu()?,
199
200 // add to queue
201 KeyCode::Char('a') => app.playlist_popup = true,
202
203 KeyCode::Right => {
204 app.conn
205 .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
206 }
207
208 // Fast forward
209 KeyCode::Char('f') => {
210 let place = app.conn.conn.status().unwrap().song.unwrap().pos;
211 let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
212 let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
213 app.conn.conn.seek(place, pos)?;
214 }
215
216 // backward
217 KeyCode::Char('b') => {
218 let place = app.conn.conn.status().unwrap().song.unwrap().pos;
219 let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
220 let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
221 app.conn.conn.seek(place, pos)?;
222 }
223
224 // Cycle through tabs
225 KeyCode::Tab => {
226 app.cycle_tabls();
227 }
228
229 // Directory browser tab
230 KeyCode::Char('1') => {
231 app.selected_tab = SelectedTab::DirectoryBrowser;
232 }
233
234 // Playing queue tab
235 KeyCode::Char('2') => {
236 app.selected_tab = SelectedTab::Queue;
237 }
238
239 // Playlists tab
240 KeyCode::Char('3') => {
241 app.selected_tab = SelectedTab::Playlists;
242 }
243
244 // Play next song
245 KeyCode::Char('>') => {
246 app.conn.conn.next()?;
247 }
248
249 // Play previous song
250 KeyCode::Char('<') => {
251 app.conn.conn.prev()?;
252 }
253
254 // Volume controls
255 KeyCode::Char('=') => {
256 app.conn.inc_volume(2);
257 app.conn.update_status();
258 }
259
260 KeyCode::Char('-') => {
261 app.conn.dec_volume(2);
262 app.conn.update_status();
263 }
264
265 // Delete highlighted song from the queue
266 KeyCode::Char('d') => {
267 if app.queue_list.index >= app.queue_list.list.len() - 1 {
268 if app.queue_list.index != 0 {
269 app.queue_list.index -= 1;
270 }
271 }
272
273 app.conn.conn.delete(app.queue_list.index as u32)?;
274 app.update_queue();
275 }
276
277 // Update MPD database
278 KeyCode::Char('U') => {
279 app.conn.conn.update()?;
280 }
281
282 // Search for songs
283 KeyCode::Char('/') => {
284 app.inputmode = InputMode::toggle_editing_states(&app.inputmode);
285 }
286
287 _ => {}
288 }
289 }
290 Ok(())
291}