Krolyxon
Commit f665c4e

rmptuisrchandler.rs

167 lines · 4.9 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::SongList => app.song_list.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::SongList => app.song_list.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::SongList => {
36 let song = app.conn.get_song_with_only_filename(
37 app.conn.songs_filenames.get(app.song_list.index).unwrap(),
38 );
39 app.conn.push(&song)?;
40 }
41 SelectedTab::Queue => {
42 let song = app.conn.get_song_with_only_filename(
43 app.queue_list.list.get(app.queue_list.index).unwrap(),
44 );
45 app.conn.push(&song)?;
46 }
47 SelectedTab::Playlists => {
48 app.conn
49 .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
50 }
51 }
52 }
53
54 // Playback controls
55 // Toggle Pause
56 KeyCode::Char('p') => {
57 app.conn.toggle_pause();
58 }
59
60 // Pause
61 KeyCode::Char('s') => {
62 app.conn.pause();
63 }
64
65 // Clear Queue
66 KeyCode::Char('x') => {
67 app.conn.conn.clear()?;
68 // app.update_queue();
69 }
70
71 // Dmenu prompt
72 KeyCode::Char('D') => {
73 app.conn.play_dmenu()?;
74 }
75
76 // add to queue
77 KeyCode::Char('a') => {
78 let song = app.conn.get_song_with_only_filename(
79 app.conn.songs_filenames.get(app.song_list.index).unwrap(),
80 );
81 app.conn.conn.push(&song)?;
82 }
83
84 KeyCode::Right => {
85 app.conn
86 .push_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
87 }
88
89 KeyCode::Char('f') => {
90 let place = app.conn.conn.status().unwrap().song.unwrap().pos;
91 let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
92 let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
93 app.conn.conn.seek(place, pos)?;
94 }
95
96 KeyCode::Char('b') => {
97 let place = app.conn.conn.status().unwrap().song.unwrap().pos;
98 let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
99 let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
100 app.conn.conn.seek(place, pos)?;
101 }
102
103 KeyCode::Tab => {
104 app.cycle_tabls();
105 }
106
107 KeyCode::Char('1') => {
108 app.selected_tab = SelectedTab::SongList;
109 }
110
111 KeyCode::Char('2') => {
112 app.selected_tab = SelectedTab::Queue;
113 }
114
115 KeyCode::Char('3') => {
116 app.selected_tab = SelectedTab::Playlists;
117 }
118
119 KeyCode::Char('n') => {
120 app.conn.conn.next()?;
121 }
122
123 KeyCode::Char('N') => {
124 app.conn.conn.prev()?;
125 }
126
127 // Volume controls
128 KeyCode::Char('=') => {
129 app.conn.inc_volume(2);
130 }
131
132 KeyCode::Char('-') => {
133 app.conn.dec_volume(2);
134 }
135
136 // Delete highlighted song from the queue
137 KeyCode::Char('d') => {
138 app.conn.conn.delete(app.queue_list.index as u32)?;
139 app.update_queue();
140 }
141
142 KeyCode::Char('U') => {
143 app.conn.conn.update()?;
144 }
145
146 KeyCode::Char('L') => {
147 let str = dmenu!(prompt "Search: ");
148 let list = app
149 .conn
150 .songs_filenames
151 .iter()
152 .map(|f| f.as_str())
153 .collect::<Vec<&str>>();
154 let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&str, &list)
155 .get(0)
156 .unwrap()
157 .clone();
158
159 let song = app.conn.get_song_with_only_filename(filename);
160 app.conn.push(&song)?;
161 }
162
163 _ => {}
164 }
165
166 Ok(())
167}