change state from an Option<bool> to an enum

This commit is contained in:
figsoda 2021-01-08 21:26:37 -05:00
parent 0f173bbaf1
commit ba041ca4cd
3 changed files with 23 additions and 12 deletions

View file

@ -11,7 +11,7 @@ use tui::{
use crate::{ use crate::{
app::State, app::State,
config::{AddStyle, Column, Condition, Constrained, Texts, Widget}, config::{AddStyle, Column, Condition, Constrained, Texts, Widget},
mpd::{Song, Status, Track}, mpd::{PlayerState, Song, Status, Track},
}; };
struct FlattenState<'a, 'b> { struct FlattenState<'a, 'b> {
@ -502,9 +502,9 @@ fn eval_cond(cond: &Condition, s: &ConditionState) -> bool {
Condition::Single => s.status.single == Some(true), Condition::Single => s.status.single == Some(true),
Condition::Oneshot => s.status.single == None, Condition::Oneshot => s.status.single == None,
Condition::Consume => s.status.consume, Condition::Consume => s.status.consume,
Condition::Playing => s.status.state == Some(true), Condition::Playing => s.status.state == PlayerState::Play,
Condition::Paused => s.status.state == Some(false), Condition::Paused => s.status.state == PlayerState::Pause,
Condition::Stopped => s.status.state == None, Condition::Stopped => s.status.state == PlayerState::Stop,
Condition::TitleExist => matches!(s.current_track, Some(Track { title: Some(_), .. })), Condition::TitleExist => matches!(s.current_track, Some(Track { title: Some(_), .. })),
Condition::ArtistExist => matches!( Condition::ArtistExist => matches!(
s.current_track, s.current_track,

View file

@ -41,7 +41,7 @@ use std::{
use crate::{ use crate::{
app::{Command, Opts, State}, app::{Command, Opts, State},
layout::render, layout::render,
mpd::Client, mpd::{Client, PlayerState},
}; };
fn cleanup() -> Result<()> { fn cleanup() -> Result<()> {
@ -301,7 +301,11 @@ async fn run() -> Result<()> {
0b101 0b101
} }
Command::TogglePause => { Command::TogglePause => {
cl.command(s.status.state.map_or(b"play\n", |_| b"pause\n")) cl.command(match s.status.state {
PlayerState::Play => b"play\n",
PlayerState::Pause => b"pause\n",
_ => continue,
})
.await .await
.context("Failed to toggle pause")?; .context("Failed to toggle pause")?;
0b101 0b101

View file

@ -15,6 +15,13 @@ pub struct Client {
w: WriteHalf<TcpStream>, w: WriteHalf<TcpStream>,
} }
#[derive(Debug, PartialEq)]
pub enum PlayerState {
Play,
Pause,
Stop,
}
#[derive(Debug)] #[derive(Debug)]
pub struct Status { pub struct Status {
pub repeat: bool, pub repeat: bool,
@ -22,7 +29,7 @@ pub struct Status {
pub single: Option<bool>, // None: oneshot pub single: Option<bool>, // None: oneshot
pub consume: bool, pub consume: bool,
pub queue_len: usize, pub queue_len: usize,
pub state: Option<bool>, // Some(true): play, Some(false): pause, None: stop pub state: PlayerState,
pub song: Option<Song>, pub song: Option<Song>,
} }
@ -200,7 +207,7 @@ impl Client {
let mut single = None; let mut single = None;
let mut consume = None; let mut consume = None;
let mut queue_len = None; let mut queue_len = None;
let mut state = None; let mut state = PlayerState::Stop;
let mut pos = None; let mut pos = None;
let mut elapsed = None; let mut elapsed = None;
@ -221,8 +228,8 @@ impl Client {
b"consume: 0" => consume = Some(false), b"consume: 0" => consume = Some(false),
b"consume: 1" => consume = Some(true), b"consume: 1" => consume = Some(true),
expand!([@b"playlistlength: ", ..]) => queue_len = Some(line[16 ..].parse()?), expand!([@b"playlistlength: ", ..]) => queue_len = Some(line[16 ..].parse()?),
b"state: play" => state = Some(true), b"state: play" => state = PlayerState::Play,
b"state: pause" => state = Some(false), b"state: pause" => state = PlayerState::Pause,
expand!([@b"song: ", ..]) => pos = Some(line[6 ..].parse()?), expand!([@b"song: ", ..]) => pos = Some(line[6 ..].parse()?),
expand!([@b"elapsed: ", ..]) => { expand!([@b"elapsed: ", ..]) => {
elapsed = Some(line[9 ..].parse::<f32>()?.round() as u16) elapsed = Some(line[9 ..].parse::<f32>()?.round() as u16)