From 536624480ae70f3a3fa03f07afbea610e1048f01 Mon Sep 17 00:00:00 2001 From: fiplox <56274824+fiplox@users.noreply.github.com> Date: Mon, 31 May 2021 21:11:04 +0200 Subject: [PATCH] search by word with word as an argument And error if no files with a given word found. --- src/rnote/app.rs | 1 - src/rnote/notes.rs | 6 +++++- src/rnote/process.rs | 22 ++++++++++++---------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/rnote/app.rs b/src/rnote/app.rs index 8a52214..91cb3ce 100644 --- a/src/rnote/app.rs +++ b/src/rnote/app.rs @@ -70,7 +70,6 @@ pub fn make_app() -> App<'static, 'static> { Arg::with_name("word") .help("Search by word.") .short("w") - .conflicts_with("name") .long("word"), ) .arg(Arg::with_name("name").help("Name of the note.")), diff --git a/src/rnote/notes.rs b/src/rnote/notes.rs index cc8a5ad..1304c4b 100644 --- a/src/rnote/notes.rs +++ b/src/rnote/notes.rs @@ -145,7 +145,11 @@ pub fn get_files_by_word(word: &str) -> Result> { } } } - Ok(paths) + if paths.len() > 0 { + Ok(paths) + } else { + Err(anyhow!("No files found with word \"{}\"", word)) + } } /// Create a new note. diff --git a/src/rnote/process.rs b/src/rnote/process.rs index cb49449..3ca5ea8 100644 --- a/src/rnote/process.rs +++ b/src/rnote/process.rs @@ -84,6 +84,16 @@ pub fn list(matches: &ArgMatches) -> Result<()> { /// Process argument `search`. pub fn search(matches: &ArgMatches) -> Result<()> { + if matches.is_present("word") { + let word: String = match matches.value_of("name") { + Some(s) => s.to_string(), + None => Input::with_theme(&ColorfulTheme::default()) + .with_prompt("String to search") + .interact_text()?, + }; + return notes::search_by_word(&word); + } + match matches.value_of("name") { Some(s) => { let p = notes::get_note_path_interractive(s)?; @@ -92,18 +102,10 @@ pub fn search(matches: &ArgMatches) -> Result<()> { let editor = std::env::var("EDITOR")?; std::process::Command::new(editor).arg(s).status()?; } - None => (), + None => return Err(anyhow!("Nothing found.")), } } - None => match matches.is_present("word") { - true => { - let s: String = Input::with_theme(&ColorfulTheme::default()) - .with_prompt("String to search") - .interact_text()?; - notes::search_by_word(&s)?; - } - false => return Err(anyhow!("Nothing entered for search.")), - }, + None => return Err(anyhow!("Nothing entered for search.")), } Ok(()) }