From 471fad9ef42b6176be083eb876dd4fbe85ecd1c1 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 14 Nov 2020 16:28:21 +0200 Subject: feat: add "jt edit" to edit a draft with a chosen editor Also, add some debugging log statements that were needed while making this change. --- src/main.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index cb24474..c5117c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ use anyhow::Result; +use log::debug; use std::fs; use std::path::{Path, PathBuf}; +use std::process::Command; use structopt::StructOpt; use thiserror::Error; @@ -32,6 +34,17 @@ enum JT { #[structopt(help = "Title of new draft")] title: String, }, + Edit { + #[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, + }, Finish { #[structopt(long, short, help = "Use DIRNAME as the location of the journal")] dirname: PathBuf, @@ -45,6 +58,7 @@ enum JournalError { } fn main() -> Result<()> { + pretty_env_logger::init_custom_env("JT_LOG"); let opt = JT::from_args(); match opt { JT::Init { @@ -58,6 +72,7 @@ fn main() -> Result<()> { dirname, editor, } => new_draft(&title, &dirname, &editor)?, + JT::Edit { dirname, editor } => edit_draft(&dirname, &editor)?, JT::Finish { dirname } => finish_draft(&dirname)?, } Ok(()) @@ -86,6 +101,17 @@ fn new_draft(title: &str, dirname: &Path, editor: &str) -> anyhow::Result<()> { Ok(()) } +fn edit_draft(dirname: &Path, editor: &str) -> anyhow::Result<()> { + debug!("edit_draft: dirname={:?}", dirname); + debug!("edit_draft: editor={:?}", editor); + let drafts = dirname.join("drafts"); + let draft_filename = drafts.join("0.md"); + debug!("edit_draft: draft_filename={:?}", draft_filename); + let status = Command::new(editor).arg(draft_filename).status()?; + debug!("edit_draft: editor finished"); + Ok(()) +} + fn finish_draft(dirname: &Path) -> anyhow::Result<()> { let drafts = dirname.join("drafts"); let draft = drafts.join("0.md"); -- cgit v1.2.1