From 91d584ba3b1747b034d8f0c38a9ae922659e6f30 Mon Sep 17 00:00:00 2001 From: figsoda Date: Sun, 1 Nov 2020 08:48:44 -0500 Subject: [PATCH] ability to play the selected song --- src/main.rs | 12 ++++++++++++ src/mpd.rs | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main.rs b/src/main.rs index c902f0d..0ac9a63 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,7 @@ enum Command { UpdateQueue(Vec), UpdateStatus, TogglePause, + Play, Down, Up, } @@ -124,6 +125,9 @@ async fn run() -> Result<()> { KeyCode::Char('p') => { tx.send(Command::TogglePause).await.unwrap_or_else(die); } + KeyCode::Enter => { + tx.send(Command::Play).await.unwrap_or_else(die); + } KeyCode::Char('j') | KeyCode::Down => { tx.send(Command::Down).await.unwrap_or_else(die); } @@ -174,6 +178,14 @@ async fn run() -> Result<()> { .context("Failed to toggle pause") .unwrap_or_else(die); } + Command::Play => { + if selected < queue.len() { + mpd::play(&mut cl, selected) + .await + .context("Failed to play the selected song") + .unwrap_or_else(die); + } + } Command::Down => { let len = queue.len(); if selected >= len { diff --git a/src/mpd.rs b/src/mpd.rs index 3e2146b..53659c2 100644 --- a/src/mpd.rs +++ b/src/mpd.rs @@ -199,3 +199,19 @@ pub async fn toggle_pause(cl: &mut Client) -> Result<()> { Ok(()) } + +pub async fn play(cl: &mut Client, pos: usize) -> Result<()> { + cl.write_all(b"play ").await?; + cl.write_all(pos.to_string().as_bytes()).await?; + cl.write_u8(b'\n').await?; + let mut lines = cl.lines(); + + while let Some(line) = lines.next_line().await? { + match line.as_bytes() { + b"OK" | expand!([@b"ACK ", ..]) => break, + _ => continue, + } + } + + Ok(()) +}