~sotirisp/today

Added TODAY_TEMPLATE v1 PROPOSED

Tobias Schmidl: 1
 Added TODAY_TEMPLATE

 2 files changed, 12 insertions(+), 0 deletions(-)
Hi Sotiris,

Am 07.07.2024 12:51, schrieb Sotiris Papatheodorou:
Next
Hi Tobias,
Next
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sotirisp/today/patches/53687/mbox | git am -3
Learn more about email & git

[PATCH] Added TODAY_TEMPLATE Export this patch

When working with structured log files, we have to add some metadata.
The format for this metadata is always the same - which is a hindrance
for a daily journal. TODAY_TEMPLATE solves this by providing a file
which is copied to the current journal file if the latter doesn't yet
exist.

Signed-off-by: Tobias Schmidl <tobias@schmidl.dev>
---
 doc/today.1.scd | 4 ++++
 today           | 8 ++++++++
 2 files changed, 12 insertions(+)

diff --git a/doc/today.1.scd b/doc/today.1.scd
index a2ce13e..73a03ec 100644
--- a/doc/today.1.scd
+++ b/doc/today.1.scd
@@ -83,6 +83,10 @@ appropriate environment variables.
	$EDITOR "$TODAY_FILE"
	```

*TODAY_TEMPLATE*
	Copies the file from $*TODAY_TEMPLATE* to the current journal file before editing it -
	but only if the latter doesn't exist yet.

*EDITOR*
	The program used to edit journal files. Defaults to *vi*(1) if unset or
	empty.
diff --git a/today b/today
index 30c4af2..db891de 100755
--- a/today
+++ b/today
@@ -101,6 +101,14 @@ edit_file() (
		return 2
	fi
	mkdir -p "${TODAY_FILE%/*}"

	set +u
	if [ -f "$TODAY_TEMPLATE" ] && [ ! -f "$TODAY_FILE" ];
	then
		cp "$TODAY_TEMPLATE" "$TODAY_FILE"
	fi
	set -u

	set +e
	eval "$TODAY_CMD"
	set -e
-- 
2.45.2
Thank you for the patch Tobias! However, I'd rather not add a feature 
that can be achieved using existing functionality. The same thing could 
be accomplished using the $TODAY_CMD environment variable and a wrapper 
script. Here's an example:

	#!/bin/sh
	# today-template-wrapper
	# Usage: today-template-wrapper $TODAY_FILE
	template_file=~/.config/today.template
	# Use the template if the journal doesn't exist or is empty.
	if [ ! -f "$1" ] || [ ! -s "$1" ]
	then
		cp "$template_file" "$1"
	fi
	# Edit the journal file.
	$EDITOR "$1"

and using

	TODAY_CMD=today-template-wrapper "$TODAY_FILE"

where today-template-wrapper is the above script.

All the best,
Sotiris