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")
|
SubCommand::with_name("remove")
|
||||||
.alias("r")
|
.alias("r")
|
||||||
.about("Remove a note.")
|
.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(
|
||||||
SubCommand::with_name("edit")
|
SubCommand::with_name("edit")
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use crate::rnote::show;
|
use crate::rnote::show;
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use chrono::Utc;
|
use chrono::{DateTime, Utc};
|
||||||
use dialoguer::{theme::ColorfulTheme, Confirm, Select};
|
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;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
/// Get the path to the root directory of all notes.
|
/// Get the path to the root directory of all notes.
|
||||||
@ -125,6 +125,7 @@ pub fn remove(header: &str) -> Result<()> {
|
|||||||
{
|
{
|
||||||
println!("Deleting...");
|
println!("Deleting...");
|
||||||
fs::remove_file(path.unwrap())?;
|
fs::remove_file(path.unwrap())?;
|
||||||
|
remove_empty_dirs()?;
|
||||||
println!("Successfully deleted.");
|
println!("Successfully deleted.");
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
@ -299,3 +300,41 @@ pub fn list_category(category: &str) -> Result<()> {
|
|||||||
}
|
}
|
||||||
Ok(())
|
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 anyhow::{anyhow, Result};
|
||||||
use clap::ArgMatches;
|
use clap::ArgMatches;
|
||||||
use dialoguer::{theme::ColorfulTheme, Input};
|
use dialoguer::{theme::ColorfulTheme, Input};
|
||||||
@ -17,13 +17,24 @@ pub fn new(matches: &ArgMatches) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove(matches: &ArgMatches) -> Result<()> {
|
pub fn remove(matches: &ArgMatches) -> Result<()> {
|
||||||
let header = match matches.value_of("header") {
|
match matches.value_of("header") {
|
||||||
Some(s) => s.to_owned(),
|
Some(s) => notes::remove(s)?,
|
||||||
None => Input::with_theme(&ColorfulTheme::default())
|
None => match matches.is_present("date") {
|
||||||
.with_prompt("Name of your note")
|
true => {
|
||||||
.interact_text()?,
|
let date: String = Input::with_theme(&ColorfulTheme::default())
|
||||||
};
|
.with_prompt("Date")
|
||||||
notes::remove(&header)?;
|
.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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user