Commit: 796db4b
Parent: 1d406de

Open note after creation

Mårten Åsberg committed on 2026-07-18 at 18:49
Cargo.lock +1 -0
diff --git a/Cargo.lock b/Cargo.lock
index 8ece4c0..c55995e 100644
@@ -499,6 +499,7 @@ dependencies = [
"chrono",
"clap",
"frontmatter-gen",
"shlex",
]
[[package]]
Cargo.toml +1 -0
diff --git a/Cargo.toml b/Cargo.toml
index 7953ad7..aea7840 100644
@@ -8,3 +8,4 @@ anyhow = "1.0.103"
chrono = "0.4.45"
clap = { version = "4.6.1", features = ["derive"] }
frontmatter-gen = "0.0.6"
shlex = "2.0.1"
src/main.rs +29 -11
diff --git a/src/main.rs b/src/main.rs
index 423c701..060cb0b 100644
@@ -1,6 +1,6 @@
use anyhow::Result;
use anyhow::{Context, Result};
use frontmatter_gen::Frontmatter;
use std::fs;
use std::{env, fs, process::Command};
use clap::{Parser, Subcommand};
@@ -15,14 +15,24 @@ struct Cli {
#[derive(Subcommand)]
enum Commands {
/// Creates a new note based on the latest note in the directory
New,
New(NewArg),
}
#[derive(Parser)]
struct NewArg {
/// Creates the note file without opening it for editing.
///
/// By default the new note file is immediately opened for editing with the default EDITOR. This flag prevents the
/// note from being opened, and only creates it.
#[clap(long, action)]
no_open: bool,
}
fn main() -> Result<()> {
let args = Cli::parse();
match args.command {
Commands::New => {
Commands::New(arg) => {
let mut frontmatter = fs::read_dir(".")?
.filter_map(|e| e.ok())
.filter(|e| {
@@ -42,19 +52,27 @@ fn main() -> Result<()> {
frontmatter_gen::Value::String(now.format("%F").to_string()),
);
fs::write(
format!("{}.md", now.format("%FT%H%M%S")),
format!(
"\
let path = format!("{}.md", now.format("%FT%H%M%S"));
let contents = format!(
"\
---\n\
{}\n\
---\n\
\n\
\n\
",
frontmatter_gen::to_format(&frontmatter, frontmatter_gen::Format::Yaml)?
),
)?;
frontmatter_gen::to_format(&frontmatter, frontmatter_gen::Format::Yaml)?
);
fs::write(&path, &contents)?;
if !arg.no_open {
let editor = env::var("EDITOR").context("No EDITOR variable defined.")?;
let editor_args = shlex::split(&editor).context("Invalid EDITOR variable.")?;
_ = Command::new(&editor_args[0])
.args(&editor_args[1..])
.arg(format!("{path}:{}", contents.matches('\n').count()))
.status()?;
}
}
}