option to edit, delete, view, etc with name

This commit is contained in:
aaron-jack-manning 2022-08-21 17:14:26 +10:00
parent cd0dce4168
commit 1a136a8966
3 changed files with 51 additions and 13 deletions

View File

@ -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

View File

@ -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()?;

View File

@ -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<Id, error::Error> {
match name.parse::<Id>() {
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)))),
}
}
}
}
}