From cf394f37425fbe4601b441b5a9dac2cff70410b0 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Thu, 3 Mar 2022 21:17:22 -0500 Subject: [PATCH] Add initial implementation with new/list/edit/init There's probably a lot of polish (and doc) to still do, but I wanted to get in the initial implementation! --- today | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 today diff --git a/today b/today new file mode 100755 index 0000000..484a25d --- /dev/null +++ b/today @@ -0,0 +1,88 @@ +#!/bin/sh +# today -- what did you do today? +set -e +err() { + echo "$*" 1>&2 +} + +die() { + err "$*" + exit 1 +} + +DIR=${TODAY_DIR:-$HOME/.today} +LOG=$DIR/log + +gitdo() { + git -C "$DIR" "$@" +} + +commit() { + if ! git diff --quiet; then + gitdo add "$LOG" + gitdo commit -m "$@" + else + echo "No changes, exiting" + fi +} + +init() { + mkdir -p "$DIR" + [ -d "$DIR/.git" ] && die "$DIR exists as a git repo already" + gitdo init -b main + cat <"$LOG" +today -- what did you do today? +------------------------------- +EOM + commit "Initial commit" +} + +reqinit() { + if [ ! -d "$DIR" ]; then + die "$DIR doesn't exist; init with -i first or set \$TODAY_DIR" + fi +} + +edlog() { + ed -p'ed> ' "$@" "$LOG" + commit "Auto commit" +} + +new() { + printf "\n[%s]\n" "$(date "+%F")" >> "$LOG" + edlog +} + +list() { + re='^\[[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\]$' + case "x$1" in + x) cat "$LOG" ;; + x[0-9]*) + i="$1" + N="$(grep "$re" "$LOG" | wc -l)" + if [ $i -gt $N ]; then + cat "$LOG" + return + fi + rm -f /tmp/today.ed + while [ $i -gt 0 ]; do + i=$((i - 1)) + printf "?%s?;" "$re" >>/tmp/today.ed + done + printf "\$p\nq\n" >>/tmp/today.ed + ed -s "$LOG"