diff --git a/src/app.rs b/src/app.rs index 7286657..22cd91e 100755 --- a/src/app.rs +++ b/src/app.rs @@ -113,11 +113,11 @@ impl App { Ok(()) } - pub fn handle_remove_or_from_current_playlist(&mut self) -> AppResult<()> { + pub fn handle_add_or_remove_from_current_playlist(&mut self) -> AppResult<()> { match self.selected_tab { SelectedTab::DirectoryBrowser => { let (_, file) = self.browser.filetree.get(self.browser.selected).unwrap(); - self.browser.selected += 1; + let mut status = false; for (i, song) in self.queue_list.list.clone().iter().enumerate() { if song.contains(file) { @@ -127,14 +127,21 @@ impl App { } if !status { - let song = self - .conn - .get_song_with_only_filename(&self.conn.get_full_path(&file).unwrap()); - self.conn.conn.push(&song)?; + if let Some(full_path) = &self.conn.get_full_path(&file) { + let song = self.conn.get_song_with_only_filename(full_path); + self.conn.conn.push(&song)?; + } + } + + if self.browser.selected != self.browser.filetree.len() - 1 { + self.browser.selected += 1; } } SelectedTab::Queue => { + if self.queue_list.list.len() == 0 { + return Ok(()); + } let file = self .queue_list .list @@ -145,6 +152,9 @@ impl App { for (i, song) in self.queue_list.list.clone().iter().enumerate() { if song.contains(&file) { self.conn.conn.delete(i as u32).unwrap(); + if self.queue_list.index != 0 { + self.queue_list.index -= 1; + } } } } @@ -205,9 +215,10 @@ impl App { } pub fn search_song(&mut self) -> AppResult<()> { - let filename = self.conn.get_full_path(&self.search_input)?; - let song = self.conn.get_song_with_only_filename(&filename); - self.conn.push(&song)?; + if let Some(filename) = self.conn.get_full_path(&self.search_input) { + let song = self.conn.get_song_with_only_filename(&filename); + self.conn.push(&song)?; + } Ok(()) } diff --git a/src/connection.rs b/src/connection.rs index 1ec76ae..8f4fe72 100755 --- a/src/connection.rs +++ b/src/connection.rs @@ -188,18 +188,26 @@ impl Connection { } /// Given a song name from a directory, it returns the full path of the song in the database - pub fn get_full_path(&self, short_path: &str) -> AppResult { - let list = self - .songs_filenames - .iter() - .map(|f| f.as_str()) - .collect::>(); - let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&short_path, &list) - .get(0) - .unwrap() - .clone(); + pub fn get_full_path(&self, short_path: &str) -> Option { + // let list = self + // .songs_filenames + // .iter() + // .map(|f| f.as_str()) + // .collect::>(); + // let (filename, _) = rust_fuzzy_search::fuzzy_search_sorted(&short_path, &list) + // .get(0) + // .unwrap() + // .clone(); - Ok(filename.to_string()) + for (i, f) in self.songs_filenames.iter().enumerate() { + if f.contains(short_path) { + return Some(self.songs_filenames.get(i).unwrap().to_string()); + } + } + + None + + // Ok(filename.to_string()) } /// Print status to stdout diff --git a/src/handler.rs b/src/handler.rs index 7f23688..c9d4d04 100755 --- a/src/handler.rs +++ b/src/handler.rs @@ -137,16 +137,16 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { _ => {} } - let full_path = app.conn.get_full_path(&short_path)?; - let song = app.conn.get_song_with_only_filename(&full_path); + if let Some(full_path) = app.conn.get_full_path(&short_path) { + let song = app.conn.get_song_with_only_filename(&full_path); - if pl_name == "Current Playlist" { - app.conn.conn.push(&song)?; - app.update_queue(); - } else { - app.conn.add_to_playlist(pl_name, &song)?; + if pl_name == "Current Playlist" { + app.conn.conn.push(&song)?; + app.update_queue(); + } else { + app.conn.add_to_playlist(pl_name, &song)?; + } } - // hide the playlist popup app.playlist_popup = false; app.append_list.index = 0; @@ -156,7 +156,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { } else { match key_event.code { // Quit - KeyCode::Char('q') => app.quit(), + KeyCode::Char('q') => app.quit(), KeyCode::Char('c') | KeyCode::Char('C') => { if key_event.modifiers == KeyModifiers::CONTROL { app.quit(); @@ -237,7 +237,6 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { // add to queue KeyCode::Char('a') => app.playlist_popup = true, - // Fast forward KeyCode::Char('f') => { 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<()> { // Remove from Current Playlsit KeyCode::Char(' ') | KeyCode::Backspace => { - app.handle_remove_or_from_current_playlist()?; + app.handle_add_or_remove_from_current_playlist()?; } // Change playlist name