diff options
| author | Hsieh Chin Fan <pham@topo.tw> | 2023-02-14 13:33:23 +0800 |
|---|---|---|
| committer | Hsieh Chin Fan <pham@topo.tw> | 2023-02-14 13:33:23 +0800 |
| commit | 6fae25b305d714b3ab7608fa003f1af9bf024545 (patch) | |
| tree | 05507b2c0505659d2fd847ecce988dacab63a236 /bin/wiki | |
| parent | 41ad31a2dee9ff912f222652f022b4c55cddcbf7 (diff) | |
Rename tools into bin
Diffstat (limited to 'bin/wiki')
| -rwxr-xr-x | bin/wiki/diary | 49 | ||||
| -rwxr-xr-x | bin/wiki/log.sh | 31 | ||||
| -rwxr-xr-x | bin/wiki/notify | 7 |
3 files changed, 87 insertions, 0 deletions
diff --git a/bin/wiki/diary b/bin/wiki/diary new file mode 100755 index 0000000..3995ecf --- /dev/null +++ b/bin/wiki/diary | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | #! /bin/bash | ||
| 2 | |||
| 3 | today=~/log/`date +%y.w%W.md` | ||
| 4 | |||
| 5 | specify_date() { | ||
| 6 | YEAR=${YEAR:-22} | ||
| 7 | |||
| 8 | while read -e -p 'Month? ' -r MONTH </dev/tty; do | ||
| 9 | [ -z $MONTH ] && MONTH=$(date +%m) | ||
| 10 | MONTH=$(printf "%02d" $MONTH) | ||
| 11 | date -d $YEAR-$MONTH-01 &>/dev/null && break | ||
| 12 | done | ||
| 13 | |||
| 14 | while read -e -p 'Day? ' -r DAY </dev/tty; do | ||
| 15 | [ -z $DAY ] && DAY=$(date +%d) | ||
| 16 | DAY=$(printf "%02d" $DAY) | ||
| 17 | date -d $YEAR-$MONTH-$DAY &>/dev/null && break | ||
| 18 | done | ||
| 19 | |||
| 20 | FILE=~/log/diary/20$YEAR-$MONTH-$DAY.md | ||
| 21 | vim -c 'Goyo' $FILE | ||
| 22 | } | ||
| 23 | |||
| 24 | print_today() { | ||
| 25 | [[ ! -e $today ]] && touch $today | ||
| 26 | cat $today | ||
| 27 | } | ||
| 28 | |||
| 29 | edit_today() { | ||
| 30 | vim -c Goyo $today | ||
| 31 | } | ||
| 32 | |||
| 33 | add_entry() { | ||
| 34 | [[ $# -ne 0 ]] && echo - "$@" >>$today | ||
| 35 | while read -e -r entry; do | ||
| 36 | echo $entry | sed -E 's/\t/ /g; s/(^[[:space:]]*)/\1- /' >>$today | ||
| 37 | done | ||
| 38 | } | ||
| 39 | |||
| 40 | case "$1" in | ||
| 41 | specify) | ||
| 42 | specify_date ;; | ||
| 43 | print) | ||
| 44 | print_today ;; | ||
| 45 | today) | ||
| 46 | edit_today ;; | ||
| 47 | *) | ||
| 48 | add_entry "$@" ;; | ||
| 49 | esac | ||
diff --git a/bin/wiki/log.sh b/bin/wiki/log.sh new file mode 100755 index 0000000..4ef2c27 --- /dev/null +++ b/bin/wiki/log.sh | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | #! /bin/bash | ||
| 2 | |||
| 3 | # Restore mail in variable | ||
| 4 | MAIL="$(cat)" | ||
| 5 | |||
| 6 | # Only execute the following script when mail receiver is log@topo.tw | ||
| 7 | grep -qE "^X-Original-To: .*log@topo.tw[>]?$" <<<"$MAIL" || exit 0 | ||
| 8 | # A little hacky way to check if mail is sent from me | ||
| 9 | sed -nE '/^Received: /p;/^$/q' <<<"$(MAIL)" | wc -l | xargs -i test {} -lt 2 || exit 0 | ||
| 10 | |||
| 11 | # Leave log | ||
| 12 | date >>~/Downloads/log.log | ||
| 13 | echo $$ >>~/Downloads/log.log | ||
| 14 | awk -v RS= 'NR>1' <<<"$MAIL" >>~/Downloads/log.log | ||
| 15 | |||
| 16 | LOG=~/log/`date +%y.w%W.md` | ||
| 17 | TODAY="`date '+%a %b.%d'`" | ||
| 18 | |||
| 19 | grep -Eq "^## ${TODAY}$" ${LOG} || \ | ||
| 20 | cat <<EOF >>${LOG} | ||
| 21 | |||
| 22 | |||
| 23 | ## $TODAY | ||
| 24 | EOF | ||
| 25 | |||
| 26 | |||
| 27 | # Save content to log file of current week | ||
| 28 | echo >>${LOG} | ||
| 29 | awk -v RS= 'NR>1' <<<"$MAIL" >>${LOG} | ||
| 30 | |||
| 31 | { cd ~/log && git add `basename ${LOG}` && git commit -m "Update by mail"; } >>~/Downloads/log.log | ||
diff --git a/bin/wiki/notify b/bin/wiki/notify new file mode 100755 index 0000000..6e510d8 --- /dev/null +++ b/bin/wiki/notify | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | #! /bin/sh | ||
| 2 | |||
| 3 | exec >/dev/pts/$(ls /dev/pts -t | head -1) | ||
| 4 | |||
| 5 | echo | ||
| 6 | echo | ||
| 7 | cat | ||