remove all notes created at given date
This commit is contained in:
parent
fce453c53a
commit
f5df03fe4b
@ -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")
|
||||
|
@ -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(())
|
||||
}
|
||||
|
@ -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(())
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user