dialoguer

This commit is contained in:
Volodymyr Patuta 2020-11-27 22:20:45 +01:00
parent ab1cfa4575
commit 752ba0e56b
3 changed files with 27 additions and 35 deletions

View File

@ -2,7 +2,7 @@ pub use clap::{App, AppSettings, Arg, SubCommand};
pub fn make_app() -> App<'static, 'static> { pub fn make_app() -> App<'static, 'static> {
App::new("rnote") App::new("rnote")
.version("0.0.0") .version("0.0.1")
.author("Volodymyr Patuta <vpatuta AT protonmail DOT com>") .author("Volodymyr Patuta <vpatuta AT protonmail DOT com>")
.about("Minimal note talking cli tool.") .about("Minimal note talking cli tool.")
.setting(AppSettings::ArgRequiredElseHelp) .setting(AppSettings::ArgRequiredElseHelp)

View File

@ -1,5 +1,6 @@
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use chrono::Utc; use chrono::Utc;
use dialoguer::{theme::ColorfulTheme, Confirm, Select};
use std::{env, fs, process::Command}; use std::{env, fs, process::Command};
use text_io::read; use text_io::read;
use walkdir::WalkDir; use walkdir::WalkDir;
@ -67,38 +68,30 @@ fn find_path(header: &str) -> Result<String> {
let header = format!("{}.md", header); let header = format!("{}.md", header);
for entry in WalkDir::new(base) { for entry in WalkDir::new(base) {
let entry = entry?; let entry = entry?;
let p: &str = match entry.path().to_str() { let p: String = match entry.path().to_str() {
Some(s) => s, Some(s) => s.to_owned(),
None => "", None => "".to_owned(),
}; };
let name: &str = match entry.file_name().to_str() { let name: &str = match entry.file_name().to_str() {
Some(s) => s, Some(s) => s,
None => "", None => "",
}; };
if name == header { if name == header {
paths.push(String::from(p)); paths.push(p);
} }
} }
if paths.is_empty() { if paths.is_empty() {
Err(anyhow!("Note not found.")) Err(anyhow!("Note not found."))
} else { } else {
if paths.len() == 1 { if paths.len() == 1 {
Ok(format!("{}.md", paths[0])) Ok(paths.remove(0))
} else { } else {
let mut n: usize; let selection = Select::with_theme(&ColorfulTheme::default())
loop { .with_prompt("Choose a note")
let mut i = 1; .default(0)
println!("Choose one: \n"); .items(&paths)
for path in &paths { .interact()?;
println!("{}\t {}", i, path); Ok(paths.remove(selection))
i += 1;
}
n = read!();
if n >= 1 && n <= paths.len() {
break;
}
}
Ok(format!("{}.md", paths[n]))
} }
} }
} }
@ -106,9 +99,10 @@ fn find_path(header: &str) -> Result<String> {
/// Deletes a note. /// Deletes a note.
pub fn remove(header: &str) -> Result<()> { pub fn remove(header: &str) -> Result<()> {
let path = find_path(header)?; let path = find_path(header)?;
println!("Are you sure you want to delete {} [Y/n]", header); if Confirm::with_theme(&ColorfulTheme::default())
let response: String = read!(); .with_prompt(format!("Do you want to delete {}?", header))
if response == "y" || response == "Y" || response == "yes" || response == "Yes" { .interact()?
{
println!("Deleting..."); println!("Deleting...");
fs::remove_file(path)?; fs::remove_file(path)?;
println!("Successfully deleted."); 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 editor = env::var("EDITOR").unwrap_or("/bin/vi".to_owned());
let file = find_path(header)?; let file = find_path(header)?;
Command::new(editor).arg(&file).status()?; Command::new(editor).arg(&file).status()?;
println!("Edited successfully!");
Ok(()) Ok(())
} }

View File

@ -1,15 +1,15 @@
use crate::rnote::notes; use crate::rnote::notes;
use anyhow::Result; use anyhow::Result;
use clap::ArgMatches; use clap::ArgMatches;
use dialoguer::{theme::ColorfulTheme, Input};
use text_io::read; use text_io::read;
pub fn new(matches: &ArgMatches) -> Result<()> { pub fn new(matches: &ArgMatches) -> Result<()> {
let header = match matches.value_of("header") { let header = match matches.value_of("header") {
Some(s) => s.to_owned(), Some(s) => s.to_owned(),
None => { None => Input::with_theme(&ColorfulTheme::default())
println!("Enter the name of your note: "); .with_prompt("Name of your note")
read!() .interact_text()?,
}
}; };
let category = matches.value_of("category").unwrap_or(""); let category = matches.value_of("category").unwrap_or("");
@ -30,20 +30,17 @@ pub fn remove(matches: &ArgMatches) -> Result<()> {
pub fn edit(matches: &ArgMatches) -> Result<()> { pub fn edit(matches: &ArgMatches) -> Result<()> {
let header = match matches.value_of("header") { let header = match matches.value_of("header") {
Some(s) => s.to_owned(), Some(s) => s.to_owned(),
None => { None => Input::with_theme(&ColorfulTheme::default())
print!("Enter the name of your note: "); .with_prompt("Name of your note")
read!() .interact_text()?,
}
}; };
notes::modify(&header)?; notes::modify(&header)?;
Ok(()) Ok(())
} }
pub fn list(matches: &ArgMatches) -> Result<()> { pub fn list(matches: &ArgMatches) -> Result<()> {
unimplemented!("list all notes, one note or category"); unimplemented!("list all notes, one note or category {:?}", matches);
Ok(())
} }
pub fn search(matches: &ArgMatches) -> Result<()> { pub fn search(matches: &ArgMatches) -> Result<()> {
unimplemented!("Search a note by header or by word."); unimplemented!("Search a note by header or by word. {:?}", matches);
Ok(())
} }