summaryrefslogtreecommitdiff
path: root/ar/.local/bin/statusbar/sb-clock
blob: 578a03c28e32b2fd4d89f202a9d1730ec5bfea26 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/sh

# Get current hour and minute
calendar="🗓"
hour=$(date '+%I')
minute=$(date '+%M')

# Cache cron daemon check for 5 minutes
crontab_cache="${XDG_CACHE_HOME:-${HOME}/.cache}/crontab/cronjobs"
if [ ! -f "$crontab_cache" ] || [ $(($(date +%s) - $(stat -c %Y "$crontab_cache" 2>/dev/null || echo 0))) -gt 300 ]; then
  mkdir -p "${XDG_CACHE_HOME:-${HOME}/.cache}/crontab"
  if crontab -l 2>/dev/null | grep -qv '^#\|^$'; then
    echo "1" >"$crontab_cache"
  else
    echo "0" >"$crontab_cache"
  fi
fi

# Determine the icon based on hour and minute
if [ "$(cat "$crontab_cache" 2>/dev/null)" = "1" ]; then
  if [ "$minute" -ge 30 ]; then
    case "$hour" in
    "00" | "12") icon="🕧" ;; # 12:30
    "01" | "13") icon="🕜" ;; # 1:30
    "02" | "14") icon="🕝" ;; # 2:30
    "03" | "15") icon="🕞" ;; # 3:30
    "04" | "16") icon="🕟" ;; # 4:30
    "05" | "17") icon="🕠" ;; # 5:30
    "06" | "18") icon="🕡" ;; # 6:30
    "07" | "19") icon="🕢" ;; # 7:30
    "08" | "20") icon="🕣" ;; # 8:30
    "09" | "21") icon="🕤" ;; # 9:30
    "10" | "22") icon="🕥" ;; # 10:30
    "11" | "23") icon="🕦" ;; # 11:30
    esac
  else
    case "$hour" in
    "00" | "12") icon="🕛" ;; # 12:00
    "01" | "13") icon="🕐" ;; # 1:00
    "02" | "14") icon="🕑" ;; # 2:00
    "03" | "15") icon="🕒" ;; # 3:00
    "04" | "16") icon="🕓" ;; # 4:00
    "05" | "17") icon="🕔" ;; # 5:00
    "06" | "18") icon="🕕" ;; # 6:00
    "07" | "19") icon="🕖" ;; # 7:00
    "08" | "20") icon="🕗" ;; # 8:00
    "09" | "21") icon="🕘" ;; # 9:00
    "10" | "22") icon="🕙" ;; # 10:00
    "11" | "23") icon="🕚" ;; # 11:00
    esac
  fi
else
  icon="⏰"
fi

# Shows the current moon phase.
locationfile="${XDG_CACHE_HOME:-${HOME}/.cache}/statusbar/location"
moonfile="${XDG_DATA_HOME:-${HOME}/.local/share}/wallpapers/moonphase"

# Cache location for 24 hours - stores lat,lon
valid_latlon() { echo "$1" | grep -qE '^-?[0-9]+\.?[0-9]*,-?[0-9]+\.?[0-9]*$'; }
if ! ([ -s "$locationfile" ] && valid_latlon "$(cat "$locationfile")" && [ $(($(date +%s) - $(stat -c %Y "$locationfile" 2>/dev/null || echo 0))) -le 86400 ]); then
  mkdir -p "${XDG_CACHE_HOME:-${HOME}/.cache}/statusbar"
  loc=$(curl -s --max-time 2 http://ip-api.com/json 2>/dev/null | jq -r 'select(.status == "success") | [.lat, .lon] | join(",")' 2>/dev/null)
  valid_latlon "$loc" && echo "$loc" >"$locationfile" &
fi

location=$(cat "$locationfile" 2>/dev/null || echo "")

# Update moon phase only once per day
if [ ! -s "$moonfile" ] || [ "$(stat -c %y "$moonfile" 2>/dev/null | cut -d' ' -f1)" != "$(date '+%Y-%m-%d')" ]; then
  if [ -n "$location" ]; then
    lat=${location%%,*} lon=${location##*,}
    moon=$(curl -sf --max-time 2 "wttr.in/${lat},${lon}?format=%m" 2>/dev/null)
    [ -n "$moon" ] && ! echo "$moon" | grep -q 'Unknown location' && echo "$moon" >"$moonfile" &
  fi
fi

moonicon="$(cat "$moonfile" 2>/dev/null)"

case $BLOCK_BUTTON in
1)
  hcache="${XDG_CACHE_HOME:-${HOME}/.cache}/statusbar/holidays-$(date +%Y)"
  if [ ! -s "$hcache" ]; then
    mkdir -p "${XDG_CACHE_HOME:-${HOME}/.cache}/statusbar"
    python -c "import holidays; y=$(date +%Y); p=holidays.KR(years=y,categories=('public',)); [print('public' if d in p else 'bank', d.strftime('%m-%d')) for d in sorted(holidays.KR(years=y,categories=('public','bank')))]" 2>/dev/null >"$hcache"
  fi
  mm=$(date +%m)
  hpub=$(awk -v m="$mm" '$1=="public" && substr($2,1,2)==m {split($2,a,"-"); printf "%s,", a[2]+0}' "$hcache" 2>/dev/null | sed 's/,$//')
  hbnk=$(awk -v m="$mm" '$1=="bank" && substr($2,1,2)==m {split($2,a,"-"); printf "%s,", a[2]+0}' "$hcache" 2>/dev/null | sed 's/,$//')
  notify-send "This Month" "$(cal | awk -v t="$(date +%-d)" -v m="$(date +%B)" -v y="$(date +%Y)" -v p="$hpub" -v b="$hbnk" 'BEGIN{cnt=split(p,ap,",");for(k=1;k<=cnt;k++)hpub[ap[k]+0]=1;cnt=split(b,ab,",");for(k=1;k<=cnt;k++)hbnk[ab[k]+0]=1}NR<=2{gsub(m,"<b><span color=\"blue\">"m"</span></b>");gsub(y,"<b><span color=\"blue\">"y"</span></b>");print;next}{out="";for(i=0;i<7;i++){pos=i*3+1;raw=substr($0,pos,2);n=raw+0;if(length(raw)<2)raw=raw"  ";raw=substr(raw,1,2);sep=(i<6)?" ":"";if(n>0&&n==t){out=out"<b><span color=\"#5fafff\">"raw"</span></b>"sep}else if(n>0&&(n in hpub)){out=out"<span color=\"red\">"raw"</span>"sep}else if(n>0&&(n in hbnk)){out=out"<span color=\"orange\">"raw"</span>"sep}else if(n>0&&(i==0||i==6)){out=out"<span color=\"red\">"raw"</span>"sep}else{out=out raw sep}}print out}')" && notify-send "Appointments" "$(calcurse -d3)"
  ;;
2) setsid -f "$TERMINAL" -e calcurse ;;
3)
  notify-send "📅 Time/date module" "\- Left click to show upcoming appointments for the next three days via \`calcurse -d3\` and show the month via \`cal\`
- Left click also displays the current time in other cities.
- Middle click opens calcurse if installed"
  notify-send "🌜 Moon phase module" "Displays current moon phase
- 🌑: New
- 🌒: Waxing Crescent
- 🌓: First Quarter
- 🌔: Waxing Gibbous
- 🌕: Full
- 🌖: Waning Gibbous
- 🌗: Last Quarter
- 🌘: Waning Crescent"
  ;;
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
7) timezones ;;
esac

# Output the formatted date and time
date "+${moonicon:-$calendar}%a,%d $icon%H:%M"