dotfiles/bin/time.sh (view raw)
Oleksandr Smirnov
Oleksandr Smirnov
olexsmir@gmail.com setup time.sh script for time tracking, once again, shoutout to Sylvan Franklin, 1 month ago
olexsmir@gmail.com setup time.sh script for time tracking, once again, shoutout to Sylvan Franklin, 1 month ago
| 1 | #!/usr/bin/env bash |
| 2 | # hledger -f "$HOME/org/time.timeclock" balance --daily -p 'this week' |
| 3 | # |
| 4 | # sudo groupadd hostblock |
| 5 | # sudo usermod -aG hostblock olex |
| 6 | # sudo chown root:hostblock /etc/hosts |
| 7 | # sudo chmod 664 /etc/hosts |
| 8 | |
| 9 | readonly TC="$HOME/org/time.timeclock" |
| 10 | readonly CATS=$'stop\nWORK\nVIM\nWASTE' |
| 11 | readonly BLOCKED=("news.ycombinator.com" "www.lobste.rs" "www.twitch.tv" "www.reddit.com" "www.chess.com" "www.lichess.org") # "www.youtube.com" "youtube.com" |
| 12 | |
| 13 | now() { date '+%Y-%m-%d %H:%M:%S'; } |
| 14 | current() { awk '/^[io]/ { last_type=$1; last_cat=$4 } END { if (last_type == "i") print last_cat }' "$TC"; } |
| 15 | stop_tracking() { [[ -n "$(current)" ]] && printf 'o %s\n' "$(now)" >>"$TC"; } |
| 16 | |
| 17 | block_sites() { |
| 18 | for domain in "${BLOCKED[@]}"; do |
| 19 | hostess "$1" "$domain" "::1" >/dev/null 2>&1 |
| 20 | done |
| 21 | } |
| 22 | |
| 23 | update_tmux() { |
| 24 | [[ -z "${TMUX:-}" ]] && return |
| 25 | local curr=$(tmux show -gqv status-right 2>/dev/null || true) |
| 26 | local clean=${curr#"#(/home/olex/bin/time.sh status)" } |
| 27 | if [[ -n "${1:-}" ]] |
| 28 | then tmux set -gq status-right "#($0 status)${clean:+ $clean}" |
| 29 | else tmux set -gq status-right "$clean" |
| 30 | fi |
| 31 | } |
| 32 | |
| 33 | case "${1:-}" in |
| 34 | status) |
| 35 | open=$(current) |
| 36 | [[ -n "$open" ]] && printf '%s\n' "${open#t:}" |
| 37 | ;; |
| 38 | ""|s) |
| 39 | cat=$(printf '%s\n' "$CATS" | fzf) || exit |
| 40 | [[ -z "$cat" || "$cat" == stop ]] && { stop_tracking; block_sites rm; update_tmux ""; exit; } |
| 41 | |
| 42 | stop_tracking |
| 43 | printf 'i %s t:%s\n' "$(now)" "$cat" >> "$TC" |
| 44 | [[ "$cat" == WASTE ]] && block_sites rm || block_sites add |
| 45 | update_tmux "$cat" |
| 46 | ;; |
| 47 | esac |