summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index 4e0a887..cb24474 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -19,6 +19,23 @@ enum JT {
#[structopt(help = "Directory that may or may not be a journal")]
dirname: PathBuf,
},
+ New {
+ #[structopt(long, short, help = "Use DIRNAME as the location of the journal")]
+ dirname: PathBuf,
+ #[structopt(
+ long,
+ short,
+ help = "Invoke EDITOR for user to edit draft",
+ default_value = "/usr/bin/editor"
+ )]
+ editor: String,
+ #[structopt(help = "Title of new draft")]
+ title: String,
+ },
+ Finish {
+ #[structopt(long, short, help = "Use DIRNAME as the location of the journal")]
+ dirname: PathBuf,
+ },
}
#[derive(Debug, Error)]
@@ -36,6 +53,12 @@ fn main() -> Result<()> {
description,
} => init(&dirname, &journalname, &description)?,
JT::IsJournal { dirname } => is_journal(&dirname)?,
+ JT::New {
+ title,
+ dirname,
+ editor,
+ } => new_draft(&title, &dirname, &editor)?,
+ JT::Finish { dirname } => finish_draft(&dirname)?,
}
Ok(())
}
@@ -52,3 +75,27 @@ fn is_journal(dirname: &Path) -> anyhow::Result<()> {
}
Ok(())
}
+
+fn new_draft(title: &str, dirname: &Path, editor: &str) -> anyhow::Result<()> {
+ let drafts = dirname.join("drafts");
+ if !drafts.exists() {
+ std::fs::create_dir(&drafts)?;
+ }
+ let draft_filename = drafts.join("0.md");
+ std::fs::write(draft_filename, title)?;
+ Ok(())
+}
+
+fn finish_draft(dirname: &Path) -> anyhow::Result<()> {
+ let drafts = dirname.join("drafts");
+ let draft = drafts.join("0.md");
+
+ let entries = dirname.join("entries");
+ if !entries.exists() {
+ std::fs::create_dir(&entries)?;
+ }
+ let entry = entries.join("0.md");
+
+ std::fs::rename(draft, entry)?;
+ Ok(())
+}