commit c7704fb66b24220a3116d460ea9e97b6a5d95319 from: Paul W. Rankin date: Wed Nov 15 01:56:47 2023 UTC Revert "Delete t_done.sh" This reverts commit 4a5637f26aac894ad4a4368d8f2bb5d5299c5e89. commit - f4953d174779a298ffbc77ec7b9da04d3024f921 commit + c7704fb66b24220a3116d460ea9e97b6a5d95319 blob - /dev/null blob + bfdec4fce808e6c34c559561adcee1867f3a63f5 (mode 755) --- /dev/null +++ t_done.sh @@ -0,0 +1,233 @@ +#! /usr/bin/env bash + +usage() { + cat <> "$buffer" + else + printf "%${n_width}s %s\n" "$n" "${todo#- }" >> "$buffer" + fi + (( n++ )) + done + + if (( lines <= n_total )) + then ${PAGER:-less} -X < "$buffer" + else cat "$buffer" + fi + + rm "$buffer" +} + +t_select() { + if [[ $1 =~ ^[0-9]+$ ]] + then + selection=${todo_list[(( $1 - 1 ))]} + else + local casematch + [[ ! $@ =~ [A-Z] ]] && casematch='-i' + selection=($(printf "%s\n" "${todo_list[@]}" | grep $casematch "$@" )) + fi +} + +t_done() { + t_read "$query" + t_select "$1" + + for todo in "${selection[@]}" + do + todo=$(sed 's/[][\/$*.^|]/\\&/g' <<< "$todo") + sed -i'' "/$todo/s/^- \[ ]/- \[X]/" "$todofile" + done +} + +t_kill() { + t_read "$query" + t_select "$1" + + for todo in "${selection[@]}" + do + todo=$(sed 's/[][\/$*.^|]/\\&/g' <<< "$todo") + sed -i '' "/$todo/d" "$todofile" + done +} + +t_toggle() { + t_read "$query" + t_select "$1" + + for todo in "${selection[@]}" + do + if [[ $todo =~ $re_done ]] + then + todo=$(sed 's/[][\/$*.^|]/\\&/g' <<< "$todo") + sed -i '' "/$todo/s/^- \[[xX]]/- [ ]/" "$todofile" + elif [[ $todo =~ $re_todo ]] + then + todo=$(sed 's/[][\/$*.^|]/\\&/g' <<< "$todo") + sed -i '' "/$todo/s/^- \[ ]/- [X]/" "$todofile" + fi + done +} + +t_openurl() { + t_read "$query" + t_select "$1" + + urls=($(printf "%s\n" "${selection[@]}" | grep -Eo "https?://[^ ]+")) + for url in "${urls[@]}" + do + open "$url" && echo "t: opening ${url} ..." + done +} + +while getopts ':heaDns:k:d:z:u:Tt:' opt +do + case $opt in + (h) usage ;; + (e) ${EDITOR:-vi} "$todofile" + exit 0;; + (a) showall=0;; + (D) onlydone=0;; + (n) export=0;; + (s) query=$OPTARG;; + (k) kill=$OPTARG;; + (d) markdone=$OPTARG;; + (z) toggle=$OPTARG;; + (u) openurl=$OPTARG;; + (T) due=" $(date +%F)";; + (t) due=" $(date -v $OPTARG +%F)";; + (:) printf "t: option -%s requires an argument\n" "$OPTARG" + exit 2 ;; + (*) printf "t: unrecognized option -%s\n\n" "$OPTARG" + usage ;; + esac +done + +shift "$(( OPTIND - 1 ))" + +[[ $@ =~ ^\/ ]] && query="${*#/}" + +if [[ -n $openurl ]] +then + t_openurl "$openurl" +elif [[ -n $markdone ]] +then + t_done "$markdone" +elif [[ -n $toggle ]] +then + t_toggle "$toggle" +elif [[ -n $kill ]] +then + t_kill "$kill" +elif [[ -n $query ]] +then + t_print "$query" +elif [[ -n $@ ]] +then + todo="$str_prefix$*$due" + echo $todo >> "$todofile" +else + t_print +fi