From 1a136a89663f6c7c02cb53b364ef628026864239 Mon Sep 17 00:00:00 2001 From: aaron-jack-manning Date: Sun, 21 Aug 2022 17:14:26 +1000 Subject: [PATCH] option to edit, delete, view, etc with name --- README.md | 3 --- src/main.rs | 25 +++++++++++++++---------- src/state.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8b6b25a..25730bc 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,6 @@ A (currently in development) to do app for the command line. - Options for which field to order by, and how to order (ascending or descending) - Options for which columns to include - If no values given, read a set of defaults from a `list.toml` file, which can be edited from a similar command -- Ability to view, edit, delete, etc. using name - - Disallow numerical names and have command automatically identify if it is a name or Id - - Error on operation if two tasks exist with the same name - Dependency tracker - Store dependencies in a file and correctly update them upon creation and removal of notes - Error if any circular dependencies are introduced diff --git a/src/main.rs b/src/main.rs index 854315f..90ef936 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,26 +37,26 @@ enum Command { }, /// Displays the specified task in detail. View { - id : Id, + id_or_name : String, }, /// Edit a note directly. Edit { - id : Id, + id_or_name : String, /// Edit the info specifically in its own file. #[clap(short, long)] info : bool, }, /// Delete a task completely. Delete { - id : Id, + id_or_name : String, }, /// Discard a task without deleting the underlying file. Discard { - id : Id, + id_or_name : String, }, /// Mark a task as complete. Complete { - id : Id, + id_or_name : String, }, /// Run Git commands at the root of the vault. #[clap(trailing_var_arg=true)] @@ -163,7 +163,8 @@ fn program() -> Result<(), error::Error> { let task = tasks::Task::new(name, info, tags, dependencies, priority, vault_folder, &mut state)?; println!("Created task {} (ID: {})", colour::task_name(&task.data.name), colour::id(&task.data.id.to_string())); }, - Delete { id } => { + Delete { id_or_name } => { + let id = state.name_or_id_to_id(&id_or_name)?; let task = tasks::Task::load(id, vault_folder, false)?; let name = task.data.name.clone(); state.index_remove(task.data.name.clone(), task.data.id); @@ -171,11 +172,13 @@ fn program() -> Result<(), error::Error> { println!("Deleted task {} (ID: {})", colour::task_name(&name), colour::id(&id.to_string())); }, - View { id } => { + View { id_or_name } => { + let id = state.name_or_id_to_id(&id_or_name)?; let task = tasks::Task::load(id, vault_folder, true)?; task.display()?; }, - Edit { id, info } => { + Edit { id_or_name, info } => { + let id = state.name_or_id_to_id(&id_or_name)?; if info { edit::edit_info(id, vault_folder.clone(), "nvim")?; } @@ -184,13 +187,15 @@ fn program() -> Result<(), error::Error> { } println!("Updated task {}", colour::id(&id.to_string())); }, - Discard { id } => { + Discard { id_or_name } => { + let id = state.name_or_id_to_id(&id_or_name)?; let mut task = tasks::Task::load(id, vault_folder, false)?; task.data.discarded = true; task.save()?; println!("Discarded task {}", colour::id(&id.to_string())); }, - Complete { id } => { + Complete { id_or_name } => { + let id = state.name_or_id_to_id(&id_or_name)?; let mut task = tasks::Task::load(id, vault_folder, false)?; task.data.complete = true; task.save()?; diff --git a/src/state.rs b/src/state.rs index 3f8f75f..f2d622e 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,5 +1,6 @@ use crate::error; use crate::tasks; +use crate::colour; use crate::tasks::Id; use std::fs; @@ -128,4 +129,39 @@ impl State { } } } + + pub fn name_or_id_to_id(&self, name : &String) -> Result { + match name.parse::() { + Ok(id) => Ok(id), + Err(_) => { + match self.data.index.get(name) { + Some(ids) => { + if ids.len() == 1 { + Ok(ids[0]) + } + else { + let coloured_ids : Vec<_> = + ids.into_iter() + .map(|i| colour::id(&i.to_string())) + .collect(); + + let mut display_ids = String::new(); + + for id in coloured_ids { + display_ids.push_str(&format!("{}, ", id)); + } + + if !display_ids.is_empty() { + display_ids.pop(); + display_ids.pop(); + } + + Err(error::Error::Generic(format!("Multiple notes (Ids: [{}]) by that name exist", display_ids))) + } + }, + None => Err(error::Error::Generic(format!("A note by the name {} does not exist", colour::task_name(&name)))), + } + } + } + } }