From 12d9b54ea2a578a0996f145514b0d7591db2defd Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Sun, 29 Nov 2020 22:50:48 +0100 Subject: [PATCH] search by header optionally open --- src/rnote/notes.rs | 51 +++++++++++++++++++++++++++++--------------- src/rnote/process.rs | 17 ++++++--------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/rnote/notes.rs b/src/rnote/notes.rs index c613a26..3df9b63 100644 --- a/src/rnote/notes.rs +++ b/src/rnote/notes.rs @@ -64,7 +64,7 @@ fn is_duplicate(header: &str, category: &str) -> Result<()> { } /// Finds a path to desired note. -pub fn find_path(header: &str) -> Result { +pub fn find_path(header: &str) -> Result> { let mut paths: Vec = Vec::new(); let base = get_base_path()?; let header = format!("{}.md", header); @@ -86,14 +86,17 @@ pub fn find_path(header: &str) -> Result { Err(anyhow!("Note not found.")) } else { if paths.len() == 1 { - Ok(paths.remove(0)) + Ok(Some(paths.remove(0))) } else { let selection = Select::with_theme(&ColorfulTheme::default()) - .with_prompt("Choose a note") + .with_prompt("Optionally choose a note") .default(0) .items(&paths) - .interact()?; - Ok(paths.remove(selection)) + .interact_opt()?; + match selection { + Some(s) => Ok(Some(paths.remove(s))), + None => Ok(None), + } } } } @@ -104,9 +107,10 @@ pub fn remove(header: &str) -> Result<()> { if Confirm::with_theme(&ColorfulTheme::default()) .with_prompt(format!("Do you want to delete {}?", header)) .interact()? + && path.is_some() { println!("Deleting..."); - fs::remove_file(path)?; + fs::remove_file(path.unwrap())?; println!("Successfully deleted."); Ok(()) } else { @@ -118,21 +122,23 @@ pub fn remove(header: &str) -> Result<()> { pub fn modify(header: &str) -> Result<()> { let editor = env::var("EDITOR")?; let file = find_path(header)?; - Command::new(editor).arg(&file).status()?; - println!("Edited successfully!"); - Ok(()) -} - -fn print_path(path: String) { - let off = path.find(".rnote/").unwrap_or(path.len()) + 7; - let mut path = path; - path.drain(..off); - println!("{}", path); + match file { + Some(f) => { + Command::new(editor).arg(f).status()?; + println!("Edited successfully!"); + Ok(()) + } + None => { + println!("Abort."); + Ok(()) + } + } } pub fn search_by_word(word: &str) -> Result<()> { extern crate fstream; let path = get_base_path()?; + let mut paths: Vec = Vec::new(); for (_, file) in WalkDir::new(path) .into_iter() .filter_map(|file| file.ok()) @@ -144,7 +150,7 @@ pub fn search_by_word(word: &str) -> Result<()> { if b { let path = file.path().to_str().unwrap_or(""); if !path.is_empty() { - print_path(path.to_owned()); + paths.push(path.to_owned()); } } } @@ -152,6 +158,17 @@ pub fn search_by_word(word: &str) -> Result<()> { } } } + let selection = Select::with_theme(&ColorfulTheme::default()) + .with_prompt("Optionally choose a note") + .default(0) + .items(&paths) + .interact_opt()?; + if let Some(selection) = selection { + let editor = std::env::var("EDITOR")?; + std::process::Command::new(editor) + .arg(paths.remove(selection)) + .status()?; + } Ok(()) } diff --git a/src/rnote/process.rs b/src/rnote/process.rs index ffd6dd8..cb4b730 100644 --- a/src/rnote/process.rs +++ b/src/rnote/process.rs @@ -1,7 +1,7 @@ use crate::rnote::notes; use anyhow::{anyhow, Result}; use clap::ArgMatches; -use dialoguer::{theme::ColorfulTheme, Confirm, Input}; +use dialoguer::{theme::ColorfulTheme, Input}; pub fn new(matches: &ArgMatches) -> Result<()> { let header = match matches.value_of("header") { @@ -47,15 +47,12 @@ pub fn search(matches: &ArgMatches) -> Result<()> { match matches.value_of("header") { Some(s) => { let p = notes::find_path(s)?; - if Confirm::with_theme(&ColorfulTheme::default()) - .with_prompt("Do you want to open it?") - .default(true) - .interact()? - { - let editor = std::env::var("EDITOR")?; - std::process::Command::new(editor).arg(&p).status()?; - } else { - println!("{}", p); + match p { + Some(s) => { + let editor = std::env::var("EDITOR")?; + std::process::Command::new(editor).arg(s).status()?; + } + None => (), } } None => match matches.is_present("word") {