Merge branch 'delete-date' into 'main'

remove all notes created at given date

See merge request paris8-rust/rnote!8
This commit is contained in:
Volodymyr Patuta 2020-12-03 11:42:38 +01:00
commit 964e420f96
4 changed files with 67 additions and 10 deletions

View File

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

View File

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

View File

@ -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<Utc> = 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(())
}

View File

@ -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(())
}