From 76facd0ec2cdd11bad768b64ee864c9bdc2de32b Mon Sep 17 00:00:00 2001 From: Marty Sluijtman Date: Sat, 21 Jun 2025 15:46:40 +0200 Subject: [PATCH] Asked Vlams to look over it, this is the result This commit is authored by me due to laziness --- src/main.rs | 243 ++++++++++++++++++++-------------------------------- 1 file changed, 93 insertions(+), 150 deletions(-) diff --git a/src/main.rs b/src/main.rs index 53b54ad..574fcda 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,23 +24,23 @@ struct Cli { #[derive(Subcommand)] enum Commands { /// Toggle MPD stream - Toggle {}, + Toggle, /// Skip to the next track - Next {}, + Next, /// Revert to the previous track - Prev {}, + Prev, /// Stops playing - Stop {}, + Stop, /// Play queueueu Play { track: Option }, /// Set or get crossfade Crossfade { seconds: Option }, ///Update still needs some work - Update {}, + Update, /// Return currently playing song - Current {}, + Current, /// Clear current queueueu - Clear {}, + Clear, /// Query database Search { ///Search query @@ -75,7 +75,7 @@ enum Commands { // position: Option, // }, /// Shuffles the current queueue - Shuffle {}, + Shuffle, } fn main() { @@ -89,15 +89,8 @@ fn main() { None => String::from("localhost:6600"), }; - // let mut conn = Client::connect(host).unwrap(); - let mut conn = Client::connect(host).expect("Connection failed"); - // let mut conn = match Client::connect(host) { - // Ok(conn) => conn, - // Err(error) => panic!("yo fucked up: {}", error.to_string()), - // }; - if let Some(cmd) = &cli.command { match cmd { Commands::Play { track } => { @@ -106,36 +99,19 @@ fn main() { } else { conn.play().unwrap(); } - let status = get_status(conn, cli.verbose, n); - println!("{status}"); } - - Commands::Stop {} => { + Commands::Stop => { conn.stop().unwrap(); } - - Commands::Toggle {} => { + Commands::Toggle => { if conn.status().unwrap().state == State::Stop { conn.play().unwrap(); } else { conn.toggle_pause().unwrap(); } - let status = get_status(conn, cli.verbose, n); - println!("{status}"); } - - Commands::Next {} => { - conn.next().unwrap(); - let status = get_status(conn, cli.verbose, n); - println!("{status}"); - } - - Commands::Prev {} => { - conn.prev().unwrap(); - let status = get_status(conn, cli.verbose, n); - println!("{status}"); - } - + Commands::Next => conn.next().unwrap(), + Commands::Prev => conn.prev().unwrap(), Commands::List { file } => conn.queue().unwrap().iter().for_each(|x| { println!( "{:<02} {}", @@ -147,166 +123,133 @@ fn main() { } ) }), - - Commands::Update {} => { + Commands::Update => { let thing = conn.update().unwrap(); println!("{thing}") } - - Commands::Current {} => { - let status = get_status(conn, cli.verbose, n); - println!("{status}"); - } - - Commands::Clear {} => { - conn.clear().unwrap(); - } - - Commands::Search { - query, - max, - // append, - // insert, - } => { - let mut songs: Vec = vec![]; + Commands::Current => (), // this is basically the same as having no command + Commands::Clear => conn.clear().unwrap(), + Commands::Search { query, max } => { let query = query.join(" "); - // I want to return all results by default - let window = ( - 0, - if let Some(n) = max { - *n - } else { - conn.stats().unwrap().songs - }, - ); - // let queue_length = conn.status().unwrap().queue_len; - // conn.search(&Query::new().and(Term::Any, query), window) - // .unwrap() - // .iter() - // .for_each(|x| songs.push(x.clone())); + let max = max.unwrap_or(conn.stats().unwrap().songs); - // songs.iter().rev().for_each(|x| { - // conn.insert(x, usize::try_from(queue_length).unwrap()) - // .unwrap(); - // }); - //conn.insert(, usize::try_from(queue_length).unwrap()) - conn.search(&Query::new().and(Term::Any, query), window) + conn.search(&Query::new().and(Term::Any, query), (0, max)) .unwrap() .iter() - .for_each(|x| songs.push(x.clone())); - songs.iter().for_each(|x| println!("{}", x.file)) + .for_each(|x| println!("{}", x.file)); } - Commands::Find { query, max, append } => { - let mut songs: Vec = vec![]; let query = query.join(" "); - let window = ( - 0, - if let Some(n) = max { - *n - } else { - conn.stats().unwrap().songs - }, - ); + let max = max.unwrap_or(conn.stats().unwrap().songs); + if *append { conn.findadd(&Query::new().and(Term::Any, query)).unwrap(); } else { - conn.find(&Query::new().and(Term::Any, query), window) + conn.find(&Query::new().and(Term::Any, query), (0, max)) .unwrap() .iter() - .for_each(|x| songs.push(x.clone())); - songs.iter().for_each(|x| println!("{}", x.file)) + .for_each(|x| println!("{}", x.file)); } } - Commands::Crossfade { seconds } => { if let Some(crossfade) = seconds { - conn.crossfade(*crossfade).unwrap() + conn.crossfade(*crossfade).unwrap(); } else { - if let Some(crossfade) = conn.status().unwrap().crossfade { - println!("Crossfade: {}", crossfade.as_secs()); - } else { - println!("Crossfade disabled") - } + println!( + "Crossfade: {}", + conn.status() + .unwrap() + .crossfade + .map_or("Disabled".into(), |d| d.as_secs().to_string()) + ); } } - - Commands::Shuffle {} => { - send_notification("Shuffling queueueu...".to_string(), n); + Commands::Shuffle => { + send_notification("Shuffling queueueu...", &n); println!("Shuffling queueueu..."); conn.shuffle(..).unwrap(); } } - } else { - let status = get_status(conn, cli.verbose, n); - println!("{}", &status); - libnotify::uninit(); + + if let Commands::Stop + | Commands::List { .. } + | Commands::Update + | Commands::Clear + | Commands::Search { .. } + | Commands::Find { .. } + | Commands::Crossfade { .. } + | Commands::Shuffle = cmd + { + return; + } } + println!("{}", get_status(conn, cli.verbose, n)); } fn format_song(song: Song) -> String { format!("{} - {}", song.artist.unwrap(), song.title.unwrap()) } -///Bool to String -fn bts(b: bool) -> &'static str { - b.then(|| "on").unwrap_or("off") +trait QuickFmt { + fn quick_fmt(&self) -> &'static str; +} + +impl QuickFmt for bool { + fn quick_fmt(&self) -> &'static str { + if *self { + "on" + } else { + "off" + } + } } fn get_status(mut client: Client, verbose: bool, notify: Notification) -> String { + let current_song = if let Some(song) = client.currentsong().unwrap() { + format_song(song) + } else { + "No song playing".into() + }; + + send_notification(¤t_song, ¬ify); + if !verbose { + return current_song; + } + + let mut output = Vec::with_capacity(3); + output.push(current_song); + let status = client.status().unwrap(); - let repeat = bts(status.repeat); - let single = bts(status.single); - let random = bts(status.random); - let consume = bts(status.consume); - let duration = match status.time { - Some((stamp, total)) => { - let stamp = to_mins_string(stamp); - let total = to_mins_string(total); - format!("{stamp}/{total}") - } - None => "".to_string(), + output.push(format!( + "volume: {vol:<6} repeat: {repeat:<6} random: {rnd:<6} single: {single:<6} consume: {consume:<6}", + vol = format!("{}%", status.volume), + repeat = status.repeat.quick_fmt(), + rnd = status.random.quick_fmt(), + single = status.single.quick_fmt(), + consume = status.consume.quick_fmt() + )); + + let state = match status.state { + State::Stop => "stopped", + State::Pause => "paused", + State::Play => "playing", }; - let mut output = vec![]; - if let Some(song) = client.currentsong().unwrap() { - output.push(format_song(song)) - } else { - output.push("No song playing".to_string()) - } + let duration = status.time.map_or("".into(), |(stamp, total)| { + let stamp = to_mins_string(stamp); + let total = to_mins_string(total); + format!("{stamp}/{total}") + }); - send_notification(output.first().unwrap().clone(), notify); - - if verbose { - output.push(format!( - "volume: {vol:<6} repeat: {repeat:<6} random: {rnd:<6} single: {single:<6} consume: {consume:<6}", - vol = {format!("{}%", status.volume)}, - repeat = repeat, - rnd = random, - single = single, - consume = consume - )); - - output.push(format!( - "[{status}] {duration}", - status = { - match client.status().unwrap().state { - State::Stop => "stopped", - State::Pause => "paused", - State::Play => "playing", - } - } - )); - } + output.push(format!("[{state}] {duration}")); output.join("\n") } -fn send_notification(body: String, notify: Notification) -> String { - let _ = notify.update("Noise", body.as_str(), None); +fn send_notification(body: &str, notify: &Notification) { + let _ = notify.update("Noise", body, None); notify.show().unwrap(); - body } fn to_mins_string(dur: Duration) -> String {