ability to play the selected song

This commit is contained in:
figsoda 2020-11-01 08:48:44 -05:00
parent 6d297fe725
commit 91d584ba3b
2 changed files with 28 additions and 0 deletions

View file

@ -45,6 +45,7 @@ enum Command {
UpdateQueue(Vec<Track>),
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 {

View file

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