diff --git a/src/rnote/app.rs b/src/rnote/app.rs index 2083b85..331dfd7 100644 --- a/src/rnote/app.rs +++ b/src/rnote/app.rs @@ -51,12 +51,12 @@ pub fn make_app() -> App<'static, 'static> { SubCommand::with_name("search") .alias("s") .about("Search a note.") - .arg(Arg::with_name("header").help("Name of the note.")) .arg( Arg::with_name("word") .help("Search by word.") .short("w") .long("word"), - ), + ) + .arg(Arg::with_name("header").help("Name of the note.")), ) } diff --git a/src/rnote/notes.rs b/src/rnote/notes.rs index 3a5b9a6..ce13cb8 100644 --- a/src/rnote/notes.rs +++ b/src/rnote/notes.rs @@ -120,3 +120,29 @@ pub fn modify(header: &str) -> Result<()> { println!("Edited successfully!"); Ok(()) } + +pub fn search_by_word(word: &str) -> Result<()> { + extern crate fstream; + let path = get_base_path()?; + for (_, file) in WalkDir::new(path) + .into_iter() + .filter_map(|file| file.ok()) + .enumerate() + { + if file.metadata()?.is_file() { + match fstream::contains(file.path(), word) { + Some(b) => { + if b { + let path = file.path().to_str().unwrap_or(""); + if !path.is_empty() { + println!("{}", path); + } + } + } + None => continue, + } + } + } + + Ok(()) +} diff --git a/src/rnote/process.rs b/src/rnote/process.rs index 2655932..ecdfaf8 100644 --- a/src/rnote/process.rs +++ b/src/rnote/process.rs @@ -1,5 +1,5 @@ use crate::rnote::notes; -use anyhow::Result; +use anyhow::{anyhow, Result}; use clap::ArgMatches; use dialoguer::{theme::ColorfulTheme, Input}; use text_io::read; @@ -42,5 +42,17 @@ pub fn list(matches: &ArgMatches) -> Result<()> { unimplemented!("list all notes, one note or category {:?}", matches); } pub fn search(matches: &ArgMatches) -> Result<()> { - unimplemented!("Search a note by header or by word. {:?}", matches); + match matches.value_of("header") { + Some(s) => unimplemented!("{}", s), + 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.")), + }, + } + Ok(()) }