dotfiles/bin/time.sh (view raw)
| 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" "WORK" "WASTE") |
| 11 | readonly BLOCKED=("news.ycombinator.com" "lobste.rs" "www.reddit.com" "www.chess.com" "www.lichess.org" |
| 12 | "discord.com" "www.x.com" "www.bsky.app" "www.twitch.tv" "www.youtube.com" "youtube.com") |
| 13 | |
| 14 | now() { date '+%Y-%m-%d %H:%M:%S'; } |
| 15 | current() { awk '/^[io]/ { last_type=$1; last_cat=$4 } END { if (last_type == "i") print last_cat }' "$TC"; } |
| 16 | stop_tracking() { [[ -n "$(current)" ]] && printf 'o %s\n' "$(now)" >>"$TC"; } |
| 17 | |
| 18 | block_sites() { |
| 19 | for domain in "${BLOCKED[@]}"; do |
| 20 | hostess "$1" "$domain" "::1" >/dev/null 2>&1 |
| 21 | done |
| 22 | } |
| 23 | |
| 24 | update_tmux() { |
| 25 | [[ -z "${TMUX:-}" ]] && return |
| 26 | local curr=$(tmux show -gqv status-right 2>/dev/null || true) |
| 27 | local clean=${curr#"#(/home/olex/bin/time.sh status)" } |
| 28 | if [[ -n "${1:-}" ]] |
| 29 | then tmux set -gq status-right "#($0 status)${clean:+ $clean}" |
| 30 | else tmux set -gq status-right "$clean" |
| 31 | fi |
| 32 | } |
| 33 | |
| 34 | case "${1:-}" in |
| 35 | status) |
| 36 | open=$(current) |
| 37 | [[ -n "$open" ]] && printf '%s\n' "${open#t:}" |
| 38 | ;; |
| 39 | "") |
| 40 | cat=$(printf '%s\n' "${CATS[@]}" | fzf --reverse --no-info) || exit |
| 41 | stop_tracking |
| 42 | |
| 43 | [[ -z "$cat" || "$cat" == stop ]] && { block_sites rm; update_tmux ""; exit; } |
| 44 | |
| 45 | printf 'i %s t:%s\n' "$(now)" "$cat" >> "$TC" |
| 46 | [[ "$cat" == WASTE ]] && block_sites rm || block_sites add |
| 47 | update_tmux "$cat" |
| 48 | ;; |
| 49 | esac |