Krolyxon
Commit 6b66a44

rmptui6b66a44

fix queue deletion & make get_full_path return an option

Author
krolxon
Date
2024-04-28 23:59 +0530
Parent
66a1cfc
Commit
6b66a44ed035c33c43377a63b23856923743ea09

+50 −32

3 files changed

  1. M src/app.rs +20 −9
  2. M src/connection.rs +19 −11
  3. M src/handler.rs +11 −12
M src/app.rs View
@@ -113,11 +113,11 @@ impl App {
113113 Ok(())
114114 }
115115
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<()> {
117117 match self.selected_tab {
118118 SelectedTab::DirectoryBrowser => {
119119 let (_, file) = self.browser.filetree.get(self.browser.selected).unwrap();
120− self.browser.selected += 1;
120+
121121 let mut status = false;
122122 for (i, song) in self.queue_list.list.clone().iter().enumerate() {
123123 if song.contains(file) {
@@ -127,14 +127,21 @@ impl App {
127127 }
128128
129129 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;
134138 }
135139 }
136140
137141 SelectedTab::Queue => {
142+ if self.queue_list.list.len() == 0 {
143+ return Ok(());
144+ }
138145 let file = self
139146 .queue_list
140147 .list
@@ -145,6 +152,9 @@ impl App {
145152 for (i, song) in self.queue_list.list.clone().iter().enumerate() {
146153 if song.contains(&file) {
147154 self.conn.conn.delete(i as u32).unwrap();
155+ if self.queue_list.index != 0 {
156+ self.queue_list.index -= 1;
157+ }
148158 }
149159 }
150160 }
@@ -205,9 +215,10 @@ impl App {
205215 }
206216
207217 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+ }
211222 Ok(())
212223 }
213224
M src/connection.rs View
@@ -188,18 +188,26 @@ impl Connection {
188188 }
189189
190190 /// 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
201209
202− Ok(filename.to_string())
210+ // Ok(filename.to_string())
203211 }
204212
205213 /// Print status to stdout
M src/handler.rs View
@@ -137,16 +137,16 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
137137 _ => {}
138138 }
139139
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+ }
148149 }
149−
150150 // hide the playlist popup
151151 app.playlist_popup = false;
152152 app.append_list.index = 0;
@@ -156,7 +156,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
156156 } else {
157157 match key_event.code {
158158 // Quit
159− KeyCode::Char('q') => app.quit(),
159+ KeyCode::Char('q') => app.quit(),
160160 KeyCode::Char('c') | KeyCode::Char('C') => {
161161 if key_event.modifiers == KeyModifiers::CONTROL {
162162 app.quit();
@@ -237,7 +237,6 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
237237 // add to queue
238238 KeyCode::Char('a') => app.playlist_popup = true,
239239
240−
241240 // Fast forward
242241 KeyCode::Char('f') => {
243242 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<()> {
319318
320319 // Remove from Current Playlsit
321320 KeyCode::Char(' ') | KeyCode::Backspace => {
322− app.handle_remove_or_from_current_playlist()?;
321+ app.handle_add_or_remove_from_current_playlist()?;
323322 }
324323
325324 // Change playlist name