search by word with word as an argument

And error if no files with a given word found.
This commit is contained in:
fiplox 2021-05-31 21:11:04 +02:00
parent beaa8a8119
commit 536624480a
3 changed files with 17 additions and 12 deletions

View File

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

View File

@ -145,7 +145,11 @@ pub fn get_files_by_word(word: &str) -> Result<Vec<String>> {
}
}
}
Ok(paths)
if paths.len() > 0 {
Ok(paths)
} else {
Err(anyhow!("No files found with word \"{}\"", word))
}
}
/// Create a new note.

View File

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