use mpd_client::{Client, Command, Frame, Subsystem};
use tokio::net::TcpStream;
use tokio::stream::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let connection = TcpStream::connect("localhost:6600").await?;
let (client, mut state_changes) = Client::connect(connection).await?;
let initial = client.command(Command::new("currentsong")).await?;
print_current_song(initial);
while let Some(subsys) = state_changes.next().await {
let subsys = subsys?;
if subsys == Subsystem::Player {
let current = client.command(Command::new("currentsong")).await?;
print_current_song(current);
}
}
Ok(())
}
fn print_current_song(response: Frame) {
let values = response.values_as_map();
if values.is_empty() {
println!("(none)");
} else {
println!(
"\"{}\" by \"{}\"",
display_value(values.get("Title")),
display_value(values.get("Artist"))
);
}
}
fn display_value<'a>(value: Option<&'_ Vec<&'a str>>) -> &'a str {
value.and_then(|v| v.first()).unwrap_or(&"(empty value)")
}