diff --git a/Cargo.lock b/Cargo.lock index 92351f0..50a57b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -180,6 +180,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "deunicode" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "850878694b7933ca4c9569d30a34b55031b9b139ee1fc7b94a527c4ef960d690" + [[package]] name = "difference" version = "2.0.0" @@ -194,6 +200,8 @@ dependencies = [ "color-eyre", "directories", "edit", + "rprompt", + "slug", "structopt", ] @@ -512,6 +520,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "rprompt" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b386f4748bdae2aefc96857f5fda07647f851d089420e577831e2a14b45230f8" + [[package]] name = "rust-argon2" version = "0.8.3" @@ -607,6 +621,15 @@ dependencies = [ "loom", ] +[[package]] +name = "slug" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3bc762e6a4b6c6fcaade73e77f9ebc6991b676f88bb2358bddb56560f073373" +dependencies = [ + "deunicode", +] + [[package]] name = "strsim" version = "0.8.0" diff --git a/Cargo.toml b/Cargo.toml index a5054eb..76caa8e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,8 @@ path = "src/main.rs" color-eyre = "0.5.10" directories = "3.0.1" edit = "0.1.2" +rprompt = "1.0.5" +slug = "0.1.4" structopt = "0.3.21" [dev-dependencies] diff --git a/src/write.rs b/src/write.rs index 46efbfa..e1885de 100644 --- a/src/write.rs +++ b/src/write.rs @@ -34,6 +34,49 @@ pub fn write(garden_path: PathBuf, title: Option) -> Result<()> { .map(|maybe_line| maybe_line.trim_start_matches("# ").to_string()) }); - dbg!(contents, document_title); + // get the filename to use for the file + let filename = match document_title { + Some(raw_title) => confirm_filename(&raw_title), + None => ask_for_filename(), + }?; + + dbg!(contents, filename); todo!() } + +fn ask_for_filename() -> Result { + rprompt::prompt_reply_stderr( + "\ +Enter filename +> ", + ) + .wrap_err("Failed to get filename") + .map(|title| slug::slugify(title)) +} + +fn confirm_filename(raw_title: &str) -> Result { + loop { + // prompt defaults to uppercase character in question + // this is a convention, not a requirement enforced by + // the code + let result = rprompt::prompt_reply_stderr(&format!( + "\ +current title: `{}` +Do you want a different title? (y/N): ", + raw_title, + )) + .wrap_err("Failed to get input for y/n question")?; + + match result.as_str() { + "y" | "Y" => break ask_for_filename(), + "n" | "N" | "" => { + // the capital N in the prompt means "default", + // so we handle "" as input here + break Ok(slug::slugify(raw_title)); + } + _ => { + // ask again because something went wrong + } + }; + } +}