From 3f07a2bbc54eb82b9bfb1e674d547d03cb4121b5 Mon Sep 17 00:00:00 2001 From: Dastan-glitch Date: Sun, 24 Jul 2022 02:06:41 +0000 Subject: [PATCH] bin/tau: use State struct instead of hardcoded states --- bin/tau/tau-cli/src/filter.rs | 13 ++++++++----- bin/tau/tau-cli/src/primitives.rs | 9 +++++++++ bin/tau/tau-cli/src/view.rs | 9 +++++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/bin/tau/tau-cli/src/filter.rs b/bin/tau/tau-cli/src/filter.rs index 338f50558..e965e26a2 100644 --- a/bin/tau/tau-cli/src/filter.rs +++ b/bin/tau/tau-cli/src/filter.rs @@ -1,18 +1,21 @@ use chrono::{Datelike, NaiveDateTime, Utc}; use serde_json::Value; -use crate::{primitives::TaskInfo, TaskEvent}; +use crate::{ + primitives::{State, TaskInfo}, + TaskEvent, +}; /// Helper function to check task's state -fn check_task_state(task: &TaskInfo, state: &str) -> bool { +fn check_task_state(task: &TaskInfo, state: State) -> bool { let last_state = task.events.last().unwrap_or(&TaskEvent::default()).action.clone(); - state == last_state + state.to_string() == last_state } pub fn apply_filter(tasks: &mut Vec, filter: &str) { match filter { - "open" => tasks.retain(|task| check_task_state(task, "open")), - "pause" => tasks.retain(|task| check_task_state(task, "pause")), + "open" => tasks.retain(|task| check_task_state(task, State::Open)), + "pause" => tasks.retain(|task| check_task_state(task, State::Pause)), _ if filter.len() == 4 && filter.parse::().is_ok() => { let (month, year) = diff --git a/bin/tau/tau-cli/src/primitives.rs b/bin/tau/tau-cli/src/primitives.rs index 2804ca2c9..f2996f970 100644 --- a/bin/tau/tau-cli/src/primitives.rs +++ b/bin/tau/tau-cli/src/primitives.rs @@ -11,6 +11,15 @@ pub enum State { Stop, } +impl State { + pub const fn is_start(&self) -> bool { + matches!(*self, Self::Start) + } + pub const fn is_pause(&self) -> bool { + matches!(*self, Self::Pause) + } +} + impl fmt::Display for State { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { diff --git a/bin/tau/tau-cli/src/view.rs b/bin/tau/tau-cli/src/view.rs index b713f535c..4694575c6 100644 --- a/bin/tau/tau-cli/src/view.rs +++ b/bin/tau/tau-cli/src/view.rs @@ -1,4 +1,4 @@ -use std::fmt::Write; +use std::{fmt::Write, str::FromStr}; use prettytable::{ cell, @@ -13,7 +13,7 @@ use darkfi::{ use crate::{ filter::apply_filter, - primitives::{Comment, TaskInfo}, + primitives::{Comment, State, TaskInfo}, TaskEvent, }; @@ -54,10 +54,11 @@ pub fn print_task_list(tasks: Vec, filters: Vec) -> Result<()> for task in tasks { let state = task.events.last().unwrap_or(&TaskEvent::default()).action.clone(); + let state = State::from_str(&state)?; - let (max_style, min_style, mid_style, gen_style) = if state == "start" { + let (max_style, min_style, mid_style, gen_style) = if state.is_start() { ("bFg", "Fc", "Fg", "Fg") - } else if state == "pause" { + } else if state.is_pause() { ("iFYBd", "iFYBd", "iFYBd", "iFYBd") } else { ("", "", "", "")