diff --git a/src/layout.rs b/src/layout.rs index d580101..c6756b9 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -11,7 +11,7 @@ use tui::{ use crate::{ app::State, config::{AddStyle, Column, Condition, Constrained, Texts, Widget}, - mpd::{Song, Status, Track}, + mpd::{PlayerState, Song, Status, Track}, }; struct FlattenState<'a, 'b> { @@ -502,9 +502,9 @@ fn eval_cond(cond: &Condition, s: &ConditionState) -> bool { Condition::Single => s.status.single == Some(true), Condition::Oneshot => s.status.single == None, Condition::Consume => s.status.consume, - Condition::Playing => s.status.state == Some(true), - Condition::Paused => s.status.state == Some(false), - Condition::Stopped => s.status.state == None, + Condition::Playing => s.status.state == PlayerState::Play, + Condition::Paused => s.status.state == PlayerState::Pause, + Condition::Stopped => s.status.state == PlayerState::Stop, Condition::TitleExist => matches!(s.current_track, Some(Track { title: Some(_), .. })), Condition::ArtistExist => matches!( s.current_track, diff --git a/src/main.rs b/src/main.rs index c704292..6ed9130 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,7 @@ use std::{ use crate::{ app::{Command, Opts, State}, layout::render, - mpd::Client, + mpd::{Client, PlayerState}, }; fn cleanup() -> Result<()> { @@ -301,9 +301,13 @@ async fn run() -> Result<()> { 0b101 } Command::TogglePause => { - cl.command(s.status.state.map_or(b"play\n", |_| b"pause\n")) - .await - .context("Failed to toggle pause")?; + cl.command(match s.status.state { + PlayerState::Play => b"play\n", + PlayerState::Pause => b"pause\n", + _ => continue, + }) + .await + .context("Failed to toggle pause")?; 0b101 } Command::Stop => { diff --git a/src/mpd.rs b/src/mpd.rs index b53b15e..9727891 100644 --- a/src/mpd.rs +++ b/src/mpd.rs @@ -15,6 +15,13 @@ pub struct Client { w: WriteHalf, } +#[derive(Debug, PartialEq)] +pub enum PlayerState { + Play, + Pause, + Stop, +} + #[derive(Debug)] pub struct Status { pub repeat: bool, @@ -22,7 +29,7 @@ pub struct Status { pub single: Option, // None: oneshot pub consume: bool, pub queue_len: usize, - pub state: Option, // Some(true): play, Some(false): pause, None: stop + pub state: PlayerState, pub song: Option, } @@ -200,7 +207,7 @@ impl Client { let mut single = None; let mut consume = None; let mut queue_len = None; - let mut state = None; + let mut state = PlayerState::Stop; let mut pos = None; let mut elapsed = None; @@ -221,8 +228,8 @@ impl Client { b"consume: 0" => consume = Some(false), b"consume: 1" => consume = Some(true), expand!([@b"playlistlength: ", ..]) => queue_len = Some(line[16 ..].parse()?), - b"state: play" => state = Some(true), - b"state: pause" => state = Some(false), + b"state: play" => state = PlayerState::Play, + b"state: pause" => state = PlayerState::Pause, expand!([@b"song: ", ..]) => pos = Some(line[6 ..].parse()?), expand!([@b"elapsed: ", ..]) => { elapsed = Some(line[9 ..].parse::()?.round() as u16)