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. --- Cargo.lock | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + jt.md | 22 +++++++---- src/main.rs | 26 +++++++++++++ subplot/jt.py | 16 ++++++++ subplot/jt.yaml | 11 +++++- 6 files changed, 183 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8f97a58..2f48602 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,14 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "aho-corasick" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5" +dependencies = [ + "memchr", +] + [[package]] name = "ansi_term" version = "0.11.0" @@ -32,6 +41,12 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "clap" version = "2.33.3" @@ -47,6 +62,19 @@ dependencies = [ "vec_map", ] +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "heck" version = "0.3.1" @@ -65,11 +93,22 @@ dependencies = [ "libc", ] +[[package]] +name = "humantime" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +dependencies = [ + "quick-error", +] + [[package]] name = "jt2" version = "0.1.0" dependencies = [ "anyhow", + "log", + "pretty_env_logger", "structopt", "thiserror", ] @@ -86,6 +125,31 @@ version = "0.2.76" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3" +[[package]] +name = "log" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "memchr" +version = "2.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" + +[[package]] +name = "pretty_env_logger" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d" +dependencies = [ + "env_logger", + "log", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -119,6 +183,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quote" version = "1.0.7" @@ -128,6 +198,24 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", + "thread_local", +] + +[[package]] +name = "regex-syntax" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189" + [[package]] name = "strsim" version = "0.8.0" @@ -169,6 +257,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "termcolor" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +dependencies = [ + "winapi-util", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -198,6 +295,15 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" +dependencies = [ + "lazy_static", +] + [[package]] name = "unicode-segmentation" version = "1.6.0" @@ -244,6 +350,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 2066c46..a5617a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,5 @@ edition = "2018" structopt = "0.3" anyhow = "1" thiserror = "1" +pretty_env_logger = "0.4" +log = "0.4" diff --git a/jt.md b/jt.md index 3295e0f..2b6fadc 100644 --- a/jt.md +++ b/jt.md @@ -55,7 +55,7 @@ then command fails ~~~ -## Create a new draft and publish it +## Create a new draft, edit it, then publish it Verify that we can create a new draft entry for the journal. @@ -67,19 +67,27 @@ and there are no journal entries in jrnl when I invoke jt new "Abracadabra" --editor=none --dirname=jrnl then command is successful -then there is one draft in jrnl +and there is one draft in jrnl and draft 0 in jrnl contains "Abracadabra" -when I edit draft 0 in jrnl to also contain "Behold!" -and I invoke jt finish --dirname=jrnl +given an executable script append.sh +when I invoke jt edit --editor=./append.sh --dirname=jrnl then command is successful -then there is one journal entry in jrnl, at FILE +and draft 0 in jrnl contains "Open sesame!" + +when I invoke jt finish --dirname=jrnl +then command is successful +and there is one journal entry in jrnl, at FILE and file contains "Abracadabra" -and file contains "Behold!" +and file contains "Open sesame!" and there are no drafts in jrnl ~~~ - +~~~{#append.sh .file .numberLines} +#!/bin/sh +set -eux +echo "Open sesame!" >> "$1" +~~~ --- title: "jt—a journalling tool" 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"); diff --git a/subplot/jt.py b/subplot/jt.py index b9a7122..380cde4 100644 --- a/subplot/jt.py +++ b/subplot/jt.py @@ -2,6 +2,13 @@ import logging import os +def create_script(ctx, filename=None): + get_file = globals()["get_file"] + text = get_file(filename) + open(filename, "wb").write(text) + os.chmod(filename, 0o755) + + def run_jt_init(ctx, dirname=None, journalname=None, title=None): runcmd_run = globals()["runcmd_run"] runcmd_run(ctx, [_binary("jt"), "init", dirname, journalname, title]) @@ -24,6 +31,15 @@ def run_jt_new(ctx, title=None, dirname=None): ) +def run_jt_edit(ctx, editor=None, dirname=None): + runcmd_run = globals()["runcmd_run"] + env = dict(os.environ) + env["JT_LOG"] = "jt" + runcmd_run( + ctx, [_binary("jt"), "edit", "--dirname", dirname, "--editor", editor], env=env + ) + + def run_jt_finish(ctx, dirname=None): runcmd_run = globals()["runcmd_run"] runcmd_run(ctx, [_binary("jt"), "finish", "--dirname", dirname]) diff --git a/subplot/jt.yaml b/subplot/jt.yaml index 5ed6ed3..6d10a3a 100644 --- a/subplot/jt.yaml +++ b/subplot/jt.yaml @@ -1,3 +1,6 @@ +- given: "an executable script {filename}" + function: create_script + - when: I invoke jt init (?P\S+) (?P\S+) "(?P.*)" regex: true function: run_jt_init @@ -11,6 +14,10 @@ - when: I invoke jt new "{title:text}" --editor=none --dirname={dirname} function: run_jt_new +- when: "I invoke jt edit --editor={editor} --dirname={dirname}" + regex: false + function: run_jt_edit + - when: I invoke jt finish --dirname={dirname} function: run_jt_finish @@ -30,7 +37,7 @@ - then: there is one draft in {dirname} function: journal_has_one_draft -- then: draft {draftno} in {dirname} contains "{pattern}" +- then: draft {draftno} in {dirname} contains "{pattern:text}" function: draft_contains_string - then: there are no journal entries in {dirname} @@ -39,5 +46,5 @@ - then: there is one journal entry in {dirname}, at {variable} function: journal_has_one_entry -- then: file <{variable}> contains "{pattern}" +- then: file <{variable}> contains "{pattern:text}" function: file_contains -- cgit v1.2.1