mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
bin/tau: use State struct instead of hardcoded states
This commit is contained in:
@@ -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<TaskInfo>, 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::<u32>().is_ok() => {
|
||||
let (month, year) =
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<TaskInfo>, filters: Vec<String>) -> 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 {
|
||||
("", "", "", "")
|
||||
|
||||
Reference in New Issue
Block a user