From ba06e4083e5b3026a399ea01c3f42cb8431c1c5a Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Fri, 27 Nov 2020 23:19:16 +0100 Subject: [PATCH 1/2] fstream --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index e4854c4..9217ac3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,4 @@ walkdir = "2.3.1" text_io = "0.1.8" clap = "2.33.3" dialoguer = "0.7.1" +fstream = "0.1.2" From 8a689437cf68a6a3a94dab10546a1b5061b07a00 Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Fri, 27 Nov 2020 23:19:34 +0100 Subject: [PATCH 2/2] search by word --- src/rnote/app.rs | 4 ++-- src/rnote/notes.rs | 26 ++++++++++++++++++++++++++ src/rnote/process.rs | 16 ++++++++++++++-- 3 files changed, 42 insertions(+), 4 deletions(-) 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(()) }