diff --git a/README.md b/README.md index 9e136ba..401fbb5 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,4 @@ Example: - [x] List all notes - [x] List all notes from a `Category` - [x] Delete all notes (`panic`) +- [x] Delete all notes created at a certain date diff --git a/src/rnote/app.rs b/src/rnote/app.rs index 4f90349..7292229 100644 --- a/src/rnote/app.rs +++ b/src/rnote/app.rs @@ -25,7 +25,13 @@ pub fn make_app() -> App<'static, 'static> { SubCommand::with_name("remove") .alias("r") .about("Remove a note.") - .arg(Arg::with_name("header").help("Name of the note.")), + .arg(Arg::with_name("header").help("Name of the note.")) + .arg( + Arg::with_name("date") + .help("Delete all notes created at given date.") + .short("d") + .long("date"), + ), ) .subcommand( SubCommand::with_name("edit") diff --git a/src/rnote/notes.rs b/src/rnote/notes.rs index 114abed..1e3d838 100644 --- a/src/rnote/notes.rs +++ b/src/rnote/notes.rs @@ -1,8 +1,8 @@ use crate::rnote::show; use anyhow::{anyhow, Result}; -use chrono::Utc; +use chrono::{DateTime, Utc}; use dialoguer::{theme::ColorfulTheme, Confirm, Select}; -use std::{env, fs, io::Write, os::unix::fs::PermissionsExt, process::Command}; +use std::{env, fs, io::Write, os::unix::fs::PermissionsExt, path::PathBuf, process::Command}; use walkdir::WalkDir; /// Get the path to the root directory of all notes. @@ -125,6 +125,7 @@ pub fn remove(header: &str) -> Result<()> { { println!("Deleting..."); fs::remove_file(path.unwrap())?; + remove_empty_dirs()?; println!("Successfully deleted."); Ok(()) } else { @@ -299,3 +300,41 @@ pub fn list_category(category: &str) -> Result<()> { } Ok(()) } + +pub fn wipe_date(date: &str) -> Result<()> { + let base = get_base_path()?; + for (_, file) in WalkDir::new(base) + .into_iter() + .filter_map(|file| file.ok()) + .enumerate() + { + if file.metadata()?.is_file() { + let time: DateTime = file.metadata()?.created()?.into(); + if time.format("%Y-%m-%d").to_string() == date { + fs::remove_file(file.path())?; + } + } + } + remove_empty_dirs()?; + + Ok(()) +} + +fn remove_empty_dirs() -> Result<()> { + let base = get_base_path()?; + for (_, file) in WalkDir::new(base) + .into_iter() + .filter_map(|file| file.ok()) + .enumerate() + { + let is_empty = PathBuf::from(file.path()) + .read_dir() + .map(|mut i| i.next().is_none()) + .unwrap_or(false); + if is_empty { + fs::remove_dir(file.path())?; + } + } + + Ok(()) +} diff --git a/src/rnote/process.rs b/src/rnote/process.rs index 819d7dc..74c3ff4 100644 --- a/src/rnote/process.rs +++ b/src/rnote/process.rs @@ -17,13 +17,24 @@ pub fn new(matches: &ArgMatches) -> Result<()> { } pub fn remove(matches: &ArgMatches) -> Result<()> { - let header = match matches.value_of("header") { - Some(s) => s.to_owned(), - None => Input::with_theme(&ColorfulTheme::default()) - .with_prompt("Name of your note") - .interact_text()?, - }; - notes::remove(&header)?; + match matches.value_of("header") { + Some(s) => notes::remove(s)?, + None => match matches.is_present("date") { + true => { + let date: String = Input::with_theme(&ColorfulTheme::default()) + .with_prompt("Date") + .interact_text()?; + notes::wipe_date(&date)?; + return Ok(()); + } + false => { + let header: String = Input::with_theme(&ColorfulTheme::default()) + .with_prompt("Name of your note") + .interact_text()?; + notes::remove(&header)?; + } + }, + } Ok(()) }