aboutsummaryrefslogtreecommitdiff
path: root/timer
blob: fa8883ccd23dbcc18b840e79cc2d415cb4bc842b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
# timer DURATION [MESSAGE...]
# examples: timer 25m "Pomodoro done" | timer 90s "Break over"

set -euo pipefail

dur="${1:-}"
shift || true
msg="${*:-Time is up}"

if [ -z "$dur" ]; then
  echo "usage: timer 25m \"Pomodoro done\"   |   timer 90s \"Break over\""
  exit 1
fi

# Parse duration: Ns or Nm (only)
case "$dur" in
  *m) total=$(( ${dur%m} * 60 ));;
  *s) total=$(( ${dur%s} ));;
  *)  echo "Bad duration: $dur (use e.g. 25m or 90s)"; exit 1;;
esac

# Terminal helpers
cols=$(tput cols 2>/dev/null || echo 80)
barw=$(( cols - 22 ))
[ "$barw" -lt 10 ] && barw=10

hide_cursor() { tput civis 2>/dev/null || true; }
show_cursor() { tput cnorm 2>/dev/null || true; }
cleanup() { show_cursor; printf "\n"; }
trap cleanup EXIT INT TERM

hide_cursor

# ANSI colors (no external deps)
YELLOW=$'\033[33m'
GREEN=$'\033[32m'
DIM=$'\033[2m'
RESET=$'\033[0m'

secs="$total"
start_msg="${msg}"

while [ "$secs" -gt 0 ]; do
  mm=$(( secs / 60 ))
  ss=$(( secs % 60 ))

  done=$(( total - secs ))
  pct=$(( done * 100 / total ))

  fill=$(( barw * done / total ))
  empty=$(( barw - fill ))

  # Build progress bar
  bar="$(printf "%${fill}s" "" | tr ' ' '=')"
  pad="$(printf "%${empty}s" "")"

  printf "\r${YELLOW}T-%02d:%02d${RESET} ${DIM}[${bar}>${pad}]${RESET} %3d%%  " \
    "$mm" "$ss" "$pct"

  sleep 1
  secs=$(( secs - 1 ))
done

printf "\r${GREEN}DONE 00:00 100%%\n"
notify-send "Timer" "$start_msg"
paplay /usr/share/sounds/freedesktop/stereo/complete.oga >/dev/null 2>&1 || true