rmptuib29846f
abide by the lord clippy
+70 −68
7 files changed
- M src/app.rs +15 −15
- M src/browser.rs +11 −3
- M src/connection.rs +9 −9
- M src/handler.rs +17 −14
- M src/list.rs +8 −21
- M src/queue.rs +8 −4
- M src/ui.rs +2 −2
| @@ -16,10 +16,10 @@ pub struct App { | |||
| 16 | 16 | /// check if app is running | |
| 17 | 17 | pub running: bool, | |
| 18 | 18 | pub conn: Connection, | |
| 19 | − | pub browser: FileBrowser, // Directory browser | |
| 20 | − | pub queue_list: Queue, // Stores the current playing queue | |
| 21 | − | pub pl_list: ContentList<String>, // Stores list of playlists | |
| 22 | − | pub selected_tab: SelectedTab, // Used to switch between tabs | |
| 19 | + | pub browser: FileBrowser, // Directory browser | |
| 20 | + | pub queue_list: Queue, // Stores the current playing queue | |
| 21 | + | pub pl_list: ContentList<String>, // Stores list of playlists | |
| 22 | + | pub selected_tab: SelectedTab, // Used to switch between tabs | |
| 23 | 23 | ||
| 24 | 24 | // Search | |
| 25 | 25 | pub inputmode: InputMode, // Defines input mode, Normal or Search | |
| @@ -34,8 +34,6 @@ pub struct App { | |||
| 34 | 34 | pub append_list: ContentList<String>, | |
| 35 | 35 | } | |
| 36 | 36 | ||
| 37 | − | ||
| 38 | − | ||
| 39 | 37 | #[derive(Debug, PartialEq, Clone)] | |
| 40 | 38 | pub enum SelectedTab { | |
| 41 | 39 | DirectoryBrowser, | |
| @@ -135,7 +133,7 @@ impl App { | |||
| 135 | 133 | } | |
| 136 | 134 | ||
| 137 | 135 | if !status { | |
| 138 | − | if let Some(full_path) = &self.conn.get_full_path(&file) { | |
| 136 | + | if let Some(full_path) = &self.conn.get_full_path(file) { | |
| 139 | 137 | let song = self.conn.get_song_with_only_filename(full_path); | |
| 140 | 138 | self.conn.conn.push(&song)?; | |
| 141 | 139 | } | |
| @@ -147,14 +145,15 @@ impl App { | |||
| 147 | 145 | } | |
| 148 | 146 | ||
| 149 | 147 | SelectedTab::Queue => { | |
| 150 | − | if self.queue_list.list.len() == 0 { | |
| 148 | + | if self.queue_list.list.is_empty() { | |
| 151 | 149 | return Ok(()); | |
| 152 | 150 | } | |
| 153 | 151 | let file = self | |
| 154 | 152 | .queue_list | |
| 155 | 153 | .list | |
| 156 | 154 | .get(self.queue_list.index) | |
| 157 | − | .unwrap().file | |
| 155 | + | .unwrap() | |
| 156 | + | .file | |
| 158 | 157 | .to_string(); | |
| 159 | 158 | ||
| 160 | 159 | for (i, song) in self.queue_list.list.clone().iter().enumerate() { | |
| @@ -207,7 +206,11 @@ impl App { | |||
| 207 | 206 | // .get(0) | |
| 208 | 207 | // .unwrap() | |
| 209 | 208 | // .clone(); | |
| 210 | − | let index = self.queue_list.list.iter().position(|x| x.file.contains(path)); | |
| 209 | + | let index = self | |
| 210 | + | .queue_list | |
| 211 | + | .list | |
| 212 | + | .iter() | |
| 213 | + | .position(|x| x.file.contains(path)); | |
| 211 | 214 | ||
| 212 | 215 | if index.is_some() { | |
| 213 | 216 | self.conn.conn.switch(index.unwrap() as u32)?; | |
| @@ -349,11 +352,8 @@ impl App { | |||
| 349 | 352 | } | |
| 350 | 353 | ||
| 351 | 354 | pub fn change_playlist_name(&mut self) -> AppResult<()> { | |
| 352 | − | match self.selected_tab { | |
| 353 | − | SelectedTab::Playlists => { | |
| 354 | − | self.inputmode = InputMode::PlaylistRename; | |
| 355 | − | } | |
| 356 | − | _ => {} | |
| 355 | + | if self.selected_tab == SelectedTab::Playlists { | |
| 356 | + | self.inputmode = InputMode::PlaylistRename; | |
| 357 | 357 | } | |
| 358 | 358 | Ok(()) | |
| 359 | 359 | } | |
| @@ -23,7 +23,7 @@ pub trait FileExtension { | |||
| 23 | 23 | ||
| 24 | 24 | impl<P: AsRef<Path>> FileExtension for P { | |
| 25 | 25 | fn has_extension<S: AsRef<str>>(&self, extensions: &[S]) -> bool { | |
| 26 | − | if let Some(ref extension) = self.as_ref().extension().and_then(OsStr::to_str) { | |
| 26 | + | if let Some(extension) = self.as_ref().extension().and_then(OsStr::to_str) { | |
| 27 | 27 | return extensions | |
| 28 | 28 | .iter() | |
| 29 | 29 | .any(|x| x.as_ref().eq_ignore_ascii_case(extension)); | |
| @@ -84,7 +84,7 @@ impl FileBrowser { | |||
| 84 | 84 | }] | |
| 85 | 85 | }); | |
| 86 | 86 | ||
| 87 | − | self.songs.push(v.get(0).unwrap().clone()); | |
| 87 | + | self.songs.push(v.first().unwrap().clone()); | |
| 88 | 88 | } else { | |
| 89 | 89 | let v = Song { | |
| 90 | 90 | file: "".to_string(), | |
| @@ -126,7 +126,7 @@ impl FileBrowser { | |||
| 126 | 126 | /// handles going back event | |
| 127 | 127 | pub fn handle_go_back(&mut self, conn: &mut Connection) -> AppResult<()> { | |
| 128 | 128 | if self.prev_path != "." { | |
| 129 | − | let r = self.path.rfind("/").unwrap(); | |
| 129 | + | let r = self.path.rfind('/').unwrap(); | |
| 130 | 130 | self.path = self.path.as_str()[..r].to_string(); | |
| 131 | 131 | self.update_directory(conn)?; | |
| 132 | 132 | } else { | |
| @@ -138,3 +138,11 @@ impl FileBrowser { | |||
| 138 | 138 | Ok(()) | |
| 139 | 139 | } | |
| 140 | 140 | } | |
| 141 | + | ||
| 142 | + | ||
| 143 | + | ||
| 144 | + | impl Default for FileBrowser { | |
| 145 | + | fn default() -> Self { | |
| 146 | + | Self::new() | |
| 147 | + | } | |
| 148 | + | } | |
| @@ -47,7 +47,7 @@ impl Connection { | |||
| 47 | 47 | let current_song = conn | |
| 48 | 48 | .currentsong() | |
| 49 | 49 | .unwrap_or_else(|_| Some(empty_song.clone())) | |
| 50 | − | .unwrap_or_else(|| empty_song); | |
| 50 | + | .unwrap_or(empty_song); | |
| 51 | 51 | Ok(Self { | |
| 52 | 52 | conn, | |
| 53 | 53 | songs_filenames, | |
| @@ -63,10 +63,10 @@ impl Connection { | |||
| 63 | 63 | ||
| 64 | 64 | /// Fzf prompt for selecting song | |
| 65 | 65 | pub fn play_fzf(&mut self) -> Result<()> { | |
| 66 | − | is_installed("fzf").map_err(|ex| ex)?; | |
| 66 | + | is_installed("fzf")?; | |
| 67 | 67 | let ss = &self.songs_filenames; | |
| 68 | 68 | let fzf_choice = rust_fzf::select(ss.clone(), Vec::new()).unwrap(); | |
| 69 | − | let index = get_choice_index(&self.songs_filenames, fzf_choice.get(0).unwrap()); | |
| 69 | + | let index = get_choice_index(&self.songs_filenames, fzf_choice.first().unwrap()); | |
| 70 | 70 | let song = self.get_song_with_only_filename(ss.get(index).unwrap()); | |
| 71 | 71 | self.push(&song)?; | |
| 72 | 72 | ||
| @@ -75,7 +75,7 @@ impl Connection { | |||
| 75 | 75 | ||
| 76 | 76 | /// Dmenu prompt for selecting songs | |
| 77 | 77 | pub fn play_dmenu(&mut self) -> Result<()> { | |
| 78 | − | is_installed("dmenu").map_err(|ex| ex)?; | |
| 78 | + | is_installed("dmenu")?; | |
| 79 | 79 | let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect(); | |
| 80 | 80 | let op = dmenu!(iter &ss; args "-p", "Choose a song: ", "-l", "30"); | |
| 81 | 81 | let index = get_choice_index(&self.songs_filenames, &op); | |
| @@ -92,7 +92,7 @@ impl Connection { | |||
| 92 | 92 | .conn | |
| 93 | 93 | .currentsong() | |
| 94 | 94 | .unwrap_or_else(|_| Some(empty_song.clone())) | |
| 95 | − | .unwrap_or_else(|| empty_song); | |
| 95 | + | .unwrap_or(empty_song); | |
| 96 | 96 | ||
| 97 | 97 | // Playback State | |
| 98 | 98 | match status.state { | |
| @@ -205,12 +205,12 @@ impl Connection { | |||
| 205 | 205 | pub fn now_playing(&mut self) -> Result<Option<String>> { | |
| 206 | 206 | if let Some(s) = &self.current_song.title { | |
| 207 | 207 | if let Some(a) = &self.current_song.artist { | |
| 208 | − | return Ok(Some(format!("\"{}\" By {}", s, a))); | |
| 208 | + | Ok(Some(format!("\"{}\" By {}", s, a))) | |
| 209 | 209 | } else { | |
| 210 | − | return Ok(Some(s.to_string())); | |
| 210 | + | Ok(Some(s.to_string())) | |
| 211 | 211 | } | |
| 212 | 212 | } else { | |
| 213 | − | return Ok(Some(self.current_song.file.clone())); | |
| 213 | + | Ok(Some(self.current_song.file.clone())) | |
| 214 | 214 | } | |
| 215 | 215 | } | |
| 216 | 216 | ||
| @@ -262,7 +262,7 @@ impl Connection { | |||
| 262 | 262 | } | |
| 263 | 263 | ||
| 264 | 264 | /// Gets the index of the string from the Vector | |
| 265 | − | fn get_choice_index(ss: &Vec<String>, selection: &str) -> usize { | |
| 265 | + | fn get_choice_index(ss: &[String], selection: &str) -> usize { | |
| 266 | 266 | let mut choice: usize = 0; | |
| 267 | 267 | if let Some(index) = ss.iter().position(|s| s == selection) { | |
| 268 | 268 | choice = index; | |
| @@ -23,7 +23,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 23 | 23 | let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>(); | |
| 24 | 24 | ||
| 25 | 25 | for (i, (_, item)) in app.browser.filetree.iter().enumerate() { | |
| 26 | − | if item.contains(res.get(0).unwrap()) { | |
| 26 | + | if item.contains(res.first().unwrap()) { | |
| 27 | 27 | app.browser.selected = i; | |
| 28 | 28 | } | |
| 29 | 29 | } | |
| @@ -40,7 +40,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 40 | 40 | let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>(); | |
| 41 | 41 | ||
| 42 | 42 | for (i, item) in app.queue_list.list.iter().enumerate() { | |
| 43 | − | if item.file.contains(res.get(0).unwrap()) { | |
| 43 | + | if item.file.contains(res.first().unwrap()) { | |
| 44 | 44 | app.queue_list.index = i; | |
| 45 | 45 | } | |
| 46 | 46 | } | |
| @@ -57,7 +57,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 57 | 57 | let res = res.iter().map(|(x, _)| *x).collect::<Vec<&str>>(); | |
| 58 | 58 | ||
| 59 | 59 | for (i, item) in app.pl_list.list.iter().enumerate() { | |
| 60 | − | if item.contains(res.get(0).unwrap()) { | |
| 60 | + | if item.contains(res.first().unwrap()) { | |
| 61 | 61 | app.pl_list.index = i; | |
| 62 | 62 | } | |
| 63 | 63 | } | |
| @@ -83,7 +83,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 83 | 83 | .collect::<Vec<&str>>(); | |
| 84 | 84 | ||
| 85 | 85 | let res: Vec<(&str, f32)> = fuzzy_search_sorted(&app.search_input, &list); | |
| 86 | − | let (res, _) = res.get(0).unwrap(); | |
| 86 | + | let (res, _) = res.first().unwrap(); | |
| 87 | 87 | ||
| 88 | 88 | for (i, (_, item)) in app.browser.filetree.iter().enumerate() { | |
| 89 | 89 | if item.contains(res) { | |
| @@ -121,7 +121,10 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 121 | 121 | app.enter_char(to_insert); | |
| 122 | 122 | } | |
| 123 | 123 | KeyCode::Enter => { | |
| 124 | − | app.conn.conn.pl_rename(app.pl_list.list.get(app.pl_list.index).unwrap(), &app.pl_newname_input)?; | |
| 124 | + | app.conn.conn.pl_rename( | |
| 125 | + | app.pl_list.list.get(app.pl_list.index).unwrap(), | |
| 126 | + | &app.pl_newname_input, | |
| 127 | + | )?; | |
| 125 | 128 | app.pl_list.list = App::get_playlist(&mut app.conn.conn)?; | |
| 126 | 129 | app.pl_newname_input.clear(); | |
| 127 | 130 | app.reset_cursor(); | |
| @@ -164,10 +167,10 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 164 | 167 | s_index = app.queue_list.index; | |
| 165 | 168 | let short_path = &app.queue_list.list.get(s_index).unwrap().file; | |
| 166 | 169 | ||
| 167 | − | if let Some(full_path) = app.conn.get_full_path(&short_path) { | |
| 170 | + | if let Some(full_path) = app.conn.get_full_path(short_path) { | |
| 168 | 171 | let song = app.conn.get_song_with_only_filename(&full_path); | |
| 169 | 172 | ||
| 170 | − | if pl_name.to_string() == "Current Playlist" { | |
| 173 | + | if *pl_name == "Current Playlist" { | |
| 171 | 174 | app.conn.conn.push(&song)?; | |
| 172 | 175 | app.update_queue(); | |
| 173 | 176 | } else { | |
| @@ -183,7 +186,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 183 | 186 | if let Some(full_path) = app.conn.get_full_path(short_path) { | |
| 184 | 187 | let song = app.conn.get_song_with_only_filename(&full_path); | |
| 185 | 188 | ||
| 186 | − | if pl_name.to_string() == "Current Playlist" { | |
| 189 | + | if *pl_name == "Current Playlist" { | |
| 187 | 190 | app.conn.conn.push(&song)?; | |
| 188 | 191 | app.update_queue(); | |
| 189 | 192 | } else { | |
| @@ -199,9 +202,9 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 199 | 202 | "wma", "mpc", "aiff", "dff", "mp2", "mka", | |
| 200 | 203 | ]) | |
| 201 | 204 | { | |
| 202 | − | let full_path = app.conn.get_full_path(&f).unwrap_or_default(); | |
| 205 | + | let full_path = app.conn.get_full_path(f).unwrap_or_default(); | |
| 203 | 206 | let song = app.conn.get_song_with_only_filename(&full_path); | |
| 204 | − | if pl_name.to_string() == "Current Playlist" { | |
| 207 | + | if *pl_name == "Current Playlist" { | |
| 205 | 208 | app.conn.conn.push(&song)?; | |
| 206 | 209 | } else { | |
| 207 | 210 | app.conn.add_to_playlist(pl_name, &song)?; | |
| @@ -371,10 +374,10 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 371 | 374 | ||
| 372 | 375 | // Delete highlighted song from the queue | |
| 373 | 376 | KeyCode::Char('d') => { | |
| 374 | − | if app.queue_list.index >= app.queue_list.list.len() - 1 { | |
| 375 | − | if app.queue_list.index != 0 { | |
| 376 | − | app.queue_list.index -= 1; | |
| 377 | − | } | |
| 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; | |
| 378 | 381 | } | |
| 379 | 382 | ||
| 380 | 383 | app.conn.conn.delete(app.queue_list.index as u32)?; | |
| @@ -15,20 +15,9 @@ impl<T> ContentList<T> { | |||
| 15 | 15 | // Go to next item in list | |
| 16 | 16 | pub fn next(&mut self) { | |
| 17 | 17 | let len = self.list.len(); | |
| 18 | − | if len != 0 { | |
| 19 | − | if self.index < len - 1 { | |
| 20 | − | self.index += 1; | |
| 21 | − | } | |
| 18 | + | if len != 0 && self.index < len - 1 { | |
| 19 | + | self.index += 1; | |
| 22 | 20 | } | |
| 23 | − | ||
| 24 | − | // let len = self.list.len(); | |
| 25 | − | // if len != 0 { | |
| 26 | − | // if self.index == self.list.len() - 1 { | |
| 27 | − | // self.index = 0; | |
| 28 | − | // } else { | |
| 29 | − | // self.index += 1; | |
| 30 | − | // } | |
| 31 | − | // } | |
| 32 | 21 | } | |
| 33 | 22 | ||
| 34 | 23 | /// Go to previous item in list | |
| @@ -36,17 +25,15 @@ impl<T> ContentList<T> { | |||
| 36 | 25 | if self.index != 0 { | |
| 37 | 26 | self.index -= 1; | |
| 38 | 27 | } | |
| 39 | − | // if self.index == 0 { | |
| 40 | − | // let len = self.list.len(); | |
| 41 | − | // if len != 0 { | |
| 42 | − | // self.index = len - 1; | |
| 43 | − | // } | |
| 44 | − | // } else { | |
| 45 | − | // self.index -= 1; | |
| 46 | − | // } | |
| 47 | 28 | } | |
| 48 | 29 | ||
| 49 | 30 | pub fn reset_index(&mut self) { | |
| 50 | 31 | self.index = 0; | |
| 51 | 32 | } | |
| 52 | 33 | } | |
| 34 | + | ||
| 35 | + | impl<T> Default for ContentList<T> { | |
| 36 | + | fn default() -> Self { | |
| 37 | + | Self::new() | |
| 38 | + | } | |
| 39 | + | } | |
| @@ -17,10 +17,8 @@ impl Queue { | |||
| 17 | 17 | // Go to next item in list | |
| 18 | 18 | pub fn next(&mut self) { | |
| 19 | 19 | let len = self.list.len(); | |
| 20 | − | if len != 0 { | |
| 21 | − | if self.index < len - 1 { | |
| 22 | − | self.index += 1; | |
| 23 | − | } | |
| 20 | + | if len != 0 && self.index < len - 1 { | |
| 21 | + | self.index += 1; | |
| 24 | 22 | } | |
| 25 | 23 | } | |
| 26 | 24 | ||
| @@ -35,3 +33,9 @@ impl Queue { | |||
| 35 | 33 | self.index = 0; | |
| 36 | 34 | } | |
| 37 | 35 | } | |
| 36 | + | ||
| 37 | + | impl Default for Queue { | |
| 38 | + | fn default() -> Self { | |
| 39 | + | Self::new() | |
| 40 | + | } | |
| 41 | + | } | |
| @@ -297,10 +297,10 @@ fn draw_progress_bar(frame: &mut Frame, app: &mut App, size: Rect) { | |||
| 297 | 297 | ||
| 298 | 298 | // Define the title | |
| 299 | 299 | let title = Block::default() | |
| 300 | − | .title(Title::from(format!("{}", state).red().bold())) | |
| 300 | + | .title(Title::from(state.red().bold())) | |
| 301 | 301 | .title(Title::from(song.green().bold())) | |
| 302 | 302 | .title(Title::from(duration.cyan().bold()).alignment(Alignment::Right)) | |
| 303 | − | .title(Title::from(format!("{}", modes_bottom)).position(block::Position::Bottom)) | |
| 303 | + | .title(Title::from(modes_bottom).position(block::Position::Bottom)) | |
| 304 | 304 | .borders(Borders::ALL); | |
| 305 | 305 | ||
| 306 | 306 | let progress_bar = LineGauge::default() | |