Krolyxon
Commit 8269766

rmptuisrchandler.rs

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