Skip to content

Commit

Permalink
Renaming files
Browse files Browse the repository at this point in the history
`PathBuf` lets us check to see if a file
exists before writing to it, so we can
take advantage of that to make sure that
we don't overwrite a file that already
exists, even if the user has indicated
a title that would overwrite another file.

```
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(garden_tmpfile, &dest)?;
        break;
    }
}
```
  • Loading branch information
ChristopherBiscardi committed Jan 16, 2021
1 parent 7d45753 commit 97a861d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/main.rs
Expand Up @@ -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"),
Expand Down
34 changes: 28 additions & 6 deletions 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"# ";

Expand All @@ -16,7 +19,7 @@ pub fn write(garden_path: PathBuf, title: Option<String>) -> 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))?;
Expand All @@ -25,10 +28,9 @@ pub fn write(garden_path: PathBuf, title: Option<String>) -> 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())
Expand All @@ -40,8 +42,28 @@ pub fn write(garden_path: PathBuf, title: Option<String>) -> 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<String> {
Expand Down

0 comments on commit 97a861d

Please sign in to comment.