From 752ba0e56bf8570a93159f642739175a4bb01860 Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Fri, 27 Nov 2020 22:20:45 +0100 Subject: [PATCH] dialoguer --- src/rnote/app.rs | 2 +- src/rnote/notes.rs | 39 +++++++++++++++++---------------------- src/rnote/process.rs | 21 +++++++++------------ 3 files changed, 27 insertions(+), 35 deletions(-) diff --git a/src/rnote/app.rs b/src/rnote/app.rs index d552227..2083b85 100644 --- a/src/rnote/app.rs +++ b/src/rnote/app.rs @@ -2,7 +2,7 @@ pub use clap::{App, AppSettings, Arg, SubCommand}; pub fn make_app() -> App<'static, 'static> { App::new("rnote") - .version("0.0.0") + .version("0.0.1") .author("Volodymyr Patuta ") .about("Minimal note talking cli tool.") .setting(AppSettings::ArgRequiredElseHelp) diff --git a/src/rnote/notes.rs b/src/rnote/notes.rs index 87130ea..a69f09a 100644 --- a/src/rnote/notes.rs +++ b/src/rnote/notes.rs @@ -1,5 +1,6 @@ use anyhow::{anyhow, Result}; use chrono::Utc; +use dialoguer::{theme::ColorfulTheme, Confirm, Select}; use std::{env, fs, process::Command}; use text_io::read; use walkdir::WalkDir; @@ -67,38 +68,30 @@ fn find_path(header: &str) -> Result { let header = format!("{}.md", header); for entry in WalkDir::new(base) { let entry = entry?; - let p: &str = match entry.path().to_str() { - Some(s) => s, - None => "", + let p: String = match entry.path().to_str() { + Some(s) => s.to_owned(), + None => "".to_owned(), }; let name: &str = match entry.file_name().to_str() { Some(s) => s, None => "", }; if name == header { - paths.push(String::from(p)); + paths.push(p); } } if paths.is_empty() { Err(anyhow!("Note not found.")) } else { if paths.len() == 1 { - Ok(format!("{}.md", paths[0])) + Ok(paths.remove(0)) } else { - let mut n: usize; - loop { - let mut i = 1; - println!("Choose one: \n"); - for path in &paths { - println!("{}\t {}", i, path); - i += 1; - } - n = read!(); - if n >= 1 && n <= paths.len() { - break; - } - } - Ok(format!("{}.md", paths[n])) + let selection = Select::with_theme(&ColorfulTheme::default()) + .with_prompt("Choose a note") + .default(0) + .items(&paths) + .interact()?; + Ok(paths.remove(selection)) } } } @@ -106,9 +99,10 @@ fn find_path(header: &str) -> Result { /// Deletes a note. pub fn remove(header: &str) -> Result<()> { let path = find_path(header)?; - println!("Are you sure you want to delete {} [Y/n]", header); - let response: String = read!(); - if response == "y" || response == "Y" || response == "yes" || response == "Yes" { + if Confirm::with_theme(&ColorfulTheme::default()) + .with_prompt(format!("Do you want to delete {}?", header)) + .interact()? + { println!("Deleting..."); fs::remove_file(path)?; println!("Successfully deleted."); @@ -123,5 +117,6 @@ pub fn modify(header: &str) -> Result<()> { let editor = env::var("EDITOR").unwrap_or("/bin/vi".to_owned()); let file = find_path(header)?; Command::new(editor).arg(&file).status()?; + println!("Edited successfully!"); Ok(()) } diff --git a/src/rnote/process.rs b/src/rnote/process.rs index 76770b5..2655932 100644 --- a/src/rnote/process.rs +++ b/src/rnote/process.rs @@ -1,15 +1,15 @@ use crate::rnote::notes; use anyhow::Result; use clap::ArgMatches; +use dialoguer::{theme::ColorfulTheme, Input}; use text_io::read; pub fn new(matches: &ArgMatches) -> Result<()> { let header = match matches.value_of("header") { Some(s) => s.to_owned(), - None => { - println!("Enter the name of your note: "); - read!() - } + None => Input::with_theme(&ColorfulTheme::default()) + .with_prompt("Name of your note") + .interact_text()?, }; let category = matches.value_of("category").unwrap_or(""); @@ -30,20 +30,17 @@ pub fn remove(matches: &ArgMatches) -> Result<()> { pub fn edit(matches: &ArgMatches) -> Result<()> { let header = match matches.value_of("header") { Some(s) => s.to_owned(), - None => { - print!("Enter the name of your note: "); - read!() - } + None => Input::with_theme(&ColorfulTheme::default()) + .with_prompt("Name of your note") + .interact_text()?, }; notes::modify(&header)?; Ok(()) } pub fn list(matches: &ArgMatches) -> Result<()> { - unimplemented!("list all notes, one note or category"); - Ok(()) + unimplemented!("list all notes, one note or category {:?}", matches); } pub fn search(matches: &ArgMatches) -> Result<()> { - unimplemented!("Search a note by header or by word."); - Ok(()) + unimplemented!("Search a note by header or by word. {:?}", matches); }