rmptui6b66a44
fix queue deletion & make get_full_path return an option
+50 −32
3 files changed
- M src/app.rs +20 −9
- M src/connection.rs +19 −11
- M src/handler.rs +11 −12
| @@ -113,11 +113,11 @@ impl App { | |||
| 113 | 113 | Ok(()) | |
| 114 | 114 | } | |
| 115 | 115 | ||
| 116 | − | pub fn handle_remove_or_from_current_playlist(&mut self) -> AppResult<()> { | |
| 116 | + | pub fn handle_add_or_remove_from_current_playlist(&mut self) -> AppResult<()> { | |
| 117 | 117 | match self.selected_tab { | |
| 118 | 118 | SelectedTab::DirectoryBrowser => { | |
| 119 | 119 | let (_, file) = self.browser.filetree.get(self.browser.selected).unwrap(); | |
| 120 | − | self.browser.selected += 1; | |
| 120 | + | ||
| 121 | 121 | let mut status = false; | |
| 122 | 122 | for (i, song) in self.queue_list.list.clone().iter().enumerate() { | |
| 123 | 123 | if song.contains(file) { | |
| @@ -127,14 +127,21 @@ impl App { | |||
| 127 | 127 | } | |
| 128 | 128 | ||
| 129 | 129 | if !status { | |
| 130 | − | let song = self | |
| 131 | − | .conn | |
| 132 | − | .get_song_with_only_filename(&self.conn.get_full_path(&file).unwrap()); | |
| 133 | − | self.conn.conn.push(&song)?; | |
| 130 | + | if let Some(full_path) = &self.conn.get_full_path(&file) { | |
| 131 | + | let song = self.conn.get_song_with_only_filename(full_path); | |
| 132 | + | self.conn.conn.push(&song)?; | |
| 133 | + | } | |
| 134 | + | } | |
| 135 | + | ||
| 136 | + | if self.browser.selected != self.browser.filetree.len() - 1 { | |
| 137 | + | self.browser.selected += 1; | |
| 134 | 138 | } | |
| 135 | 139 | } | |
| 136 | 140 | ||
| 137 | 141 | SelectedTab::Queue => { | |
| 142 | + | if self.queue_list.list.len() == 0 { | |
| 143 | + | return Ok(()); | |
| 144 | + | } | |
| 138 | 145 | let file = self | |
| 139 | 146 | .queue_list | |
| 140 | 147 | .list | |
| @@ -145,6 +152,9 @@ impl App { | |||
| 145 | 152 | for (i, song) in self.queue_list.list.clone().iter().enumerate() { | |
| 146 | 153 | if song.contains(&file) { | |
| 147 | 154 | self.conn.conn.delete(i as u32).unwrap(); | |
| 155 | + | if self.queue_list.index != 0 { | |
| 156 | + | self.queue_list.index -= 1; | |
| 157 | + | } | |
| 148 | 158 | } | |
| 149 | 159 | } | |
| 150 | 160 | } | |
| @@ -205,9 +215,10 @@ impl App { | |||
| 205 | 215 | } | |
| 206 | 216 | ||
| 207 | 217 | pub fn search_song(&mut self) -> AppResult<()> { | |
| 208 | − | let filename = self.conn.get_full_path(&self.search_input)?; | |
| 209 | − | let song = self.conn.get_song_with_only_filename(&filename); | |
| 210 | − | self.conn.push(&song)?; | |
| 218 | + | if let Some(filename) = self.conn.get_full_path(&self.search_input) { | |
| 219 | + | let song = self.conn.get_song_with_only_filename(&filename); | |
| 220 | + | self.conn.push(&song)?; | |
| 221 | + | } | |
| 211 | 222 | Ok(()) | |
| 212 | 223 | } | |
| 213 | 224 | ||
| @@ -188,18 +188,26 @@ impl Connection { | |||
| 188 | 188 | } | |
| 189 | 189 | ||
| 190 | 190 | /// Given a song name from a directory, it returns the full path of the song in the database | |
| 191 | − | pub fn get_full_path(&self, short_path: &str) -> AppResult<String> { | |
| 192 | − | let list = self | |
| 193 | − | .songs_filenames | |
| 194 | − | .iter() | |
| 195 | − | .map(|f| f.as_str()) | |
| 196 | − | .collect::<Vec<&str>>(); | |
| 197 | − | let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&short_path, &list) | |
| 198 | − | .get(0) | |
| 199 | − | .unwrap() | |
| 200 | − | .clone(); | |
| 191 | + | pub fn get_full_path(&self, short_path: &str) -> Option<String> { | |
| 192 | + | // let list = self | |
| 193 | + | // .songs_filenames | |
| 194 | + | // .iter() | |
| 195 | + | // .map(|f| f.as_str()) | |
| 196 | + | // .collect::<Vec<&str>>(); | |
| 197 | + | // let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&short_path, &list) | |
| 198 | + | // .get(0) | |
| 199 | + | // .unwrap() | |
| 200 | + | // .clone(); | |
| 201 | + | ||
| 202 | + | for (i, f) in self.songs_filenames.iter().enumerate() { | |
| 203 | + | if f.contains(short_path) { | |
| 204 | + | return Some(self.songs_filenames.get(i).unwrap().to_string()); | |
| 205 | + | } | |
| 206 | + | } | |
| 207 | + | ||
| 208 | + | None | |
| 201 | 209 | ||
| 202 | − | Ok(filename.to_string()) | |
| 210 | + | // Ok(filename.to_string()) | |
| 203 | 211 | } | |
| 204 | 212 | ||
| 205 | 213 | /// Print status to stdout | |
| @@ -137,16 +137,16 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 137 | 137 | _ => {} | |
| 138 | 138 | } | |
| 139 | 139 | ||
| 140 | − | let full_path = app.conn.get_full_path(&short_path)?; | |
| 141 | − | let song = app.conn.get_song_with_only_filename(&full_path); | |
| 142 | − | ||
| 143 | − | if pl_name == "Current Playlist" { | |
| 144 | − | app.conn.conn.push(&song)?; | |
| 145 | − | app.update_queue(); | |
| 146 | − | } else { | |
| 147 | − | app.conn.add_to_playlist(pl_name, &song)?; | |
| 140 | + | if let Some(full_path) = app.conn.get_full_path(&short_path) { | |
| 141 | + | let song = app.conn.get_song_with_only_filename(&full_path); | |
| 142 | + | ||
| 143 | + | if pl_name == "Current Playlist" { | |
| 144 | + | app.conn.conn.push(&song)?; | |
| 145 | + | app.update_queue(); | |
| 146 | + | } else { | |
| 147 | + | app.conn.add_to_playlist(pl_name, &song)?; | |
| 148 | + | } | |
| 148 | 149 | } | |
| 149 | − | ||
| 150 | 150 | // hide the playlist popup | |
| 151 | 151 | app.playlist_popup = false; | |
| 152 | 152 | app.append_list.index = 0; | |
| @@ -156,7 +156,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 156 | 156 | } else { | |
| 157 | 157 | match key_event.code { | |
| 158 | 158 | // Quit | |
| 159 | − | KeyCode::Char('q') => app.quit(), | |
| 159 | + | KeyCode::Char('q') => app.quit(), | |
| 160 | 160 | KeyCode::Char('c') | KeyCode::Char('C') => { | |
| 161 | 161 | if key_event.modifiers == KeyModifiers::CONTROL { | |
| 162 | 162 | app.quit(); | |
| @@ -237,7 +237,6 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 237 | 237 | // add to queue | |
| 238 | 238 | KeyCode::Char('a') => app.playlist_popup = true, | |
| 239 | 239 | ||
| 240 | − | ||
| 241 | 240 | // Fast forward | |
| 242 | 241 | KeyCode::Char('f') => { | |
| 243 | 242 | let place = app.conn.conn.status().unwrap().song.unwrap().pos; | |
| @@ -319,7 +318,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 319 | 318 | ||
| 320 | 319 | // Remove from Current Playlsit | |
| 321 | 320 | KeyCode::Char(' ') | KeyCode::Backspace => { | |
| 322 | − | app.handle_remove_or_from_current_playlist()?; | |
| 321 | + | app.handle_add_or_remove_from_current_playlist()?; | |
| 323 | 322 | } | |
| 324 | 323 | ||
| 325 | 324 | // Change playlist name | |