From 5556ea70df46d81788f33bb626706d8d84264c92 Mon Sep 17 00:00:00 2001 From: Marty Sluijtman Date: Wed, 9 Jul 2025 18:20:50 +0200 Subject: [PATCH 1/3] Cleanup --- src/main.rs | 15 +++++++++++---- src/noise/commands.rs | 22 ++++++++++++++-------- src/noise/parser.rs | 3 +-- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index bc9d85f..cc9bf03 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ fn main() { if let Some(cmd) = &cli.command { match cmd { - Commands::Play { track } => conn.noise_play(*track), + Commands::Play { track } => Noise::play(&mut conn, *track), Commands::Stop => conn.stop().unwrap(), Commands::Toggle => conn.toggle(), Commands::Next => conn.next().unwrap(), @@ -41,9 +41,9 @@ fn main() { Commands::List { file } => conn.list_queue(*file, cli.verbose), Commands::Current => (), Commands::Clear => conn.clear().unwrap(), - Commands::Search { query, max } => conn.noise_search(query, *max), - Commands::Find { query, max, append } => conn.noise_find(query, *max, *append), - Commands::Crossfade { seconds } => conn.noise_crossfade(*seconds), + Commands::Search { query, max } => Noise::search(&mut conn, query, *max), + Commands::Find { query, max, append } => Noise::find(&mut conn, query, *max, *append), + Commands::Crossfade { seconds } => Noise::crossfade(&mut conn, *seconds), Commands::Shuffle => conn.shuf(), Commands::Repeat => conn.toggle_repeat(), Commands::Random => conn.toggle_random(), @@ -65,5 +65,12 @@ fn main() { return; } } + let things: Vec = conn + .queue() + .unwrap() + .iter() + .map(|x| x.place.unwrap().pos.to_string()) + .collect(); + println!("{things:?}"); println!("{}", conn.get_status(cli.verbose)); } diff --git a/src/noise/commands.rs b/src/noise/commands.rs index 1696ef1..c77eff9 100644 --- a/src/noise/commands.rs +++ b/src/noise/commands.rs @@ -6,10 +6,10 @@ use mpd::{Client, Query, Song, State, Term}; use std::time::Duration; pub trait Noise { - fn noise_find(&mut self, query: &Vec, max: Option, append: bool); - fn noise_search(&mut self, query: &Vec, max: Option); - fn noise_crossfade(&mut self, seconds: Option); - fn noise_play(&mut self, track: Option); + fn find(&mut self, query: &Vec, max: Option, append: bool); + fn search(&mut self, query: &Vec, max: Option); + fn crossfade(&mut self, seconds: Option); + fn play(&mut self, track: Option); fn toggle(&mut self); fn munch(&mut self); fn length(&mut self); @@ -21,10 +21,11 @@ pub trait Noise { fn shuf(&mut self); fn get_status(&mut self, verbose: bool) -> String; fn list_queue(&mut self, file: bool, _verbose: bool); + // fn get_art(&mut self); } impl Noise for Client { - fn noise_find(&mut self, query: &Vec, max: Option, append: bool) { + fn find(&mut self, query: &Vec, max: Option, append: bool) { let query = query.join(" "); let max = max.unwrap_or(self.stats().unwrap().songs); @@ -38,7 +39,7 @@ impl Noise for Client { } } - fn noise_search(&mut self, query: &Vec, max: Option) { + fn search(&mut self, query: &Vec, max: Option) { let query = query.join(" "); let max = max.unwrap_or(self.stats().unwrap().songs); @@ -48,7 +49,7 @@ impl Noise for Client { .for_each(|x| println!("{}", x.file)); } - fn noise_crossfade(&mut self, seconds: Option) { + fn crossfade(&mut self, seconds: Option) { if let Some(seconds) = seconds { self.crossfade(seconds).unwrap(); } else { @@ -62,7 +63,7 @@ impl Noise for Client { } } - fn noise_play(&mut self, track: Option) { + fn play(&mut self, track: Option) { if let Some(i) = track { self.switch(i).unwrap(); } else { @@ -185,6 +186,11 @@ impl Noise for Client { output.join("\n") } + + // fn get_art(&mut self) { + // let current_song = self.currentsong().unwrap().unwrap().file; + // self.albumart(¤t_song).unwrap(); + // } } fn format_song(song: Song) -> String { diff --git a/src/noise/parser.rs b/src/noise/parser.rs index 4505ae6..587a95d 100644 --- a/src/noise/parser.rs +++ b/src/noise/parser.rs @@ -40,9 +40,8 @@ pub enum Commands { /// Stops playing Stop, /// Play queueueu - #[command(visible_alias = "start")] Play { - #[arg(value_hint = ValueHint::Other)] + #[arg(value_hint = ValueHint::CommandString)] track: Option, }, /// Set or get crossfade From c9ac64d5043ab304c59b31b4a255dd2b73214d8b Mon Sep 17 00:00:00 2001 From: Marty Sluijtman Date: Wed, 9 Jul 2025 21:06:35 +0200 Subject: [PATCH 2/3] Munch command --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 5 +++-- src/noise/commands.rs | 11 ++--------- 4 files changed, 7 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9925244..6f3f9d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -268,7 +268,7 @@ dependencies = [ [[package]] name = "noise" -version = "0.1.7" +version = "0.1.8" dependencies = [ "clap", "clap_complete", diff --git a/Cargo.toml b/Cargo.toml index afe0ed3..2937ab7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "noise" -version = "0.1.7" +version = "0.1.8" edition = "2024" authors = ["Nox Sluijtman"] diff --git a/src/main.rs b/src/main.rs index cc9bf03..9203c39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,17 +60,18 @@ fn main() { | Commands::Search { .. } | Commands::Find { .. } | Commands::Crossfade { .. } + | Commands::Volume { .. } | Commands::Shuffle = cmd { return; } } - let things: Vec = conn + let _things: Vec = conn .queue() .unwrap() .iter() .map(|x| x.place.unwrap().pos.to_string()) .collect(); - println!("{things:?}"); + // println!("{things:?}"); println!("{}", conn.get_status(cli.verbose)); } diff --git a/src/noise/commands.rs b/src/noise/commands.rs index c77eff9..d6e4fa8 100644 --- a/src/noise/commands.rs +++ b/src/noise/commands.rs @@ -21,7 +21,6 @@ pub trait Noise { fn shuf(&mut self); fn get_status(&mut self, verbose: bool) -> String; fn list_queue(&mut self, file: bool, _verbose: bool); - // fn get_art(&mut self); } impl Noise for Client { @@ -80,9 +79,8 @@ impl Noise for Client { } fn munch(&mut self) { - // let current_song = self.currentsong().unwrap().unwrap().place.unwrap(); - // self.delete(current_song).unwrap(); - todo!("Not net implemented") + let current_song = self.currentsong().unwrap().unwrap().place.unwrap(); + self.delete(current_song.pos).unwrap(); } fn length(&mut self) { @@ -186,11 +184,6 @@ impl Noise for Client { output.join("\n") } - - // fn get_art(&mut self) { - // let current_song = self.currentsong().unwrap().unwrap().file; - // self.albumart(¤t_song).unwrap(); - // } } fn format_song(song: Song) -> String { From eddf2231e94b7c8a7af1bd312855b97ff7024bbe Mon Sep 17 00:00:00 2001 From: Marty Sluijtman Date: Wed, 23 Jul 2025 13:18:52 +0200 Subject: [PATCH 3/3] Better error and notification handling --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 15 +++++++++------ src/noise/commands.rs | 37 ++++++++++++++++++++++++------------- src/noise/parser.rs | 19 ++++++++++--------- 5 files changed, 45 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6f3f9d8..8354351 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -268,7 +268,7 @@ dependencies = [ [[package]] name = "noise" -version = "0.1.8" +version = "0.1.9" dependencies = [ "clap", "clap_complete", diff --git a/Cargo.toml b/Cargo.toml index 2937ab7..c50e4d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "noise" -version = "0.1.8" +version = "0.1.9" edition = "2024" authors = ["Nox Sluijtman"] diff --git a/src/main.rs b/src/main.rs index 9203c39..4a6e5d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use noise::{commands::Noise, parser::*}; use clap::{Command, CommandFactory, Parser}; use clap_complete::{generate, Generator}; -use mpd::Client; +use mpd::{error::Error, Client}; use std::io; fn print_completions(generator: G, cmd: &mut Command) { @@ -17,19 +17,22 @@ fn print_completions(generator: G, cmd: &mut Command) { ); } -fn main() { +fn main() -> Result<(), Error> { let cli = Cli::parse(); if let Some(completions) = cli.completions { let mut cmd = Cli::command(); eprintln!("Generatig completion file for {completions:?}..."); print_completions(completions, &mut cmd); - return; + return Ok(()); } let host = cli.host.unwrap_or("localhost:6600".into()); - let mut conn = Client::connect(host).expect("Connection failed"); + let mut conn = match Client::connect(host) { + Ok(client) => client, + Err(err) => return Err(err), + }; if let Some(cmd) = &cli.command { match cmd { @@ -63,7 +66,7 @@ fn main() { | Commands::Volume { .. } | Commands::Shuffle = cmd { - return; + return Ok(()); } } let _things: Vec = conn @@ -72,6 +75,6 @@ fn main() { .iter() .map(|x| x.place.unwrap().pos.to_string()) .collect(); - // println!("{things:?}"); println!("{}", conn.get_status(cli.verbose)); + return Ok(()); } diff --git a/src/noise/commands.rs b/src/noise/commands.rs index d6e4fa8..ac30325 100644 --- a/src/noise/commands.rs +++ b/src/noise/commands.rs @@ -3,7 +3,7 @@ extern crate mpd; use libnotify::Notification; use mpd::{Client, Query, Song, State, Term}; -use std::time::Duration; +use std::{env, time::Duration}; pub trait Noise { fn find(&mut self, query: &Vec, max: Option, append: bool); @@ -174,12 +174,23 @@ impl Noise for Client { State::Play => "playing", }; + fn to_mins_string(dur: Duration) -> String { + let seconds = dur.as_secs(); + + let minutes = seconds / 60; + let rem_seconds = seconds % 60; + + format!("{minutes:02}:{rem_seconds:02}") + } + let duration = status.time.map_or("".into(), |(stamp, total)| { let stamp = to_mins_string(stamp); let total = to_mins_string(total); format!("{stamp}/{total}") }); + // let pos_in_queue = format!("{current_pos}/{queue_lenth}"); + output.push(format!("[{state}] {duration}")); output.join("\n") @@ -197,16 +208,16 @@ fn format_song(song: Song) -> String { // fn get_status(client: &mut Client, verbose: bool) -> String {} fn send_notification(body: &str) { - libnotify::init("noise").unwrap(); - let notify = Notification::new("Noise", Some(body), None); - notify.show().unwrap(); -} - -fn to_mins_string(dur: Duration) -> String { - let seconds = dur.as_secs(); - - let minutes = seconds / 60; - let rem_seconds = seconds % 60; - - format!("{minutes:02}:{rem_seconds:02}") + let key = "XDG_SESSION_TYPE"; + match env::var(key) { + Ok(key) => { + if key != "tty" { + libnotify::init("noise").unwrap(); + let notify = Notification::new("Noise", Some(body), None); + notify.show().unwrap(); + libnotify::uninit(); + } + } + Err(_) => return, + } } diff --git a/src/noise/parser.rs b/src/noise/parser.rs index 587a95d..71b5b87 100644 --- a/src/noise/parser.rs +++ b/src/noise/parser.rs @@ -29,6 +29,13 @@ pub struct Cli { #[derive(Subcommand, Debug, PartialEq)] pub enum Commands { + /// Stops playing + Stop, + /// Play queueueu + Play { + #[arg(value_hint = ValueHint::CommandString)] + track: Option, + }, /// Toggle MPD playback #[command(visible_alias = "t")] Toggle, @@ -37,13 +44,6 @@ pub enum Commands { Next, /// Revert to the previous track Prev, - /// Stops playing - Stop, - /// Play queueueu - Play { - #[arg(value_hint = ValueHint::CommandString)] - track: Option, - }, /// Set or get crossfade #[command(visible_alias = "fade")] Crossfade { @@ -52,8 +52,6 @@ pub enum Commands { }, // ///Update still needs some work // Update, - /// Return currently playing song - Current, /// Clear current queueueu Clear, /// Query database @@ -70,6 +68,8 @@ pub enum Commands { // #[arg(short, long)] // insert: Option, }, + /// Return currently playing song + Current, /// Query database autistically #[command(visible_alias = "fd")] Find { @@ -109,6 +109,7 @@ pub enum Commands { #[arg(value_hint = ValueHint::Other)] percentage: Option, }, + /// Remove the current track Munch, Length, }