Krolyxon
Commit e500c52

rmptuisrchandler.rs

274 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 KeyCode::Char('q') | KeyCode::Esc => app.quit(),
76 KeyCode::Char('c') | KeyCode::Char('C') => {
77 if key_event.modifiers == KeyModifiers::CONTROL {
78 app.quit();
79 } else {
80 app.conn.conn.clear()?;
81 app.conn.update_status();
82 }
83 }
84
85 KeyCode::Char('j') | KeyCode::Down => match app.selected_tab {
86 SelectedTab::DirectoryBrowser => app.browser.next(),
87 SelectedTab::Queue => app.queue_list.next(),
88 SelectedTab::Playlists => app.pl_list.next(),
89 },
90
91 KeyCode::Char('k') | KeyCode::Up => match app.selected_tab {
92 SelectedTab::DirectoryBrowser => app.browser.prev(),
93 SelectedTab::Queue => app.queue_list.prev(),
94 SelectedTab::Playlists => app.pl_list.prev(),
95 },
96
97 KeyCode::Enter | KeyCode::Char('l') => {
98 // app.update_queue();
99
100 match app.selected_tab {
101 SelectedTab::DirectoryBrowser => {
102 app.browser.handle_enter(&mut app.conn)?;
103 }
104
105 SelectedTab::Queue => {
106 let song = app.conn.get_song_with_only_filename(
107 app.queue_list.list.get(app.queue_list.index).unwrap(),
108 );
109 app.conn.push(&song)?;
110 }
111 SelectedTab::Playlists => {
112 app.conn
113 .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
114 }
115 }
116 app.conn.update_status();
117 }
118
119 KeyCode::Char('h') => match app.selected_tab {
120 SelectedTab::DirectoryBrowser => {
121 app.browser.handle_go_back(&mut app.conn)?;
122 }
123 SelectedTab::Queue => {}
124 SelectedTab::Playlists => {}
125 },
126
127 // Playback controls
128 // Toggle Pause
129 KeyCode::Char('p') => {
130 app.conn.toggle_pause();
131 }
132
133 // Pause
134 KeyCode::Char('s') => {
135 app.conn.pause();
136 }
137
138 // Toggle rpeat
139 KeyCode::Char('r') => {
140 app.conn.toggle_repeat();
141 app.conn.update_status();
142 }
143
144 // Toggle random
145 KeyCode::Char('z') => {
146 app.conn.toggle_random();
147 app.conn.update_status();
148 }
149
150 // Dmenu prompt
151 KeyCode::Char('D') => {
152 app.conn.play_dmenu()?;
153 }
154
155 // add to queue
156 KeyCode::Char('a') => {
157 // let song = app.conn.get_song_with_only_filename(
158 // app.conn.songs_filenames.get(app.song_list.index).unwrap(),
159 // );
160
161 let list = app
162 .conn
163 .songs_filenames
164 .iter()
165 .map(|f| f.as_str())
166 .collect::<Vec<&str>>();
167 let (filename, _) =
168 rust_fuzzy_search::fuzzy_search_sorted(&app.browser.path, &list)
169 .get(0)
170 .unwrap()
171 .clone();
172
173 let song = app.conn.get_song_with_only_filename(filename);
174
175 app.conn.conn.push(&song)?;
176 }
177
178 KeyCode::Right => {
179 app.conn
180 .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
181 }
182
183 KeyCode::Char('f') => {
184 let place = app.conn.conn.status().unwrap().song.unwrap().pos;
185 let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
186 let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
187 app.conn.conn.seek(place, pos)?;
188 }
189
190 KeyCode::Char('b') => {
191 let place = app.conn.conn.status().unwrap().song.unwrap().pos;
192 let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
193 let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
194 app.conn.conn.seek(place, pos)?;
195 }
196
197 KeyCode::Tab => {
198 app.cycle_tabls();
199 }
200
201 KeyCode::Char('1') => {
202 app.selected_tab = SelectedTab::DirectoryBrowser;
203 }
204
205 KeyCode::Char('2') => {
206 app.selected_tab = SelectedTab::Queue;
207 }
208
209 KeyCode::Char('3') => {
210 app.selected_tab = SelectedTab::Playlists;
211 }
212
213 KeyCode::Char('>') => {
214 app.conn.conn.next()?;
215 }
216
217 KeyCode::Char('<') => {
218 app.conn.conn.prev()?;
219 }
220
221 // Volume controls
222 KeyCode::Char('=') => {
223 app.conn.inc_volume(2);
224 app.conn.update_status();
225 }
226
227 KeyCode::Char('-') => {
228 app.conn.dec_volume(2);
229 app.conn.update_status();
230 }
231
232 // Delete highlighted song from the queue
233 KeyCode::Char('d') => {
234 if app.queue_list.index >= app.queue_list.list.len() - 1 {
235 if app.queue_list.index != 0 {
236 app.queue_list.index -= 1;
237 }
238 }
239
240 app.conn.conn.delete(app.queue_list.index as u32)?;
241 app.update_queue();
242 }
243
244 KeyCode::Char('U') => {
245 app.conn.conn.update()?;
246 }
247
248 KeyCode::Char('L') => {
249 let str = dmenu!(prompt "Search: ");
250 let list = app
251 .conn
252 .songs_filenames
253 .iter()
254 .map(|f| f.as_str())
255 .collect::<Vec<&str>>();
256 let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&str, &list)
257 .get(0)
258 .unwrap()
259 .clone();
260
261 let song = app.conn.get_song_with_only_filename(filename);
262 app.conn.push(&song)?;
263 }
264
265 // Search for songs
266 KeyCode::Char('/') => {
267 app.inputmode = InputMode::toggle_editing_states(&app.inputmode);
268 }
269
270 _ => {}
271 }
272 }
273 Ok(())
274}