From 96bae9a0458cc661a6af5ebe97eb187a1881394f Mon Sep 17 00:00:00 2001 From: Marty Sluijtman Date: Sun, 8 Jun 2025 22:53:08 +0200 Subject: [PATCH] Formatting, mainly --- .gitignore | 1 + src/main.rs | 104 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 92 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 2d5df85..1fbc882 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target +result .direnv diff --git a/src/main.rs b/src/main.rs index a2ac34e..cd8852a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,23 @@ extern crate mpd; +use std::time::Duration; + use mpd::{Client, Query, Song, Term}; use clap::{Parser, Subcommand}; #[derive(Parser)] -#[command(author, version, about, long_about = None)] +#[command(author, version, about, long_about = "Banaan")] #[command(propagate_version = true)] struct Cli { #[command(subcommand)] command: Option, + + #[arg(short, long, global = true)] + verbose: bool, + #[arg(short = 'H', long, global = true)] + host: Option, } #[derive(Subcommand)] @@ -24,29 +31,36 @@ enum Commands { /// Stops playing Stop {}, /// Play queueueu - Play { - track: Option, - }, + Play { track: Option }, /// Set or get crossfade - Crossfade { - seconds: Option, - }, + Crossfade { seconds: Option }, + ///Update still needs some work Update {}, /// Return currently playing song Current {}, /// Query database Search { + ///Search query #[arg(trailing_var_arg = true)] query: Vec, + ///Only return the first n results + #[arg(short, long)] + max: Option, }, + ///List items in the current queueueu List {}, } fn main() { - let mut conn = Client::connect("localhost:6600").unwrap(); - let cli = Cli::parse(); + let host = match cli.host { + Some(host) => host, + None => String::from("localhost:6600"), + }; + + let mut conn = Client::connect(host).unwrap(); + if let Some(cmd) = &cli.command { match cmd { Commands::Play { track } => { @@ -76,10 +90,18 @@ fn main() { println!("{}", format_song(conn.currentsong().unwrap().unwrap())) } - Commands::Search { query } => { + Commands::Search { query, max } => { let mut result: Vec = vec![]; let query = query.join(" "); - let window = (0, conn.stats().unwrap().songs); + // I want to return all results by default + let window = ( + 0, + if let Some(n) = max { + *n + } else { + conn.stats().unwrap().songs + }, + ); conn.search(&Query::new().and(Term::Any, query), window) .unwrap() .iter() @@ -100,8 +122,7 @@ fn main() { } } } else { - println!("{}", format_song(conn.currentsong().unwrap().unwrap())); - println!("{:?}", conn.stats().unwrap().songs); + println!("{}", get_status(conn, cli.verbose)); } } @@ -114,3 +135,60 @@ fn print_songs(songs: Vec) -> () { 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") +} + +fn get_status(mut client: Client, verbose: bool) -> String { + 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(), + }; + let mut output = vec![]; + + if let Some(song) = client.currentsong().unwrap() { + output.push(format_song(song)) + } else { + output.push("No song playing".to_string()) + } + + 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!( + "[{:?}] {}", + client.status().unwrap().state, + duration + )); + } + + output.join("\n") +} + +fn to_mins_string(dur: Duration) -> String { + let seconds = dur.as_secs(); + + let minutes = seconds / 60; + let rem_seconds = seconds % 60; + + format!("{:02}:{:02}", minutes, rem_seconds) +}