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::{
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,

View file

@ -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 => {

View file

@ -15,6 +15,13 @@ pub struct Client {
w: WriteHalf<TcpStream>,
}
#[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<bool>, // None: oneshot
pub consume: bool,
pub queue_len: usize,
pub state: Option<bool>, // Some(true): play, Some(false): pause, None: stop
pub state: PlayerState,
pub song: Option<Song>,
}
@ -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::<f32>()?.round() as u16)