Formatting, mainly
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Has been cancelled

This commit is contained in:
Nox Sluijtman 2025-06-08 22:53:08 +02:00
parent b2c944d1a6
commit 96bae9a045
Signed by: Egg
SSH key fingerprint: SHA256:2sG9X3C7Xvq2svGumz1/k7cm8l4G9+qAtAeugqB4J9M
2 changed files with 92 additions and 13 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target
result
.direnv

View file

@ -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<Commands>,
#[arg(short, long, global = true)]
verbose: bool,
#[arg(short = 'H', long, global = true)]
host: Option<String>,
}
#[derive(Subcommand)]
@ -24,29 +31,36 @@ enum Commands {
/// Stops playing
Stop {},
/// Play queueueu
Play {
track: Option<u32>,
},
Play { track: Option<u32> },
/// Set or get crossfade
Crossfade {
seconds: Option<i64>,
},
Crossfade { seconds: Option<i64> },
///Update still needs some work
Update {},
/// Return currently playing song
Current {},
/// Query database
Search {
///Search query
#[arg(trailing_var_arg = true)]
query: Vec<String>,
///Only return the first n results
#[arg(short, long)]
max: Option<u32>,
},
///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<String> = 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<Song>) -> () {
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)
}