make dmenu optional, dont crash

This commit is contained in:
krolxon 2024-05-13 17:32:52 +05:30
parent bc7ab75f0c
commit bba509fabf
2 changed files with 16 additions and 14 deletions

View File

@ -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

View File

@ -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()
}