From bba509fabf490ab51afc60124f0e4cf1a1db6a3d Mon Sep 17 00:00:00 2001 From: krolxon Date: Mon, 13 May 2024 17:32:52 +0530 Subject: [PATCH] make dmenu optional, dont crash --- README.md | 4 ++++ src/connection.rs | 26 ++++++++++++-------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 35b6583..2495b6b 100755 --- a/README.md +++ b/README.md @@ -34,6 +34,10 @@ A MPD client in Rust - `g` to go to top of list - `G` to go to bottom of list +### Prerequisites +- [MPD](https://wiki.archlinux.org/title/Music_Player_Daemon) installed and configured. +- [dmenu](https://tools.suckless.org/dmenu/) (optional) + ### TODO - [x] fix performance issues - [x] improvements on queue control diff --git a/src/connection.rs b/src/connection.rs index 84ca664..7afb3d2 100755 --- a/src/connection.rs +++ b/src/connection.rs @@ -66,14 +66,16 @@ impl Connection { /// Dmenu prompt for selecting songs pub fn play_dmenu(&mut self) -> Result<()> { - 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 = 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)?; + if 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 = 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(()) } @@ -240,15 +242,11 @@ impl Connection { } /// Checks if given program is installed in your system -fn is_installed(ss: &str) -> Result<()> { +fn is_installed(ss: &str) -> bool { let output = Command::new("which") .arg(ss) .output() .expect("Failed to execute command"); - if output.status.success() { - Ok(()) - } else { - let err = format!("{} not installed", ss); - Err(err.into()) - } + + output.status.success() }