rmptui87bb8e9
remove redundant functions and add comments
+34 −34
5 files changed
- M src/app.rs +14 −22
- M src/browser.rs +4 −1
- M src/connection.rs +3 −2
- D src/error.rs +0 −6
- M src/handler.rs +13 −3
| @@ -15,17 +15,18 @@ pub struct App { | |||
| 15 | 15 | /// check if app is running | |
| 16 | 16 | pub running: bool, | |
| 17 | 17 | pub conn: Connection, | |
| 18 | − | pub queue_list: ContentList<String>, | |
| 19 | − | pub pl_list: ContentList<String>, | |
| 20 | − | pub selected_tab: SelectedTab, | |
| 21 | − | pub browser: FileBrowser, | |
| 18 | + | pub browser: FileBrowser, // Directory browser | |
| 19 | + | pub queue_list: ContentList<String>, // Stores the current playing queue | |
| 20 | + | pub pl_list: ContentList<String>, // Stores list of playlists | |
| 21 | + | pub selected_tab: SelectedTab, // Used to switch between tabs | |
| 22 | 22 | ||
| 23 | 23 | // Search | |
| 24 | − | pub inputmode: InputMode, | |
| 25 | − | pub search_input: String, | |
| 26 | − | pub cursor_position: usize, | |
| 24 | + | pub inputmode: InputMode, // Defines input mode, Normal or Search | |
| 25 | + | pub search_input: String, // Stores the userinput to be searched | |
| 26 | + | pub cursor_position: usize, // Stores the cursor position | |
| 27 | 27 | ||
| 28 | − | // add to playlists | |
| 28 | + | // playlist variables | |
| 29 | + | // used to show playlist popup | |
| 29 | 30 | pub playlist_popup: bool, | |
| 30 | 31 | pub append_list: ContentList<String>, | |
| 31 | 32 | } | |
| @@ -90,12 +91,13 @@ impl App { | |||
| 90 | 91 | }); | |
| 91 | 92 | } | |
| 92 | 93 | ||
| 94 | + | // Rescan the queue into queue_list | |
| 93 | 95 | pub fn update_queue(&mut self) { | |
| 94 | 96 | self.queue_list.list.clear(); | |
| 95 | 97 | Self::get_queue(&mut self.conn, &mut self.queue_list.list); | |
| 96 | 98 | } | |
| 97 | 99 | ||
| 98 | − | pub fn get_playlist(conn: &mut Client) -> AppResult<Vec<String>> { | |
| 100 | + | fn get_playlist(conn: &mut Client) -> AppResult<Vec<String>> { | |
| 99 | 101 | let list: Vec<String> = conn.playlists()?.iter().map(|p| p.clone().name).collect(); | |
| 100 | 102 | Ok(list) | |
| 101 | 103 | } | |
| @@ -110,11 +112,7 @@ impl App { | |||
| 110 | 112 | Ok(list) | |
| 111 | 113 | } | |
| 112 | 114 | ||
| 113 | − | pub fn update_playlist(&mut self) -> AppResult<()> { | |
| 114 | − | Self::get_playlist(&mut self.conn.conn)?; | |
| 115 | − | Ok(()) | |
| 116 | − | } | |
| 117 | − | ||
| 115 | + | /// Handles the <Space> event key | |
| 118 | 116 | pub fn handle_add_or_remove_from_current_playlist(&mut self) -> AppResult<()> { | |
| 119 | 117 | match self.selected_tab { | |
| 120 | 118 | SelectedTab::DirectoryBrowser => { | |
| @@ -170,6 +168,7 @@ impl App { | |||
| 170 | 168 | Ok(()) | |
| 171 | 169 | } | |
| 172 | 170 | ||
| 171 | + | /// Cycle through tabs | |
| 173 | 172 | pub fn cycle_tabls(&mut self) { | |
| 174 | 173 | self.selected_tab = match self.selected_tab { | |
| 175 | 174 | SelectedTab::Queue => SelectedTab::DirectoryBrowser, | |
| @@ -178,6 +177,7 @@ impl App { | |||
| 178 | 177 | }; | |
| 179 | 178 | } | |
| 180 | 179 | ||
| 180 | + | /// handles the Enter event on the directory browser | |
| 181 | 181 | pub fn handle_enter(&mut self) -> AppResult<()> { | |
| 182 | 182 | let browser = &mut self.browser; | |
| 183 | 183 | let (t, path) = browser.filetree.get(browser.selected).unwrap(); | |
| @@ -218,14 +218,6 @@ impl App { | |||
| 218 | 218 | Ok(()) | |
| 219 | 219 | } | |
| 220 | 220 | ||
| 221 | − | pub fn search_song(&mut self) -> AppResult<()> { | |
| 222 | − | if let Some(filename) = self.conn.get_full_path(&self.search_input) { | |
| 223 | − | let song = self.conn.get_song_with_only_filename(&filename); | |
| 224 | − | self.conn.push(&song)?; | |
| 225 | − | } | |
| 226 | − | Ok(()) | |
| 227 | − | } | |
| 228 | − | ||
| 229 | 221 | // Cursor movements | |
| 230 | 222 | pub fn move_cursor_left(&mut self) { | |
| 231 | 223 | let cursor_moved_left = self.cursor_position.saturating_sub(1); | |
| @@ -6,6 +6,7 @@ use mpd::Song; | |||
| 6 | 6 | use crate::{app::AppResult, connection::Connection}; | |
| 7 | 7 | ||
| 8 | 8 | #[derive(Debug)] | |
| 9 | + | /// struct for working with directory browser tab in rmptui | |
| 9 | 10 | pub struct FileBrowser { | |
| 10 | 11 | pub filetree: Vec<(String, String)>, | |
| 11 | 12 | pub selected: usize, | |
| @@ -61,9 +62,10 @@ impl FileBrowser { | |||
| 61 | 62 | } | |
| 62 | 63 | } | |
| 63 | 64 | ||
| 65 | + | ||
| 66 | + | // Add metadata | |
| 64 | 67 | dir_vec.extend(file_vec); | |
| 65 | 68 | self.filetree = dir_vec; | |
| 66 | − | ||
| 67 | 69 | self.songs.clear(); | |
| 68 | 70 | for (t, song) in self.filetree.iter() { | |
| 69 | 71 | if t == "file" { | |
| @@ -121,6 +123,7 @@ impl FileBrowser { | |||
| 121 | 123 | // } | |
| 122 | 124 | } | |
| 123 | 125 | ||
| 126 | + | /// handles going back event | |
| 124 | 127 | pub fn handle_go_back(&mut self, conn: &mut Connection) -> AppResult<()> { | |
| 125 | 128 | if self.prev_path != "." { | |
| 126 | 129 | let r = self.path.rfind("/").unwrap(); | |
| @@ -8,6 +8,7 @@ pub type Result<T> = core::result::Result<T, Error>; | |||
| 8 | 8 | pub type Error = Box<dyn std::error::Error>; | |
| 9 | 9 | ||
| 10 | 10 | #[derive(Debug)] | |
| 11 | + | /// struct storing the mpd Client related stuff | |
| 11 | 12 | pub struct Connection { | |
| 12 | 13 | pub conn: Client, | |
| 13 | 14 | pub songs_filenames: Vec<String>, | |
| @@ -26,7 +27,7 @@ impl Connection { | |||
| 26 | 27 | let mut conn = Client::connect(addrs).unwrap(); | |
| 27 | 28 | ||
| 28 | 29 | let empty_song = Song { | |
| 29 | − | file: "No Song playing or in Queue".to_string(), | |
| 30 | + | file: "No Song playing or in Queue".to_string(), | |
| 30 | 31 | ..Default::default() | |
| 31 | 32 | }; | |
| 32 | 33 | ||
| @@ -86,7 +87,7 @@ impl Connection { | |||
| 86 | 87 | /// Update status | |
| 87 | 88 | pub fn update_status(&mut self) { | |
| 88 | 89 | let status = self.conn.status().unwrap(); | |
| 89 | − | let empty_song = self.get_song_with_only_filename("No Song playing or in Queue"); | |
| 90 | + | let empty_song = self.get_song_with_only_filename("No Song playing or in Queue"); | |
| 90 | 91 | let current_song = self | |
| 91 | 92 | .conn | |
| 92 | 93 | .currentsong() | |
| @@ -1,6 +0,0 @@ | |||
| 1 | − | #[derive(Debug)] | |
| 2 | − | pub enum Error { | |
| 3 | − | DmenuNotInstalled, | |
| 4 | − | FzfNotInstalled, | |
| 5 | − | } | |
| 6 | − | ||
| @@ -1,12 +1,11 @@ | |||
| 1 | 1 | use crate::browser::FileExtension; | |
| 2 | − | use std::{path::Path, time::Duration}; | |
| 3 | − | ||
| 4 | 2 | use crate::{ | |
| 5 | 3 | app::{App, AppResult, SelectedTab}, | |
| 6 | 4 | ui::InputMode, | |
| 7 | 5 | }; | |
| 8 | 6 | use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; | |
| 9 | 7 | use rust_fuzzy_search::{self, fuzzy_search_sorted}; | |
| 8 | + | use std::{path::Path, time::Duration}; | |
| 10 | 9 | ||
| 11 | 10 | pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |
| 12 | 11 | if app.inputmode == InputMode::Editing { | |
| @@ -65,6 +64,9 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 65 | 64 | } | |
| 66 | 65 | } | |
| 67 | 66 | ||
| 67 | + | // Keybind for searching | |
| 68 | + | // | |
| 69 | + | // Keybinds for when the search prompt is visible | |
| 68 | 70 | match key_event.code { | |
| 69 | 71 | KeyCode::Esc => { | |
| 70 | 72 | app.inputmode = InputMode::Normal; | |
| @@ -108,6 +110,10 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 108 | 110 | ||
| 109 | 111 | _ => {} | |
| 110 | 112 | } | |
| 113 | + | ||
| 114 | + | // Playlist popup keybinds | |
| 115 | + | // | |
| 116 | + | // Keybind for when the "append to playlist" popup is visible | |
| 111 | 117 | } else if app.playlist_popup { | |
| 112 | 118 | match key_event.code { | |
| 113 | 119 | KeyCode::Char('q') | KeyCode::Esc => { | |
| @@ -183,6 +189,9 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 183 | 189 | _ => {} | |
| 184 | 190 | } | |
| 185 | 191 | } else { | |
| 192 | + | // Global keymaps | |
| 193 | + | // | |
| 194 | + | // Keymaps related to all the tabs | |
| 186 | 195 | match key_event.code { | |
| 187 | 196 | // Quit | |
| 188 | 197 | KeyCode::Char('q') => app.quit(), | |
| @@ -343,7 +352,8 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 343 | 352 | ||
| 344 | 353 | // Update MPD database | |
| 345 | 354 | KeyCode::Char('U') => { | |
| 346 | − | app.conn.conn.update()?; | |
| 355 | + | app.conn.conn.rescan()?; | |
| 356 | + | app.browser.update_directory(&mut app.conn)?; | |
| 347 | 357 | } | |
| 348 | 358 | ||
| 349 | 359 | // Search for songs | |