Krolyxon
Commit 4bc03ce

rmptuisrchandler.rs

147 lines · 4.2 KB · Rust

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