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" "SEMI-WASTE" "WASTE") |
| 11 | readonly BLOCKED=("news.ycombinator.com" "lobste.rs" "www.reddit.com" "www.chess.com" "www.lichess.org" |
| 12 | "discord.com" "www.x.com" "bsky.app" "www.youtube.com" "youtube.com" "www.twitch.tv") |
| 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 | grep -qF "::1 $domain" /etc/hosts 2>/dev/null || printf '%s\t%s\n' '::1' "$domain" >>/etc/hosts |
| 21 | done |
| 22 | } |
| 23 | |
| 24 | unblock_sites() { |
| 25 | local pattern |
| 26 | for domain in "${BLOCKED[@]}"; do |
| 27 | pattern+="/^::1[[:space:]]\+$domain\([[:space:]]\|$\)/d;" |
| 28 | done |
| 29 | sed "$pattern" /etc/hosts >/tmp/hosts.tmp && cat /tmp/hosts.tmp >/etc/hosts |
| 30 | } |
| 31 | |
| 32 | update_tmux() { |
| 33 | [[ -z "${TMUX:-}" ]] && return |
| 34 | local curr=$(tmux show -gqv status-right 2>/dev/null || true) |
| 35 | local clean=${curr#"#(/home/olex/bin/time.sh status)" } |
| 36 | if [[ -n "${1:-}" ]] |
| 37 | then tmux set -gq status-right "#($0 status)${clean:+ $clean}" |
| 38 | else tmux set -gq status-right "$clean" |
| 39 | fi |
| 40 | } |
| 41 | |
| 42 | case "${1:-}" in |
| 43 | status) |
| 44 | open=$(current) |
| 45 | [[ -n "$open" ]] && printf '%s\n' "${open#t:}" |
| 46 | ;; |
| 47 | "") |
| 48 | cat=$(printf '%s\n' "${CATS[@]}" | fzf --reverse --no-info) || exit |
| 49 | stop_tracking |
| 50 | |
| 51 | [[ -z "$cat" || "$cat" == stop ]] && { unblock_sites; update_tmux ""; exit; } |
| 52 | |
| 53 | printf 'i %s t:%s\n' "$(now)" "$cat" >> "$TC" |
| 54 | [[ "$cat" == WASTE ]] && unblock_sites || block_sites |
| 55 | update_tmux "$cat" |
| 56 | ;; |
| 57 | esac |