2025-06-07 11:24:20 +02:00
|
|
|
extern crate mpd;
|
|
|
|
|
2025-06-07 23:50:30 +02:00
|
|
|
use mpd::{Client, Query, Song, Term};
|
2025-06-07 11:24:20 +02:00
|
|
|
|
2025-06-07 11:43:42 +02:00
|
|
|
use clap::{Parser, Subcommand};
|
2025-06-07 11:24:20 +02:00
|
|
|
|
2025-06-07 11:43:42 +02:00
|
|
|
#[derive(Parser)]
|
|
|
|
#[command(author, version, about, long_about = None)]
|
|
|
|
#[command(propagate_version = true)]
|
|
|
|
|
|
|
|
struct Cli {
|
|
|
|
#[command(subcommand)]
|
2025-06-07 23:50:30 +02:00
|
|
|
command: Option<Commands>,
|
2025-06-07 11:43:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Subcommand)]
|
|
|
|
enum Commands {
|
2025-06-07 23:50:30 +02:00
|
|
|
/// Toggle MPD stream
|
|
|
|
Toggle {},
|
|
|
|
/// Skip to the next track
|
|
|
|
Next {},
|
|
|
|
/// Revert to the previous track
|
|
|
|
Prev {},
|
|
|
|
/// Stops playing
|
|
|
|
Stop {},
|
|
|
|
/// Play queueueu
|
|
|
|
Play {
|
|
|
|
track: Option<u32>,
|
|
|
|
},
|
|
|
|
/// Set or get crossfade
|
|
|
|
Crossfade {
|
|
|
|
seconds: Option<i64>,
|
|
|
|
},
|
|
|
|
Update {},
|
|
|
|
/// Return currently playing song
|
|
|
|
Current {},
|
|
|
|
/// Query database
|
|
|
|
Search {
|
|
|
|
#[arg(trailing_var_arg = true)]
|
|
|
|
query: Vec<String>,
|
|
|
|
},
|
|
|
|
List {},
|
2025-06-07 11:24:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut conn = Client::connect("localhost:6600").unwrap();
|
|
|
|
|
2025-06-07 11:43:42 +02:00
|
|
|
let cli = Cli::parse();
|
2025-06-07 23:50:30 +02:00
|
|
|
|
|
|
|
if let Some(cmd) = &cli.command {
|
|
|
|
match cmd {
|
|
|
|
Commands::Play { track } => {
|
|
|
|
if let Some(i) = track {
|
|
|
|
conn.switch(*i).unwrap()
|
|
|
|
} else {
|
|
|
|
conn.play().unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Commands::Stop {} => conn.stop().unwrap(),
|
|
|
|
|
|
|
|
Commands::Toggle {} => conn.toggle_pause().unwrap(),
|
|
|
|
|
|
|
|
Commands::Next {} => conn.next().unwrap(),
|
|
|
|
|
|
|
|
Commands::Prev {} => conn.prev().unwrap(),
|
|
|
|
|
|
|
|
Commands::List {} => print_songs(conn.queue().unwrap()),
|
|
|
|
|
|
|
|
Commands::Update {} => {
|
|
|
|
let thing = conn.update().unwrap();
|
|
|
|
println!("{}", thing)
|
|
|
|
}
|
|
|
|
|
|
|
|
Commands::Current {} => {
|
|
|
|
println!("{}", format_song(conn.currentsong().unwrap().unwrap()))
|
|
|
|
}
|
|
|
|
|
|
|
|
Commands::Search { query } => {
|
|
|
|
let mut result: Vec<String> = vec![];
|
|
|
|
let query = query.join(" ");
|
|
|
|
let window = (0, conn.stats().unwrap().songs);
|
|
|
|
conn.search(&Query::new().and(Term::Any, query), window)
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.for_each(|x| result.push(x.file.clone()));
|
|
|
|
result.iter().for_each(|x| println!("{}", x))
|
|
|
|
}
|
|
|
|
|
|
|
|
Commands::Crossfade { seconds } => {
|
|
|
|
if let Some(crossfade) = seconds {
|
|
|
|
conn.crossfade(*crossfade).unwrap()
|
|
|
|
} else {
|
|
|
|
if let Some(crossfade) = conn.status().unwrap().crossfade {
|
|
|
|
println!("Crossfade: {}", crossfade.as_secs());
|
|
|
|
} else {
|
|
|
|
println!("Crossfade disabled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-06-07 11:43:42 +02:00
|
|
|
}
|
2025-06-07 23:50:30 +02:00
|
|
|
} else {
|
|
|
|
println!("{}", format_song(conn.currentsong().unwrap().unwrap()));
|
|
|
|
println!("{:?}", conn.stats().unwrap().songs);
|
2025-06-07 11:43:42 +02:00
|
|
|
}
|
2025-06-07 11:24:20 +02:00
|
|
|
}
|
2025-06-07 23:50:30 +02:00
|
|
|
|
|
|
|
fn print_songs(songs: Vec<Song>) -> () {
|
|
|
|
songs
|
|
|
|
.iter()
|
|
|
|
.for_each(|x| println!("{}", format_song(x.clone())));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_song(song: Song) -> String {
|
|
|
|
format!("{} - {}", song.artist.unwrap(), song.title.unwrap())
|
|
|
|
}
|