Krolyxon
Commit b7fc6bc

rmptuib7fc6bc

move the keymaps to thier individual files

Author
krolxon
Date
2024-05-01 23:45 +0530
Parent
9555328
Commit
b7fc6bc1a70da169f80afdc790c1a8ccf853d319

+465 −437

10 files changed

  1. R src/event.rs → src/event/event.rs +0 −0
  2. A src/event/handler.rs +220 −0
  3. A src/event/mod.rs +5 −0
  4. A src/event/pl_append_keys.rs +82 −0
  5. A src/event/pl_rename_keys.rs +43 −0
  6. A src/event/search_keys.rs +110 −0
  7. D src/handler.rs +0 −429
  8. M src/lib.rs +1 −4
  9. M src/main.rs +3 −3
  10. M src/tui.rs +1 −1
R src/event.rs → src/event/event.rs View

[No content changes]

A src/event/handler.rs View
@@ -0,0 +1,220 @@
1+use crate::{
2+ app::{App, AppResult, SelectedTab},
3+ ui::InputMode,
4+};
5+use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
6+use std::time::Duration;
7+
8+use super::{pl_append_keys, pl_rename_keys, search_keys};
9+
10+pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
11+ // Live search update
12+ if app.inputmode == InputMode::Editing {
13+ search_keys::handle_search_keys(key_event, app)?;
14+ } else if app.inputmode == InputMode::PlaylistRename {
15+ pl_rename_keys::handle_pl_rename_keys(key_event, app)?;
16+ } else if app.playlist_popup {
17+ pl_append_keys::hande_pl_append_keys(key_event, app)?;
18+ } else {
19+ // General KeyMaps
20+ match key_event.code {
21+ // Quit
22+ KeyCode::Char('q') => app.quit(),
23+ KeyCode::Char('c') | KeyCode::Char('C') => {
24+ if key_event.modifiers == KeyModifiers::CONTROL {
25+ app.quit();
26+ } else {
27+ app.conn.conn.clear()?;
28+ app.conn.update_status();
29+ app.queue_list.list.clear();
30+ app.queue_list.reset_index();
31+ }
32+ }
33+
34+ // Go Up
35+ KeyCode::Char('j') | KeyCode::Down => match app.selected_tab {
36+ SelectedTab::DirectoryBrowser => app.browser.next(),
37+ SelectedTab::Queue => app.queue_list.next(),
38+ SelectedTab::Playlists => app.pl_list.next(),
39+ },
40+
41+ // Go down
42+ KeyCode::Char('k') | KeyCode::Up => match app.selected_tab {
43+ SelectedTab::DirectoryBrowser => app.browser.prev(),
44+ SelectedTab::Queue => app.queue_list.prev(),
45+ SelectedTab::Playlists => app.pl_list.prev(),
46+ },
47+
48+ // Next directory
49+ KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
50+ // app.update_queue();
51+
52+ match app.selected_tab {
53+ SelectedTab::DirectoryBrowser => {
54+ app.handle_enter()?;
55+ }
56+
57+ SelectedTab::Queue => {
58+ app.conn.conn.switch(app.queue_list.index as u32)?;
59+ }
60+
61+ SelectedTab::Playlists => {
62+ app.conn
63+ .load_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
64+ }
65+ }
66+ app.conn.update_status();
67+ }
68+
69+ // head back to previous directory
70+ KeyCode::Char('h') | KeyCode::Left => match app.selected_tab {
71+ SelectedTab::DirectoryBrowser => {
72+ app.browser.handle_go_back(&mut app.conn)?;
73+ }
74+ SelectedTab::Queue => {}
75+ SelectedTab::Playlists => {}
76+ },
77+
78+ // Playback controls
79+ // Toggle Pause
80+ KeyCode::Char('p') => app.conn.toggle_pause(),
81+
82+ // Pause
83+ KeyCode::Char('s') => app.conn.pause(),
84+
85+ // Toggle rpeat
86+ KeyCode::Char('r') => {
87+ app.conn.toggle_repeat();
88+ app.conn.update_status();
89+ }
90+
91+ // Toggle random
92+ KeyCode::Char('z') => {
93+ app.conn.toggle_random();
94+ app.conn.update_status();
95+ }
96+
97+ // Dmenu prompt
98+ KeyCode::Char('D') => app.conn.play_dmenu()?,
99+
100+ // add to queue
101+ KeyCode::Char('a') => app.playlist_popup = true,
102+
103+ // Fast forward
104+ KeyCode::Char('f') => {
105+ let place = app.conn.conn.status().unwrap().song.unwrap().pos;
106+ let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
107+ let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
108+ app.conn.conn.seek(place, pos)?;
109+ }
110+
111+ // backward
112+ KeyCode::Char('b') => {
113+ let place = app.conn.conn.status().unwrap().song.unwrap().pos;
114+ let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
115+ let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
116+ app.conn.conn.seek(place, pos)?;
117+ }
118+
119+ // Cycle through tabs
120+ KeyCode::Tab => {
121+ app.cycle_tabls();
122+ }
123+
124+ // Directory browser tab
125+ KeyCode::Char('1') => {
126+ app.selected_tab = SelectedTab::Queue;
127+ }
128+
129+ // Playing queue tab
130+ KeyCode::Char('2') => {
131+ app.selected_tab = SelectedTab::DirectoryBrowser;
132+ }
133+
134+ // Playlists tab
135+ KeyCode::Char('3') => {
136+ app.selected_tab = SelectedTab::Playlists;
137+ }
138+
139+ // Play next song
140+ KeyCode::Char('>') => {
141+ if !app.queue_list.list.is_empty() {
142+ app.conn.conn.next()?;
143+ app.update_queue();
144+ }
145+ }
146+
147+ // Play previous song
148+ KeyCode::Char('<') => {
149+ if !app.queue_list.list.is_empty() {
150+ app.conn.conn.prev()?;
151+ app.update_queue();
152+ }
153+ }
154+
155+ // Volume controls
156+ KeyCode::Char('=') => {
157+ app.conn.inc_volume(2);
158+ app.conn.update_status();
159+ }
160+
161+ KeyCode::Char('-') => {
162+ app.conn.dec_volume(2);
163+ app.conn.update_status();
164+ }
165+
166+ // Delete highlighted song from the queue
167+ KeyCode::Char('d') => {
168+ if app.queue_list.index >= app.queue_list.list.len() - 1
169+ && app.queue_list.index != 0
170+ {
171+ app.queue_list.index -= 1;
172+ }
173+
174+ app.conn.conn.delete(app.queue_list.index as u32)?;
175+ app.update_queue();
176+ }
177+
178+ // Update MPD database
179+ KeyCode::Char('U') => {
180+ app.conn.conn.rescan()?;
181+ app.browser.update_directory(&mut app.conn)?;
182+ }
183+
184+ // Search for songs
185+ KeyCode::Char('/') => {
186+ if app.inputmode == InputMode::Normal {
187+ app.inputmode = InputMode::Editing;
188+ } else {
189+ app.inputmode = InputMode::Normal;
190+ }
191+ }
192+
193+ // Remove from Current Playlsit
194+ KeyCode::Char(' ') | KeyCode::Backspace => {
195+ app.handle_add_or_remove_from_current_playlist()?;
196+ }
197+
198+ // go to top of list
199+ KeyCode::Char('g') => match app.selected_tab {
200+ SelectedTab::Queue => app.queue_list.index = 0,
201+ SelectedTab::DirectoryBrowser => app.browser.selected = 0,
202+ SelectedTab::Playlists => app.pl_list.index = 0,
203+ },
204+
205+ // go to bottom of list
206+ KeyCode::Char('G') => match app.selected_tab {
207+ SelectedTab::Queue => app.queue_list.index = app.queue_list.list.len() - 1,
208+ SelectedTab::DirectoryBrowser => {
209+ app.browser.selected = app.browser.filetree.len() - 1
210+ }
211+ SelectedTab::Playlists => app.pl_list.index = app.pl_list.list.len() - 1,
212+ },
213+
214+ // Change playlist name
215+ KeyCode::Char('e') => app.change_playlist_name()?,
216+ _ => {}
217+ }
218+ }
219+ Ok(())
220+}
A src/event/mod.rs View
@@ -0,0 +1,5 @@
1+pub mod event;
2+pub mod handler;
3+pub mod search_keys;
4+pub mod pl_rename_keys;
5+pub mod pl_append_keys;
A src/event/pl_append_keys.rs View
@@ -0,0 +1,82 @@
1+use crate::app::{App, AppResult, SelectedTab};
2+use crate::browser::FileExtension;
3+use crossterm::event::{KeyCode, KeyEvent};
4+use std::path::Path;
5+
6+pub fn hande_pl_append_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
7+ match key_event.code {
8+ KeyCode::Char('q') | KeyCode::Esc => {
9+ app.playlist_popup = false;
10+ }
11+
12+ KeyCode::Char('j') | KeyCode::Down => app.append_list.next(),
13+ KeyCode::Char('k') | KeyCode::Up => app.append_list.prev(),
14+
15+ KeyCode::Enter => {
16+ let pl_index = app.append_list.index;
17+ let pl_name = &app.append_list.list.get(pl_index).unwrap();
18+
19+ let s_index: usize;
20+ match app.selected_tab {
21+ SelectedTab::Queue => {
22+ s_index = app.queue_list.index;
23+ let short_path = &app.queue_list.list.get(s_index).unwrap().file;
24+
25+ if let Some(full_path) = app.conn.get_full_path(short_path) {
26+ let song = app.conn.get_song_with_only_filename(&full_path);
27+
28+ if *pl_name == "Current Playlist" {
29+ app.conn.conn.push(&song)?;
30+ app.update_queue();
31+ } else {
32+ app.conn.add_to_playlist(pl_name, &song)?;
33+ }
34+ }
35+ }
36+
37+ SelectedTab::DirectoryBrowser => {
38+ let (t, f) = app.browser.filetree.get(app.browser.selected).unwrap();
39+ if t == "file" {
40+ let short_path = f;
41+ if let Some(full_path) = app.conn.get_full_path(short_path) {
42+ let song = app.conn.get_song_with_only_filename(&full_path);
43+
44+ if *pl_name == "Current Playlist" {
45+ app.conn.conn.push(&song)?;
46+ app.update_queue();
47+ } else {
48+ app.conn.add_to_playlist(pl_name, &song)?;
49+ }
50+ }
51+ } else if t == "directory" {
52+ for (t, f) in app.conn.conn.listfiles(f)?.iter() {
53+ // dir_vec.push((t, f));
54+ if t == "file"
55+ && Path::new(&f).has_extension(&[
56+ "mp3", "ogg", "flac", "m4a", "wav", "aac", "opus", "ape",
57+ "wma", "mpc", "aiff", "dff", "mp2", "mka",
58+ ])
59+ {
60+ let full_path = app.conn.get_full_path(f).unwrap_or_default();
61+ let song = app.conn.get_song_with_only_filename(&full_path);
62+ if *pl_name == "Current Playlist" {
63+ app.conn.conn.push(&song)?;
64+ } else {
65+ app.conn.add_to_playlist(pl_name, &song)?;
66+ }
67+ }
68+ }
69+ }
70+ }
71+ _ => {}
72+ }
73+
74+ // hide the playlist popup
75+ app.playlist_popup = false;
76+ app.append_list.index = 0;
77+ }
78+ _ => {}
79+ }
80+
81+ Ok(())
82+}
A src/event/pl_rename_keys.rs View
@@ -0,0 +1,43 @@
1+use crate::{
2+ app::{App, AppResult},
3+ ui::InputMode,
4+};
5+use crossterm::event::{KeyCode, KeyEvent};
6+
7+pub fn handle_pl_rename_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
8+ match key_event.code {
9+ KeyCode::Esc => {
10+ app.pl_newname_input.clear();
11+ app.reset_cursor();
12+ app.inputmode = InputMode::Normal;
13+ }
14+ KeyCode::Char(to_insert) => {
15+ app.enter_char(to_insert);
16+ }
17+ KeyCode::Enter => {
18+ app.conn.conn.pl_rename(
19+ app.pl_list.list.get(app.pl_list.index).unwrap(),
20+ &app.pl_newname_input,
21+ )?;
22+ app.pl_list.list = App::get_playlist(&mut app.conn.conn)?;
23+ app.pl_newname_input.clear();
24+ app.reset_cursor();
25+ app.inputmode = InputMode::Normal;
26+ }
27+
28+ KeyCode::Backspace => {
29+ app.delete_char();
30+ }
31+
32+ KeyCode::Left => {
33+ app.move_cursor_left();
34+ }
35+
36+ KeyCode::Right => {
37+ app.move_cursor_right();
38+ }
39+
40+ _ => {}
41+ }
42+ Ok(())
43+}
A src/event/search_keys.rs View
@@ -0,0 +1,110 @@
1+use crate::{
2+ app::{App, AppResult, SelectedTab},
3+ ui::InputMode,
4+};
5+use crossterm::event::{KeyCode, KeyEvent};
6+use rust_fuzzy_search::{self, fuzzy_search_sorted};
7+
8+pub fn handle_search_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
9+ match app.selected_tab {
10+ SelectedTab::DirectoryBrowser => {
11+ let list: Vec<&str> = app
12+ .browser
13+ .filetree
14+ .iter()
15+ .map(|(_, f)| f.as_str())
16+ .collect::<Vec<&str>>();
17+
18+ let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
19+ let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();
20+
21+ for (i, (_, item)) in app.browser.filetree.iter().enumerate() {
22+ if item.contains(res.first().unwrap()) {
23+ app.browser.selected = i;
24+ }
25+ }
26+ }
27+
28+ SelectedTab::Queue => {
29+ let list: Vec<&str> = app
30+ .queue_list
31+ .list
32+ .iter()
33+ .map(|f| f.file.as_str())
34+ .collect::<Vec<&str>>();
35+ let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
36+ let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();
37+
38+ for (i, item) in app.queue_list.list.iter().enumerate() {
39+ if item.file.contains(res.first().unwrap()) {
40+ app.queue_list.index = i;
41+ }
42+ }
43+ }
44+
45+ SelectedTab::Playlists => {
46+ let list: Vec<&str> = app
47+ .pl_list
48+ .list
49+ .iter()
50+ .map(|f| f.as_str())
51+ .collect::<Vec<&str>>();
52+ let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
53+ let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();
54+
55+ for (i, item) in app.pl_list.list.iter().enumerate() {
56+ if item.contains(res.first().unwrap()) {
57+ app.pl_list.index = i;
58+ }
59+ }
60+ }
61+ }
62+
63+ // Keybind for searching
64+ //
65+ // Keybinds for when the search prompt is visible
66+ match key_event.code {
67+ KeyCode::Esc => {
68+ app.inputmode = InputMode::Normal;
69+ }
70+ KeyCode::Char(to_insert) => {
71+ app.enter_char(to_insert);
72+ }
73+ KeyCode::Enter => {
74+ let list: Vec<&str> = app
75+ .browser
76+ .filetree
77+ .iter()
78+ .map(|(_, f)| f.as_str())
79+ .collect::<Vec<&str>>();
80+
81+ let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
82+ let (res, _) = res.first().unwrap();
83+
84+ for (i, (_, item)) in app.browser.filetree.iter().enumerate() {
85+ if item.contains(res) {
86+ app.browser.selected = i;
87+ }
88+ }
89+
90+ app.search_input.clear();
91+ app.reset_cursor();
92+ app.inputmode = InputMode::Normal;
93+ }
94+
95+ KeyCode::Backspace => {
96+ app.delete_char();
97+ }
98+
99+ KeyCode::Left => {
100+ app.move_cursor_left();
101+ }
102+
103+ KeyCode::Right => {
104+ app.move_cursor_right();
105+ }
106+
107+ _ => {}
108+ }
109+ Ok(())
110+}
D src/handler.rs View
@@ -1,429 +0,0 @@
1−use crate::browser::FileExtension;
2−use crate::{
3− app::{App, AppResult, SelectedTab},
4− ui::InputMode,
5−};
6−use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
7−use rust_fuzzy_search::{self, fuzzy_search_sorted};
8−use std::{path::Path, time::Duration};
9−
10−pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
11− if app.inputmode == InputMode::Editing {
12− // Live search update
13− match app.selected_tab {
14− SelectedTab::DirectoryBrowser => {
15− let list: Vec<&str> = app
16− .browser
17− .filetree
18− .iter()
19− .map(|(_, f)| f.as_str())
20− .collect::<Vec<&str>>();
21−
22− let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
23− let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();
24−
25− for (i, (_, item)) in app.browser.filetree.iter().enumerate() {
26− if item.contains(res.first().unwrap()) {
27− app.browser.selected = i;
28− }
29− }
30− }
31−
32− SelectedTab::Queue => {
33− let list: Vec<&str> = app
34− .queue_list
35− .list
36− .iter()
37− .map(|f| f.file.as_str())
38− .collect::<Vec<&str>>();
39− let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
40− let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();
41−
42− for (i, item) in app.queue_list.list.iter().enumerate() {
43− if item.file.contains(res.first().unwrap()) {
44− app.queue_list.index = i;
45− }
46− }
47− }
48−
49− SelectedTab::Playlists => {
50− let list: Vec<&str> = app
51− .pl_list
52− .list
53− .iter()
54− .map(|f| f.as_str())
55− .collect::<Vec<&str>>();
56− let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
57− let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>();
58−
59− for (i, item) in app.pl_list.list.iter().enumerate() {
60− if item.contains(res.first().unwrap()) {
61− app.pl_list.index = i;
62− }
63− }
64− }
65− }
66−
67− // Keybind for searching
68− //
69− // Keybinds for when the search prompt is visible
70− match key_event.code {
71− KeyCode::Esc => {
72− app.inputmode = InputMode::Normal;
73− }
74− KeyCode::Char(to_insert) => {
75− app.enter_char(to_insert);
76− }
77− KeyCode::Enter => {
78− let list: Vec<&str> = app
79− .browser
80− .filetree
81− .iter()
82− .map(|(_, f)| f.as_str())
83− .collect::<Vec<&str>>();
84−
85− let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list);
86− let (res, _) = res.first().unwrap();
87−
88− for (i, (_, item)) in app.browser.filetree.iter().enumerate() {
89− if item.contains(res) {
90− app.browser.selected = i;
91− }
92− }
93−
94− app.search_input.clear();
95− app.reset_cursor();
96− app.inputmode = InputMode::Normal;
97− }
98−
99− KeyCode::Backspace => {
100− app.delete_char();
101− }
102−
103− KeyCode::Left => {
104− app.move_cursor_left();
105− }
106−
107− KeyCode::Right => {
108− app.move_cursor_right();
109− }
110−
111− _ => {}
112− }
113− } else if app.inputmode == InputMode::PlaylistRename {
114− match key_event.code {
115− KeyCode::Esc => {
116− app.pl_newname_input.clear();
117− app.reset_cursor();
118− app.inputmode = InputMode::Normal;
119− }
120− KeyCode::Char(to_insert) => {
121− app.enter_char(to_insert);
122− }
123− KeyCode::Enter => {
124− app.conn.conn.pl_rename(
125− app.pl_list.list.get(app.pl_list.index).unwrap(),
126− &app.pl_newname_input,
127− )?;
128− app.pl_list.list = App::get_playlist(&mut app.conn.conn)?;
129− app.pl_newname_input.clear();
130− app.reset_cursor();
131− app.inputmode = InputMode::Normal;
132− }
133−
134− KeyCode::Backspace => {
135− app.delete_char();
136− }
137−
138− KeyCode::Left => {
139− app.move_cursor_left();
140− }
141−
142− KeyCode::Right => {
143− app.move_cursor_right();
144− }
145−
146− _ => {}
147− }
148− // Playlist popup keybinds
149− //
150− // Keybind for when the "append to playlist" popup is visible
151− } else if app.playlist_popup {
152− match key_event.code {
153− KeyCode::Char('q') | KeyCode::Esc => {
154− app.playlist_popup = false;
155− }
156−
157− KeyCode::Char('j') | KeyCode::Down => app.append_list.next(),
158− KeyCode::Char('k') | KeyCode::Up => app.append_list.prev(),
159−
160− KeyCode::Enter => {
161− let pl_index = app.append_list.index;
162− let pl_name = &app.append_list.list.get(pl_index).unwrap();
163−
164− let s_index: usize;
165− match app.selected_tab {
166− SelectedTab::Queue => {
167− s_index = app.queue_list.index;
168− let short_path = &app.queue_list.list.get(s_index).unwrap().file;
169−
170− if let Some(full_path) = app.conn.get_full_path(short_path) {
171− let song = app.conn.get_song_with_only_filename(&full_path);
172−
173− if *pl_name == "Current Playlist" {
174− app.conn.conn.push(&song)?;
175− app.update_queue();
176− } else {
177− app.conn.add_to_playlist(pl_name, &song)?;
178− }
179− }
180− }
181−
182− SelectedTab::DirectoryBrowser => {
183− let (t, f) = app.browser.filetree.get(app.browser.selected).unwrap();
184− if t == "file" {
185− let short_path = f;
186− if let Some(full_path) = app.conn.get_full_path(short_path) {
187− let song = app.conn.get_song_with_only_filename(&full_path);
188−
189− if *pl_name == "Current Playlist" {
190− app.conn.conn.push(&song)?;
191− app.update_queue();
192− } else {
193− app.conn.add_to_playlist(pl_name, &song)?;
194− }
195− }
196− } else if t == "directory" {
197− for (t, f) in app.conn.conn.listfiles(f)?.iter() {
198− // dir_vec.push((t, f));
199− if t == "file"
200− && Path::new(&f).has_extension(&[
201− "mp3", "ogg", "flac", "m4a", "wav", "aac", "opus", "ape",
202− "wma", "mpc", "aiff", "dff", "mp2", "mka",
203− ])
204− {
205− let full_path = app.conn.get_full_path(f).unwrap_or_default();
206− let song = app.conn.get_song_with_only_filename(&full_path);
207− if *pl_name == "Current Playlist" {
208− app.conn.conn.push(&song)?;
209− } else {
210− app.conn.add_to_playlist(pl_name, &song)?;
211− }
212− }
213− }
214− }
215− }
216− _ => {}
217− }
218−
219− // hide the playlist popup
220− app.playlist_popup = false;
221− app.append_list.index = 0;
222− }
223− _ => {}
224− }
225− } else {
226− // Global keymaps
227− //
228− // Keymaps related to all the tabs
229− match key_event.code {
230− // Quit
231− KeyCode::Char('q') => app.quit(),
232− KeyCode::Char('c') | KeyCode::Char('C') => {
233− if key_event.modifiers == KeyModifiers::CONTROL {
234− app.quit();
235− } else {
236− app.conn.conn.clear()?;
237− app.conn.update_status();
238− app.queue_list.list.clear();
239− app.queue_list.reset_index();
240− }
241− }
242−
243− // Go Up
244− KeyCode::Char('j') | KeyCode::Down => match app.selected_tab {
245− SelectedTab::DirectoryBrowser => app.browser.next(),
246− SelectedTab::Queue => app.queue_list.next(),
247− SelectedTab::Playlists => app.pl_list.next(),
248− },
249−
250− // Go down
251− KeyCode::Char('k') | KeyCode::Up => match app.selected_tab {
252− SelectedTab::DirectoryBrowser => app.browser.prev(),
253− SelectedTab::Queue => app.queue_list.prev(),
254− SelectedTab::Playlists => app.pl_list.prev(),
255− },
256−
257− // Next directory
258− KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => {
259− // app.update_queue();
260−
261− match app.selected_tab {
262− SelectedTab::DirectoryBrowser => {
263− app.handle_enter()?;
264− }
265−
266− SelectedTab::Queue => {
267− app.conn.conn.switch(app.queue_list.index as u32)?;
268− }
269−
270− SelectedTab::Playlists => {
271− app.conn
272− .load_playlist(app.pl_list.list.get(app.pl_list.index).unwrap())?;
273− }
274− }
275− app.conn.update_status();
276− }
277−
278− // head back to previous directory
279− KeyCode::Char('h') | KeyCode::Left => match app.selected_tab {
280− SelectedTab::DirectoryBrowser => {
281− app.browser.handle_go_back(&mut app.conn)?;
282− }
283− SelectedTab::Queue => {}
284− SelectedTab::Playlists => {}
285− },
286−
287− // Playback controls
288− // Toggle Pause
289− KeyCode::Char('p') => app.conn.toggle_pause(),
290−
291− // Pause
292− KeyCode::Char('s') => app.conn.pause(),
293−
294− // Toggle rpeat
295− KeyCode::Char('r') => {
296− app.conn.toggle_repeat();
297− app.conn.update_status();
298− }
299−
300− // Toggle random
301− KeyCode::Char('z') => {
302− app.conn.toggle_random();
303− app.conn.update_status();
304− }
305−
306− // Dmenu prompt
307− KeyCode::Char('D') => app.conn.play_dmenu()?,
308−
309− // add to queue
310− KeyCode::Char('a') => app.playlist_popup = true,
311−
312− // Fast forward
313− KeyCode::Char('f') => {
314− let place = app.conn.conn.status().unwrap().song.unwrap().pos;
315− let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
316− let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
317− app.conn.conn.seek(place, pos)?;
318− }
319−
320− // backward
321− KeyCode::Char('b') => {
322− let place = app.conn.conn.status().unwrap().song.unwrap().pos;
323− let (pos, _) = app.conn.conn.status().unwrap().time.unwrap();
324− let pos = Duration::from_secs(pos.as_secs().wrapping_add(2));
325− app.conn.conn.seek(place, pos)?;
326− }
327−
328− // Cycle through tabs
329− KeyCode::Tab => {
330− app.cycle_tabls();
331− }
332−
333− // Directory browser tab
334− KeyCode::Char('1') => {
335− app.selected_tab = SelectedTab::Queue;
336− }
337−
338− // Playing queue tab
339− KeyCode::Char('2') => {
340− app.selected_tab = SelectedTab::DirectoryBrowser;
341− }
342−
343− // Playlists tab
344− KeyCode::Char('3') => {
345− app.selected_tab = SelectedTab::Playlists;
346− }
347−
348− // Play next song
349− KeyCode::Char('>') => {
350− if !app.queue_list.list.is_empty() {
351− app.conn.conn.next()?;
352− app.update_queue();
353− }
354− }
355−
356− // Play previous song
357− KeyCode::Char('<') => {
358− if !app.queue_list.list.is_empty() {
359− app.conn.conn.prev()?;
360− app.update_queue();
361− }
362− }
363−
364− // Volume controls
365− KeyCode::Char('=') => {
366− app.conn.inc_volume(2);
367− app.conn.update_status();
368− }
369−
370− KeyCode::Char('-') => {
371− app.conn.dec_volume(2);
372− app.conn.update_status();
373− }
374−
375− // Delete highlighted song from the queue
376− KeyCode::Char('d') => {
377− if app.queue_list.index >= app.queue_list.list.len() - 1
378− && app.queue_list.index != 0
379− {
380− app.queue_list.index -= 1;
381− }
382−
383− app.conn.conn.delete(app.queue_list.index as u32)?;
384− app.update_queue();
385− }
386−
387− // Update MPD database
388− KeyCode::Char('U') => {
389− app.conn.conn.rescan()?;
390− app.browser.update_directory(&mut app.conn)?;
391− }
392−
393− // Search for songs
394− KeyCode::Char('/') => {
395− if app.inputmode == InputMode::Normal {
396− app.inputmode = InputMode::Editing;
397− } else {
398− app.inputmode = InputMode::Normal;
399− }
400− }
401−
402− // Remove from Current Playlsit
403− KeyCode::Char(' ') | KeyCode::Backspace => {
404− app.handle_add_or_remove_from_current_playlist()?;
405− }
406−
407− // go to top of list
408− KeyCode::Char('g') => match app.selected_tab {
409− SelectedTab::Queue => app.queue_list.index = 0,
410− SelectedTab::DirectoryBrowser => app.browser.selected = 0,
411− SelectedTab::Playlists => app.pl_list.index = 0,
412− },
413−
414− // go to bottom of list
415− KeyCode::Char('G') => match app.selected_tab {
416− SelectedTab::Queue => app.queue_list.index = app.queue_list.list.len() - 1,
417− SelectedTab::DirectoryBrowser => {
418− app.browser.selected = app.browser.filetree.len() - 1
419− }
420− SelectedTab::Playlists => app.pl_list.index = app.pl_list.list.len() - 1,
421− },
422−
423− // Change playlist name
424− KeyCode::Char('e') => app.change_playlist_name()?,
425− _ => {}
426− }
427− }
428− Ok(())
429−}
M src/lib.rs View
@@ -16,11 +16,8 @@ pub mod list;
1616 /// File Browser
1717 pub mod browser;
1818
19−/// Event Handler
19+/// Event Handler/ keymaps
2020 pub mod event;
2121
22−/// KeyEvent Handler
23−pub mod handler;
24−
2522 /// Application
2623 pub mod app;
M src/main.rs View
@@ -6,9 +6,9 @@ use rmptui::app::AppResult;
66 use rmptui::cli::Args;
77 use rmptui::cli::Command;
88 use rmptui::connection::Connection;
9−use rmptui::event::Event;
10−use rmptui::event::EventHandler;
11−use rmptui::handler;
9+use rmptui::event::event::Event;
10+use rmptui::event::event::EventHandler;
11+use rmptui::event::handler;
1212 use rmptui::tui;
1313 use std::env;
1414 use std::io;
M src/tui.rs View
@@ -5,7 +5,7 @@ use crossterm::terminal::{self, *};
55 use std::panic;
66
77 use crate::app::{App, AppResult};
8−use crate::event::EventHandler;
8+use crate::event::event::EventHandler;
99
1010 pub type CrosstermTerminal = ratatui::Terminal<ratatui::backend::CrosstermBackend<std::io::Stderr>>;
1111