Преглед изворни кода

bin/tau: add start state and add more colore indicating the state

ghassmo пре 4 година
родитељ
комит
7f3c831d74

+ 5 - 12
bin/tau/tau-cli/src/main.rs

@@ -1,4 +1,4 @@
-use std::process::exit;
+use std::{process::exit, str::FromStr};
 
 use clap::{Parser, Subcommand};
 use log::error;
@@ -17,7 +17,7 @@ mod rpc;
 mod util;
 mod view;
 
-use primitives::{task_from_cli, TaskEvent};
+use primitives::{task_from_cli, State, TaskEvent};
 use util::{desc_in_editor, due_as_timestamp};
 use view::{comments_as_string, print_task_info, print_task_list};
 
@@ -87,9 +87,6 @@ async fn main() -> Result<()> {
     let rpc_client = RpcClient::new(args.endpoint).await?;
     let tau = Tau { rpc_client };
 
-    // Allowed states for a task
-    let states = ["stop", "open", "pause"];
-
     // Parse subcommands
     match args.command {
         Some(sc) => match sc {
@@ -115,14 +112,10 @@ async fn main() -> Result<()> {
             TauSubcommand::State { task_id, state } => match state {
                 Some(state) => {
                     let state = state.trim().to_lowercase();
-                    if states.contains(&state.as_str()) {
-                        tau.set_state(task_id, &state).await
+                    if let Ok(st) = State::from_str(&state) {
+                        tau.set_state(task_id, &st).await
                     } else {
-                        error!(
-                            "Task state can only be one of the following {}: {:?}",
-                            states.len(),
-                            states
-                        );
+                        error!("State can only be one of the following: open start stop pause",);
                         Ok(())
                     }
                 }

+ 37 - 2
bin/tau/tau-cli/src/primitives.rs

@@ -1,7 +1,42 @@
-use darkfi::{util::Timestamp, Result};
+use std::{fmt, str::FromStr};
+
+use darkfi::{util::Timestamp, Error, Result};
 
 use crate::due_as_timestamp;
 
+pub enum State {
+    Open,
+    Start,
+    Pause,
+    Stop,
+}
+
+impl fmt::Display for State {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        match self {
+            State::Open => write!(f, "open"),
+            State::Start => write!(f, "start"),
+            State::Stop => write!(f, "stop"),
+            State::Pause => write!(f, "pause"),
+        }
+    }
+}
+
+impl FromStr for State {
+    type Err = Error;
+
+    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
+        let result = match s.to_lowercase().as_str() {
+            "open" => State::Open,
+            "stop" => State::Stop,
+            "start" => State::Start,
+            "pause" => State::Pause,
+            _ => return Err(Error::ParseFailed("unable to parse state")),
+        };
+        Ok(result)
+    }
+}
+
 #[derive(serde::Serialize, serde::Deserialize, Debug)]
 pub struct BaseTask {
     pub title: String,
@@ -42,7 +77,7 @@ impl std::fmt::Display for TaskEvent {
 
 impl Default for TaskEvent {
     fn default() -> Self {
-        Self { action: "open".into(), timestamp: Timestamp::current_time() }
+        Self { action: State::Open.to_string(), timestamp: Timestamp::current_time() }
     }
 }
 

+ 3 - 3
bin/tau/tau-cli/src/rpc.rs

@@ -4,7 +4,7 @@ use serde_json::json;
 use darkfi::{rpc::jsonrpc::JsonRequest, Result};
 
 use crate::{
-    primitives::{BaseTask, TaskInfo},
+    primitives::{BaseTask, State, TaskInfo},
     Tau,
 };
 
@@ -45,8 +45,8 @@ impl Tau {
     }
 
     /// Set the state for a task.
-    pub async fn set_state(&self, id: u64, state: &str) -> Result<()> {
-        let req = JsonRequest::new("set_state", json!([id, state]));
+    pub async fn set_state(&self, id: u64, state: &State) -> Result<()> {
+        let req = JsonRequest::new("set_state", json!([id, state.to_string()]));
         let rep = self.rpc_client.request(req).await?;
 
         debug!("Got reply: {:?}", rep);

+ 5 - 3
bin/tau/tau-cli/src/view.rs

@@ -49,10 +49,12 @@ 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 (max_style, min_style, mid_style, gen_style) = if state == "open" {
-            ("bFC", "Fb", "Fc", "")
+        let (max_style, min_style, mid_style, gen_style) = if state == "start" {
+            ("Fc", "Fc", "Fc", "Fc")
+        } else if state == "pause" {
+            ("Fy", "Fy", "Fy", "Fy")
         } else {
-            ("iFYBd", "iFYBd", "iFYBd", "iFYBd")
+            ("", "", "", "")
         };
 
         let rank = task.rank.to_string();

+ 1 - 1
bin/tau/taud/src/jsonrpc.rs

@@ -129,7 +129,7 @@ impl JsonRpcInterface {
     // <-- {"jsonrpc": "2.0", "result": true, "id": 1}
     async fn set_state(&self, params: &[Value]) -> TaudResult<Value> {
         // Allowed states for a task
-        let states = ["stop", "open", "pause"];
+        let states = ["stop", "start", "open", "pause"];
 
         debug!(target: "tau", "JsonRpc::set_state() params {:?}", params);