rework of remove

add remove category, no prompt for deleting a note, if rnote finds notes
with the same name, it prompts which one to delete. Flags of remove
subcommand now take name as argument.
This commit is contained in:
fiplox 2021-05-31 18:49:26 +02:00
parent 9430b40cf3
commit e87e50cfc6
3 changed files with 65 additions and 40 deletions

View File

@ -32,8 +32,15 @@ pub fn make_app() -> App<'static, 'static> {
Arg::with_name("date") Arg::with_name("date")
.help("Delete all notes created at given date.") .help("Delete all notes created at given date.")
.short("d") .short("d")
.conflicts_with("name") .conflicts_with("category")
.long("date"), .long("date"),
)
.arg(
Arg::with_name("category")
.help("Delete all notes from a given category / Delete a category")
.short("c")
.conflicts_with("date")
.long("category"),
), ),
) )
.subcommand( .subcommand(

View File

@ -1,7 +1,7 @@
use crate::rnote::show; use crate::rnote::show;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use dialoguer::{theme::ColorfulTheme, Confirm, Select}; use dialoguer::{theme::ColorfulTheme, Select};
use std::{env, fs, io::Write, os::unix::fs::PermissionsExt, path::PathBuf, process::Command}; use std::{env, fs, io::Write, os::unix::fs::PermissionsExt, path::PathBuf, process::Command};
use walkdir::WalkDir; use walkdir::WalkDir;
@ -212,31 +212,42 @@ pub fn get_note_path_interractive(name: &str) -> Result<Option<String>> {
} }
/// Delete a note. /// Delete a note.
pub fn remove_note(path: &str) -> Result<()> { pub fn remove_note(name: &str) -> Result<()> {
println!("Deleting..."); let mut paths = get_note_path(name)?;
fs::remove_file(path)?; if paths.len() == 1 {
remove_empty_dirs()?; println!("Deleting...");
println!("Successfully deleted."); fs::remove_file(paths.remove(0))?;
Ok(()) remove_empty_dirs()?;
} println!("Successfully deleted.");
/// Prompt user to delete a note.
pub fn remove_interractive(name: &str) -> Result<()> {
let path = get_note_path_interractive(name)?;
if path.is_none() {
return Err(anyhow!("Abort."));
}
if Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(format!("Do you want to delete {}?", name))
.interact()?
{
remove_note(&path.unwrap())?;
Ok(()) Ok(())
} else { } else {
Err(anyhow!("Abort.")) let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Choose a note to delete")
.items(&paths)
.interact_opt()?;
match selection {
Some(s) => {
fs::remove_file(paths.remove(s))?;
remove_empty_dirs()?;
println!("Successfully deleted.");
Ok(())
}
None => {
println!("Canceling...");
Ok(())
}
}
} }
} }
pub fn remove_category(category: &str) -> Result<()> {
let path = get_category_path(category)?;
println!("Deleting...");
fs::remove_dir_all(path)?;
println!("Successfully deleted.");
Ok(())
}
/// Modify a note. /// Modify a note.
pub fn modify(name: &str) -> Result<()> { pub fn modify(name: &str) -> Result<()> {
let editor = env::var("EDITOR")?; let editor = env::var("EDITOR")?;
@ -389,6 +400,7 @@ pub fn remove_by_date(date: &str) -> Result<()> {
if file.metadata()?.is_file() { if file.metadata()?.is_file() {
let time: DateTime<Utc> = file.metadata()?.created()?.into(); let time: DateTime<Utc> = file.metadata()?.created()?.into();
if time.format("%Y-%m-%d").to_string() == date { if time.format("%Y-%m-%d").to_string() == date {
// TODO: add verbose flag and prompt to delete each file.
fs::remove_file(file.path())?; fs::remove_file(file.path())?;
} }
} }

View File

@ -25,25 +25,31 @@ pub fn new(matches: &ArgMatches) -> Result<()> {
/// Process argument `remove`. /// Process argument `remove`.
pub fn remove(matches: &ArgMatches) -> Result<()> { pub fn remove(matches: &ArgMatches) -> Result<()> {
match matches.value_of("name") { if matches.is_present("date") {
Some(s) => notes::remove_interractive(s)?, let date: String = match matches.value_of("name") {
None => match matches.is_present("date") { Some(s) => s.to_string(),
true => { None => Input::with_theme(&ColorfulTheme::default())
let date: String = Input::with_theme(&ColorfulTheme::default()) .with_prompt("Date")
.with_prompt("Date") .interact_text()?,
.interact_text()?; };
notes::remove_by_date(&date)?; return notes::remove_by_date(&date);
return Ok(());
}
false => {
let name: String = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Name of your note")
.interact_text()?;
notes::remove_interractive(&name)?;
}
},
} }
Ok(()) if matches.is_present("category") {
let category: String = match matches.value_of("name") {
Some(s) => s.to_string(),
None => Input::with_theme(&ColorfulTheme::default())
.with_prompt("Category")
.interact_text()?,
};
return notes::remove_category(&category);
}
let name: String = match matches.value_of("name") {
Some(s) => s.to_string(),
None => Input::with_theme(&ColorfulTheme::default())
.with_prompt("Name")
.interact_text()?,
};
return notes::remove_note(&name);
} }
/// Process argument `remove`. /// Process argument `remove`.