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(()) +}