#!/bin/bash # Copyright (C) 2019 Ian Kelling # SPDX-License-Identifier: AGPL-3.0-or-later if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi source /a/bin/errhandle/err shopt -s nullglob shopt -s dotglob shopt -s nullglob for p in ~/.gem/ruby/*/bin; do PATH="$PATH:$p" done todaycache=/tmp/timetraptoday newer() { if [[ ! -e $todaycache ]]; then return 1 fi ! find $todaycache -mmin $1 -type f -exec false {} + &>/dev/null } td() { /a/opt/timetrap/bin/t d -ftotal all -m '^w|x$' "$@"; } main() { ps_char= if [[ -e /dev/shm/iank-status ]]; then eval $(< /dev/shm/iank-status) fi # clocked in or out? cursheet=$(sqlite3 /p/.timetrap.db "select sheet from entries where end is NULL;") if [[ $cursheet ]]; then if i3-msg -t get_bar_config bar-0 | grep '"mode":"dock"' &>/dev/null; then i3-msg -q 'bar mode hide' fi else if i3-msg -t get_bar_config bar-0 | grep '"mode":"hide"' &>/dev/null; then i3-msg -q 'bar mode dock' fi cursheet=out fi # maybe sometime show work and x separate if newer 3; then today=$(< /tmp/timetraptoday) elif newer 10; then today=$(< /tmp/timetraptoday) td -s today >/tmp/timetraptoday & else today=$(td -s today) echo "$today" >/tmp/timetraptoday fi # hours remaining in the day: # work+study in the last week, not counting today. # no arg to printf for the intentional behavior of no arg # shellcheck disable=SC2183 cache=/tmp/timetrap$(printf '%(%F)T') if [[ -e $cache ]]; then eval $(<$cache) else week=$(td -s '8 days ago' -e 'yesterday') printf 'week="%s"\n' "$week" >$cache week4=$(td -s '29 days ago' -e 'yesterday') printf 'week4="%s"\n' "$week4" >>$cache fi # weekly work+study in the last 4 weeks, not counting today. # todo later: # 4 weeks # ($x4week + $w4week + .05) /4 #weekly work+study in the last 16 weeks, not counting today. #weekly work+study in the last 52 weeks, not counting today. unixtime=$EPOCHSECONDS midnight=$(date +%s -d 'tomorrow 00:00') tmp=($(echo "scale=1; today_left=($midnight-$unixtime) / 60 / 60; today_left; done_today=($today + .05) /1; /* hours I could do today by working 80% of the remainder time */ today_left * .8 + done_today; done_today; ($week + .05) /1; ($week4 + .05) /4"|bc)) printf "[ { \"full_text\": \"" if [[ $ps_char ]]; then printf "%s| " "$ps_char" fi printf "%s | left today %s %s | today %s | week %s | 4weeks %s | %s\" } ],\n" "$cursheet" "${tmp[@]}" "$(date "+%A, %B %d, %r")" } # pass any arg and we just run once if (( $# )); then main else printf '{ "version": 1 }\n[' while true; do main sleep 5 done fi