diff --git a/src/main.rs b/src/main.rs index 3ece957..a928b60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,6 @@ fn main() -> Result<()> { color_eyre::install()?; let opt = Opt::from_args(); - dbg!(&opt); let garden_path = match opt.garden_path { Some(pathbuf) => Ok(pathbuf), None => get_default_garden_dir().wrap_err("`garden_path` was not supplied"), diff --git a/src/write.rs b/src/write.rs index e1885de..380156a 100644 --- a/src/write.rs +++ b/src/write.rs @@ -1,7 +1,10 @@ use color_eyre::{eyre::WrapErr, Result}; use edit::{edit_file, Builder}; -use std::io::{Read, Seek, SeekFrom, Write}; use std::path::PathBuf; +use std::{ + fs, + io::{Read, Seek, SeekFrom, Write}, +}; const TEMPLATE: &[u8; 2] = b"# "; @@ -16,7 +19,7 @@ pub fn write(garden_path: PathBuf, title: Option) -> Result<()> { file.write_all(TEMPLATE)?; // let the user write whatever they want in their favorite editor // before returning to the cli and finishing up - edit_file(filepath)?; + edit_file(&filepath)?; // Read the user's changes back from the file into a string let mut contents = String::new(); file.seek(SeekFrom::Start(0))?; @@ -25,10 +28,9 @@ pub fn write(garden_path: PathBuf, title: Option) -> Result<()> { // use `title` if the user passed it in, // otherwise try to find a heading in the markdown let document_title = title.or_else(|| { - dbg!("here"); contents .lines() - .find(|v| dbg!(v).starts_with("# ")) + .find(|v| v.starts_with("# ")) // markdown headings are required to have `# ` with // at least one space .map(|maybe_line| maybe_line.trim_start_matches("# ").to_string()) @@ -40,8 +42,28 @@ pub fn write(garden_path: PathBuf, title: Option) -> Result<()> { None => ask_for_filename(), }?; - dbg!(contents, filename); - todo!() + let mut i: usize = 0; + loop { + let dest_filename = format!( + "{}{}", + filename, + if i == 0 { + "".to_string() + } else { + i.to_string() + } + ); + let mut dest = garden_path.join(dest_filename); + dest.set_extension("md"); + if dest.exists() { + i = i + 1; + } else { + fs::rename(filepath, &dest)?; + break; + } + } + + Ok(()) } fn ask_for_filename() -> Result {