From c5a287253462a859843fff0c1e5d6f9a34436e05 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Thu, 29 Aug 2024 11:06:56 +0300 Subject: [PATCH] feat: enable setting specific state with label --- src/main.rs | 15 +++++++++++++-- src/task.rs | 19 +++++++++++-------- src/tasks.rs | 2 +- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6f81cbe..2f955e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -530,9 +530,20 @@ async fn main() -> Result<()> { Some('!') => match tasks.get_position() { - None => warn!("First select a task to set its state!"), + None => { + warn!("First select a task to set its state!"); + info!("Usage: ![(Open|Procedure|Pending|Done|Closed): ][Statename]"); + } Some(id) => { - tasks.set_state_for_with(id, arg_default); + 'block: { + if let Some((left, right)) = arg_default.split_once(": ") { + if let Ok(state) = left.try_into() { + tasks.set_state_for(id, right, state); + break 'block; + } + } + tasks.set_state_for_with(id, arg_default); + } tasks.move_up(); } } diff --git a/src/task.rs b/src/task.rs index 25f4b61..0e264ea 100644 --- a/src/task.rs +++ b/src/task.rs @@ -233,14 +233,17 @@ pub(crate) enum State { /// Actionable ordered task list Procedure = PROCEDURE_KIND as isize, } -impl From<&str> for State { - fn from(value: &str) -> Self { - match value { - "Closed" => State::Closed, - "Done" => State::Done, - "Pending" => State::Pending, - "Proc" | "Procedure" | "List" => State::Procedure, - _ => State::Open, +impl TryFrom<&str> for State { + type Error = (); + + fn try_from(value: &str) -> Result { + match value.to_ascii_lowercase().as_str() { + "closed" => Ok(State::Closed), + "done" => Ok(State::Done), + "pending" => Ok(State::Pending), + "proc" | "procedure" | "list" => Ok(State::Procedure), + "open" => Ok(State::Open), + _ => Err(()), } } } diff --git a/src/tasks.rs b/src/tasks.rs index 4ae1a1d..044ce74 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -856,7 +856,7 @@ impl Tasks { } pub(crate) fn set_state_for_with(&mut self, id: EventId, comment: &str) { - self.set_state_for(id, comment, comment.into()); + self.set_state_for(id, comment, comment.try_into().unwrap_or(State::Open)); } pub(crate) fn set_state_for(&mut self, id: EventId, comment: &str, state: State) -> EventId {