rework of remove
add remove category, no prompt for deleting a note, if rnote finds notes with the same name, it prompts which one to delete. Flags of remove subcommand now take name as argument.
This commit is contained in:
parent
9430b40cf3
commit
e87e50cfc6
@ -32,8 +32,15 @@ pub fn make_app() -> App<'static, 'static> {
|
||||
Arg::with_name("date")
|
||||
.help("Delete all notes created at given date.")
|
||||
.short("d")
|
||||
.conflicts_with("name")
|
||||
.conflicts_with("category")
|
||||
.long("date"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("category")
|
||||
.help("Delete all notes from a given category / Delete a category")
|
||||
.short("c")
|
||||
.conflicts_with("date")
|
||||
.long("category"),
|
||||
),
|
||||
)
|
||||
.subcommand(
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::rnote::show;
|
||||
use anyhow::{anyhow, Result};
|
||||
use chrono::{DateTime, Utc};
|
||||
use dialoguer::{theme::ColorfulTheme, Confirm, Select};
|
||||
use dialoguer::{theme::ColorfulTheme, Select};
|
||||
use std::{env, fs, io::Write, os::unix::fs::PermissionsExt, path::PathBuf, process::Command};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
@ -212,29 +212,40 @@ pub fn get_note_path_interractive(name: &str) -> Result<Option<String>> {
|
||||
}
|
||||
|
||||
/// Delete a note.
|
||||
pub fn remove_note(path: &str) -> Result<()> {
|
||||
pub fn remove_note(name: &str) -> Result<()> {
|
||||
let mut paths = get_note_path(name)?;
|
||||
if paths.len() == 1 {
|
||||
println!("Deleting...");
|
||||
fs::remove_file(path)?;
|
||||
fs::remove_file(paths.remove(0))?;
|
||||
remove_empty_dirs()?;
|
||||
println!("Successfully deleted.");
|
||||
Ok(())
|
||||
} else {
|
||||
let selection = Select::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt("Choose a note to delete")
|
||||
.items(&paths)
|
||||
.interact_opt()?;
|
||||
match selection {
|
||||
Some(s) => {
|
||||
fs::remove_file(paths.remove(s))?;
|
||||
remove_empty_dirs()?;
|
||||
println!("Successfully deleted.");
|
||||
Ok(())
|
||||
}
|
||||
None => {
|
||||
println!("Canceling...");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Prompt user to delete a note.
|
||||
pub fn remove_interractive(name: &str) -> Result<()> {
|
||||
let path = get_note_path_interractive(name)?;
|
||||
if path.is_none() {
|
||||
return Err(anyhow!("Abort."));
|
||||
}
|
||||
if Confirm::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt(format!("Do you want to delete {}?", name))
|
||||
.interact()?
|
||||
{
|
||||
remove_note(&path.unwrap())?;
|
||||
pub fn remove_category(category: &str) -> Result<()> {
|
||||
let path = get_category_path(category)?;
|
||||
println!("Deleting...");
|
||||
fs::remove_dir_all(path)?;
|
||||
println!("Successfully deleted.");
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("Abort."))
|
||||
}
|
||||
}
|
||||
|
||||
/// Modify a note.
|
||||
@ -389,6 +400,7 @@ pub fn remove_by_date(date: &str) -> Result<()> {
|
||||
if file.metadata()?.is_file() {
|
||||
let time: DateTime<Utc> = file.metadata()?.created()?.into();
|
||||
if time.format("%Y-%m-%d").to_string() == date {
|
||||
// TODO: add verbose flag and prompt to delete each file.
|
||||
fs::remove_file(file.path())?;
|
||||
}
|
||||
}
|
||||
|
@ -25,25 +25,31 @@ pub fn new(matches: &ArgMatches) -> Result<()> {
|
||||
|
||||
/// Process argument `remove`.
|
||||
pub fn remove(matches: &ArgMatches) -> Result<()> {
|
||||
match matches.value_of("name") {
|
||||
Some(s) => notes::remove_interractive(s)?,
|
||||
None => match matches.is_present("date") {
|
||||
true => {
|
||||
let date: String = Input::with_theme(&ColorfulTheme::default())
|
||||
if matches.is_present("date") {
|
||||
let date: String = match matches.value_of("name") {
|
||||
Some(s) => s.to_string(),
|
||||
None => Input::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt("Date")
|
||||
.interact_text()?;
|
||||
notes::remove_by_date(&date)?;
|
||||
return Ok(());
|
||||
.interact_text()?,
|
||||
};
|
||||
return notes::remove_by_date(&date);
|
||||
}
|
||||
false => {
|
||||
let name: String = Input::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt("Name of your note")
|
||||
.interact_text()?;
|
||||
notes::remove_interractive(&name)?;
|
||||
if matches.is_present("category") {
|
||||
let category: String = match matches.value_of("name") {
|
||||
Some(s) => s.to_string(),
|
||||
None => Input::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt("Category")
|
||||
.interact_text()?,
|
||||
};
|
||||
return notes::remove_category(&category);
|
||||
}
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
let name: String = match matches.value_of("name") {
|
||||
Some(s) => s.to_string(),
|
||||
None => Input::with_theme(&ColorfulTheme::default())
|
||||
.with_prompt("Name")
|
||||
.interact_text()?,
|
||||
};
|
||||
return notes::remove_note(&name);
|
||||
}
|
||||
|
||||
/// Process argument `remove`.
|
||||
|
Loading…
Reference in New Issue
Block a user