Krolyxon
Commit 3d569cb

rmptui3d569cb

fix dmenu_play adding song even when exit without seleciton

Author
krolxon
Date
2024-05-12 17:30 +0530
Parent
0ded150
Commit
3d569cbf7903cfdf56a6056d9a0771cd2279188b

+5 −13

1 file changed

  1. M src/connection.rs +5 −13
M src/connection.rs View
@@ -69,9 +69,11 @@ impl Connection {
6969 is_installed("dmenu")?;
7070 let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect();
7171 let op = dmenu!(iter &ss; args "-p", "Choose a song: ", "-l", "30");
72− let index = get_choice_index(&self.songs_filenames, &op);
73− let song = self.get_song_with_only_filename(ss.get(index).unwrap());
74− self.push(&song)?;
72+ let index = ss.iter().position(|s| s == &op);
73+ if let Some(i) = index {
74+ let song = self.get_song_with_only_filename(ss.get(i).unwrap());
75+ self.push(&song)?;
76+ }
7577 Ok(())
7678 }
7779
@@ -237,16 +239,6 @@ impl Connection {
237239 }
238240 }
239241
240−/// Gets the index of the string from the Vector
241−fn get_choice_index(ss: &[String], selection: &str) -> usize {
242− let mut choice: usize = 0;
243− if let Some(index) = ss.iter().position(|s| s == selection) {
244− choice = index;
245− }
246−
247− choice
248−}
249−
250242 /// Checks if given program is installed in your system
251243 fn is_installed(ss: &str) -> Result<()> {
252244 let output = Command::new("which")