From e87e50cfc64bb555b86d5585c921847d8a08723a Mon Sep 17 00:00:00 2001 From: fiplox <56274824+fiplox@users.noreply.github.com> Date: Mon, 31 May 2021 18:49:26 +0200 Subject: [PATCH] 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. --- src/rnote/app.rs | 9 +++++++- src/rnote/notes.rs | 54 +++++++++++++++++++++++++++----------------- src/rnote/process.rs | 42 +++++++++++++++++++--------------- 3 files changed, 65 insertions(+), 40 deletions(-) diff --git a/src/rnote/app.rs b/src/rnote/app.rs index 4bef840..f818924 100644 --- a/src/rnote/app.rs +++ b/src/rnote/app.rs @@ -32,8 +32,15 @@ pub fn make_app() -> App<'static, 'static> { Arg::with_name("date") .help("Delete all notes created at given date.") .short("d") - .conflicts_with("name") + .conflicts_with("category") .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( diff --git a/src/rnote/notes.rs b/src/rnote/notes.rs index 9cee5f0..0f116f2 100644 --- a/src/rnote/notes.rs +++ b/src/rnote/notes.rs @@ -1,7 +1,7 @@ use crate::rnote::show; use anyhow::{anyhow, Result}; 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 walkdir::WalkDir; @@ -212,31 +212,42 @@ pub fn get_note_path_interractive(name: &str) -> Result> { } /// Delete a note. -pub fn remove_note(path: &str) -> Result<()> { - println!("Deleting..."); - fs::remove_file(path)?; - remove_empty_dirs()?; - println!("Successfully deleted."); - Ok(()) -} - -/// 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())?; +pub fn remove_note(name: &str) -> Result<()> { + let mut paths = get_note_path(name)?; + if paths.len() == 1 { + println!("Deleting..."); + fs::remove_file(paths.remove(0))?; + remove_empty_dirs()?; + println!("Successfully deleted."); Ok(()) } 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. pub fn modify(name: &str) -> Result<()> { let editor = env::var("EDITOR")?; @@ -389,6 +400,7 @@ pub fn remove_by_date(date: &str) -> Result<()> { if file.metadata()?.is_file() { let time: DateTime = file.metadata()?.created()?.into(); if time.format("%Y-%m-%d").to_string() == date { + // TODO: add verbose flag and prompt to delete each file. fs::remove_file(file.path())?; } } diff --git a/src/rnote/process.rs b/src/rnote/process.rs index d06eb26..0b249e4 100644 --- a/src/rnote/process.rs +++ b/src/rnote/process.rs @@ -25,25 +25,31 @@ pub fn new(matches: &ArgMatches) -> Result<()> { /// Process argument `remove`. pub fn remove(matches: &ArgMatches) -> Result<()> { - match matches.value_of("name") { - Some(s) => notes::remove_interractive(s)?, - None => match matches.is_present("date") { - true => { - let date: String = Input::with_theme(&ColorfulTheme::default()) - .with_prompt("Date") - .interact_text()?; - 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)?; - } - }, + if matches.is_present("date") { + let date: String = match matches.value_of("name") { + Some(s) => s.to_string(), + None => Input::with_theme(&ColorfulTheme::default()) + .with_prompt("Date") + .interact_text()?, + }; + return notes::remove_by_date(&date); } - 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`.