From 3d569cbf7903cfdf56a6056d9a0771cd2279188b Mon Sep 17 00:00:00 2001 From: krolxon Date: Sun, 12 May 2024 17:30:55 +0530 Subject: [PATCH] fix dmenu_play adding song even when exit without seleciton --- src/connection.rs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index bbe14f6..84ca664 100755 --- a/src/connection.rs +++ b/src/connection.rs @@ -69,9 +69,11 @@ impl Connection { is_installed("dmenu")?; let ss: Vec<&str> = self.songs_filenames.iter().map(|x| x.as_str()).collect(); let op = dmenu!(iter &ss; args "-p", "Choose a song: ", "-l", "30"); - let index = get_choice_index(&self.songs_filenames, &op); - let song = self.get_song_with_only_filename(ss.get(index).unwrap()); - self.push(&song)?; + let index = ss.iter().position(|s| s == &op); + if let Some(i) = index { + let song = self.get_song_with_only_filename(ss.get(i).unwrap()); + self.push(&song)?; + } Ok(()) } @@ -237,16 +239,6 @@ impl Connection { } } -/// Gets the index of the string from the Vector -fn get_choice_index(ss: &[String], selection: &str) -> usize { - let mut choice: usize = 0; - if let Some(index) = ss.iter().position(|s| s == selection) { - choice = index; - } - - choice -} - /// Checks if given program is installed in your system fn is_installed(ss: &str) -> Result<()> { let output = Command::new("which")