Krolyxon
Commit 00c5c12

rmptuisrchandler.rs

174 lines · 5.0 KB · Rust

1use std::time::Duration;
2
3use crate::app::{App, AppResult, SelectedTab};
4use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
5use mpd::{Query, Term};
6use ratatui::style::Modifier;
7use rust_fuzzy_search;
8use simple_dmenu::dmenu;
9
10pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
11 match key_event.code {
12 KeyCode::Char('q') | KeyCode::Esc => app.quit(),
13 KeyCode::Char('c') | KeyCode::Char('C') => {
14 if key_event.modifiers == KeyModifiers::CONTROL {
15 app.quit();
16 }
17 }
18
19 KeyCode::Char('j') | KeyCode::Down => match app.selected_tab {
20 SelectedTab::DirectoryBrowser => app.browser.next(),
21 SelectedTab::Queue => app.queue_list.next(),
22 SelectedTab::Playlists => app.pl_list.next(),
23 },
24
25 KeyCode::Char('k') | KeyCode::Up => match app.selected_tab {
26 SelectedTab::DirectoryBrowser => app.browser.prev(),
27 SelectedTab::Queue => app.queue_list.prev(),
28 SelectedTab::Playlists => app.pl_list.prev(),
29 },
30
31 KeyCode::Enter | KeyCode::Char('l') => {
32 // app.update_queue();
33
34 match app.selected_tab {
35 SelectedTab::DirectoryBrowser => {
36 app.browser.handle_enter(&mut app.conn)?;
37 }
38
39 SelectedTab::Queue => {
40 let song = app.conn.get_song_with_only_filename(
41 app.queue_list.list.get(app.queue_list.index).unwrap(),
42 );
43 app.conn.push(&song)?;
44 }
45 SelectedTab::Playlists => {
46 app.conn
47 .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
48 }
49 }
50 }
51
52 KeyCode::Char('h') => match app.selected_tab {
53 SelectedTab::DirectoryBrowser => {
54 app.browser.handle_go_back(&mut app.conn)?;
55 }
56 SelectedTab::Queue => {}
57 SelectedTab::Playlists => {}
58 },
59
60 // Playback controls
61 // Toggle Pause
62 KeyCode::Char('p') => {
63 app.conn.toggle_pause();
64 }
65
66 // Pause
67 KeyCode::Char('s') => {
68 app.conn.pause();
69 }
70
71 // Clear Queue
72 KeyCode::Char('x') => {
73 app.conn.conn.clear()?;
74 // app.update_queue();
75 }
76
77 // Dmenu prompt
78 KeyCode::Char('D') => {
79 app.conn.play_dmenu()?;
80 }
81
82 // add to queue
83 KeyCode::Char('a') => {
84 let song = app.conn.get_song_with_only_filename(
85 app.conn.songs_filenames.get(app.song_list.index).unwrap(),
86 );
87 app.conn.conn.push(&song)?;
88 }
89
90 KeyCode::Right => {
91 app.conn
92 .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
93 }
94
95 KeyCode::Char('f') => {
96 let place = app.conn.conn.status().unwrap().song.unwrap().pos;
97 let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
98 let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
99 app.conn.conn.seek(place, pos)?;
100 }
101
102 KeyCode::Char('b') => {
103 let place = app.conn.conn.status().unwrap().song.unwrap().pos;
104 let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
105 let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
106 app.conn.conn.seek(place, pos)?;
107 }
108
109 KeyCode::Tab => {
110 app.cycle_tabls();
111 }
112
113 KeyCode::Char('1') => {
114 app.selected_tab = SelectedTab::DirectoryBrowser;
115 }
116
117 KeyCode::Char('2') => {
118 app.selected_tab = SelectedTab::Queue;
119 }
120
121 KeyCode::Char('3') => {
122 app.selected_tab = SelectedTab::Playlists;
123 }
124
125
126 KeyCode::Char('n') => {
127 app.conn.conn.next()?;
128 }
129
130 KeyCode::Char('N') => {
131 app.conn.conn.prev()?;
132 }
133
134 // Volume controls
135 KeyCode::Char('=') => {
136 app.conn.inc_volume(2);
137 }
138
139 KeyCode::Char('-') => {
140 app.conn.dec_volume(2);
141 }
142
143 // Delete highlighted song from the queue
144 KeyCode::Char('d') => {
145 app.conn.conn.delete(app.queue_list.index as u32)?;
146 app.update_queue();
147 }
148
149 KeyCode::Char('U') => {
150 app.conn.conn.update()?;
151 }
152
153 KeyCode::Char('L') => {
154 let str = dmenu!(prompt "Search: ");
155 let list = app
156 .conn
157 .songs_filenames
158 .iter()
159 .map(|f| f.as_str())
160 .collect::<Vec<&str>>();
161 let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&str, &list)
162 .get(0)
163 .unwrap()
164 .clone();
165
166 let song = app.conn.get_song_with_only_filename(filename);
167 app.conn.push(&song)?;
168 }
169
170 _ => {}
171 }
172
173 Ok(())
174}