today

what did you do today?
git clone git://git.alexkarle.com.com/today
Log | Files | Refs | README | LICENSE

commit cf394f37425fbe4601b441b5a9dac2cff70410b0 (patch)
parent 384f94cad900e4f8ebf404fc2247d60f9b5ad4e1
Author: Alex Karle <alex@alexkarle.com>
Date:   Thu,  3 Mar 2022 21:17:22 -0500

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!

Diffstat:
Atoday | 88+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 88 insertions(+), 0 deletions(-)

diff --git a/today 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 <<EOM >"$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" </tmp/today.ed + ;; + *) die "$1 isn't a number" ;; + esac +} + +case "$1" in + -e) reqinit && edlog ;; + -i) init ;; + -n) reqinit && new ;; + -l) reqinit && list "$2" ;; + *) + err "error: unknown option $1" + die "usage: today [-ein] [-l [NUM]]" + ;; +esac