search by word

This commit is contained in:
Volodymyr Patuta 2020-11-27 23:19:34 +01:00
parent ba06e4083e
commit 8a689437cf
3 changed files with 42 additions and 4 deletions

View File

@ -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.")),
)
}

View File

@ -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(())
}

View File

@ -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(())
}