From f5df03fe4bec8df5df5439ee3776dd287eb89ffb Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Thu, 3 Dec 2020 11:10:41 +0100 Subject: [PATCH] remove all notes created at given date --- src/rnote/app.rs | 8 +++++++- src/rnote/notes.rs | 43 +++++++++++++++++++++++++++++++++++++++++-- src/rnote/process.rs | 27 +++++++++++++++++++-------- 3 files changed, 67 insertions(+), 11 deletions(-) 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..396f53f 100644 --- a/src/rnote/process.rs +++ b/src/rnote/process.rs @@ -1,4 +1,4 @@ -use crate::rnote::notes; +use crate::rnote::{app, notes}; use anyhow::{anyhow, Result}; use clap::ArgMatches; use dialoguer::{theme::ColorfulTheme, Input}; @@ -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(()) }