diff --git a/agenda b/agenda index b4ad919..871aafe 100644 --- a/agenda +++ b/agenda @@ -25,18 +25,31 @@ AGENDA_EDITOR=${VISUAL:-${EDITOR:-vi}} AGENDA_DIR=${AGENDA_DIR:-} # The default `-e` option. -AGENDA_EXT=${AGENDA_EXT:-"md"} +AGENDA_EXTENSION=${AGENDA_EXTENSION:-"md"} # The default name if the `-t` option is omitted. AGENDA_DEFAULT=${AGENDA_DEFAULT:-"agenda"} -# A template that is automatically written to any created agenda file (excluding -# backlogs). A few magic strings are replaced: +# Command that provides the initial contents of new agenda files. # -# - `{date}` The agenda date (`%Y-%m-%d`) -# - `{name}` The agenda name -# - `{slug}` The agenda name (slugged) -AGENDA_TMPL="${AGENDA_TMPL:-"# {name} {date}\\n\\n- [ ] "}" +# The value is executed as a single binary, meaning that it is not possible to +# define any arguments for the given value. The command has access to the following +# env vars: +# +# - `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() { fold -w <<-USAGE @@ -106,7 +119,7 @@ __edit() { opt_b="no" opt_c="no" opt_E="no" - opt_e="$AGENDA_EXT" + opt_e="$AGENDA_EXTENSION" opt_t="$AGENDA_DEFAULT" while getopts 'bcEe:t:' opt; do case $opt in @@ -310,18 +323,67 @@ __agenda_create() { return 1 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 - content=$(echo "$AGENDA_TMPL" \ - | sed "s/{date}/$date/g" \ - | sed "s/{name}/$name/g" \ - | sed "s/{slug}/$slug/g") + local last + while read -r REPLY; do + if [ "$REPLY" = "$file" ]; then + 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 mkdir -p "${file%/*}" + mv "$tmp" "$file" +} - # shellcheck disable=SC2059 - printf "${content:-}" > "$file" +__agenda_default_template() { + 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