feat: make template a configurable command

Instead of taking a template string, delegate the initial content
creation to a configurable command. This allows for more powerful
configuration like carrying over old tasks.
This commit is contained in:
2023-11-14 17:18:28 +01:00
parent b217b34ba9
commit 43fe1dc50a

92
agenda
View File

@@ -25,18 +25,31 @@ AGENDA_EDITOR=${VISUAL:-${EDITOR:-vi}}
AGENDA_DIR=${AGENDA_DIR:-} AGENDA_DIR=${AGENDA_DIR:-}
# The default `-e` option. # The default `-e` option.
AGENDA_EXT=${AGENDA_EXT:-"md"} AGENDA_EXTENSION=${AGENDA_EXTENSION:-"md"}
# The default name if the `-t` option is omitted. # The default name if the `-t` option is omitted.
AGENDA_DEFAULT=${AGENDA_DEFAULT:-"agenda"} AGENDA_DEFAULT=${AGENDA_DEFAULT:-"agenda"}
# A template that is automatically written to any created agenda file (excluding # Command that provides the initial contents of new agenda files.
# backlogs). A few magic strings are replaced:
# #
# - `{date}` The agenda date (`%Y-%m-%d`) # The value is executed as a single binary, meaning that it is not possible to
# - `{name}` The agenda name # define any arguments for the given value. The command has access to the following
# - `{slug}` The agenda name (slugged) # env vars:
AGENDA_TMPL="${AGENDA_TMPL:-"# {name} {date}\\n\\n- [ ] "}" #
# - `AGENDA_DATE`
# The date of the agenda that will be created
# - `AGENDA_NAME`
# The agenda name of the agenda file that will be created.
# - `AGENDA_SLUG`
# The slugged AGENDA_NAME.
# - `AGENDA_FILE`
# The agenda file that will be created.
# - `AGENDA_LAST`
# The last agenda file dated previous to AGENDA_FILE if one exists.
# - `AGENDA_NEXT`
# The next agenda file dated next to AGENDA_FILE if one exists.
#
AGENDA_TEMPLATE=${AGENDA_TEMPLATE:-"__agenda_default_template"}
__usage() { __usage() {
fold -w <<-USAGE fold -w <<-USAGE
@@ -106,7 +119,7 @@ __edit() {
opt_b="no" opt_b="no"
opt_c="no" opt_c="no"
opt_E="no" opt_E="no"
opt_e="$AGENDA_EXT" opt_e="$AGENDA_EXTENSION"
opt_t="$AGENDA_DEFAULT" opt_t="$AGENDA_DEFAULT"
while getopts 'bcEe:t:' opt; do while getopts 'bcEe:t:' opt; do
case $opt in case $opt in
@@ -310,18 +323,67 @@ __agenda_create() {
return 1 return 1
fi fi
local content trap 'rm -f "${tmp:-}" "${tmp_err:-}"; trap - RETURN' RETURN
local tmp tmp_err
tmp=$(mktemp)
tmp_err=$(mktemp)
if [ "$backlog" = "no" ]; then if [ "$backlog" = "no" ]; then
content=$(echo "$AGENDA_TMPL" \ local last
| sed "s/{date}/$date/g" \ while read -r REPLY; do
| sed "s/{name}/$name/g" \ if [ "$REPLY" = "$file" ]; then
| sed "s/{slug}/$slug/g") break
else
last=$REPLY
fi
done < <(
{
echo "$file"
find "$AGENDA_DIR/$name" -type f
} | sort
)
# Capture exit code of the template command.
local code
set +e
AGENDA_DATE=$date AGENDA_NAME=$name AGENDA_SLUG=$slug \
AGENDA_FILE=$file AGENDA_LAST=${last:-} \
"$AGENDA_TEMPLATE" > "$tmp" 2> "$tmp_err"
code=$?
set -e
if [ $code -eq 127 ]; then
echo "${0##*/}: cannot create '$file': template '$AGENDA_TEMPLATE' does not exist" >&2
return 127
fi
if [ -s "$tmp_err" ]; then
while read -r REPLY; do
printf "agenda: $AGENDA_TEMPLATE: %s\n" "${REPLY#"${0}: line"*": "}" >&2
done < "$tmp_err"
fi
if [ $code -gt 0 ]; then
echo "${0##*/}: cannot create '$file': template error" >&2
return $code
fi
fi fi
mkdir -p "${file%/*}" mkdir -p "${file%/*}"
mv "$tmp" "$file"
}
# shellcheck disable=SC2059 __agenda_default_template() {
printf "${content:-}" > "$file" case $AGENDA_EXTENSION in
"md")
printf "# %s %s\n\n- [ ] " "$AGENDA_NAME" "$AGENDA_DATE"
;;
"txt"|"xit"|"")
printf "%s %s\n[ ] " "$AGENDA_NAME" "$AGENDA_DATE"
;;
*)
;;
esac
} }
# https://stackoverflow.com/a/49035906 # https://stackoverflow.com/a/49035906