aboutsummaryrefslogtreecommitdiff
path: root/users/ryan/modules/sketchybar/bar-config/plugins/weather.sh
blob: abf8103a105655e6ddb8e0699c844d2fead619f3 (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
#!/usr/bin/env bash
# custom/weather -> condition icon + temp, with a hover popup ("tooltip").
# Uses wttr.in JSON (format=j2, the smaller no-hourly variant). Needs jq + curl.
source "$CONFIG_DIR/colors.sh"
source "$CONFIG_DIR/icons.sh"

CACHE="/tmp/sketchybar_weather.txt"

# --- hover: show/hide the popup, populated from the cached line ---
case "$SENDER" in
  mouse.entered)
    [ -f "$CACHE" ] && sketchybar --set weather.details label="$(cat "$CACHE")"
    sketchybar --set "$NAME" popup.drawing=on
    exit 0 ;;
  mouse.exited)
    sketchybar --set "$NAME" popup.drawing=off
    exit 0 ;;
esac

command -v jq >/dev/null 2>&1 || { sketchybar --set "$NAME" label="wttr?"; exit 0; }

JSON="$(curl -sf --max-time 6 'wttr.in/?format=j2')" \
  || JSON="$(curl -sf --max-time 6 'wttr.in/?format=j1')" \
  || { sketchybar --set "$NAME" label="--"; exit 0; }

cc() { printf '%s' "$JSON" | jq -r ".current_condition[0].$1 // empty"; }
CODE="$(cc weatherCode)"; TEMP="$(cc temp_F)"; FEELS="$(cc FeelsLikeF)"
HUM="$(cc humidity)"; WSPD="$(cc windspeedMiles)"; WDIR="$(cc winddir16Point)"
DESC="$(printf '%s' "$JSON" | jq -r '.current_condition[0].weatherDesc[0].value // empty')"
AREA="$(printf '%s' "$JSON" | jq -r '.nearest_area[0].areaName[0].value // empty')"

# daytime? simple local-hour heuristic (06:00–18:59) for clear/partly-cloudy variants
HOUR=$(date +%H); HOUR=${HOUR#0}; : "${HOUR:=0}"
if [ "$HOUR" -ge 6 ] && [ "$HOUR" -lt 19 ]; then DAY=1; else DAY=0; fi

# WWO weatherCode -> nf-weather glyph
case "$CODE" in
  113) [ "$DAY" = 1 ] && ICON="$WEA_SUNNY" || ICON="$WEA_NIGHT" ;;
  116) [ "$DAY" = 1 ] && ICON="$WEA_DAY_CLOUDY" || ICON="$WEA_NIGHT_CLOUDY" ;;
  119|122)                                             ICON="$WEA_CLOUDY" ;;
  143|248|260)                                         ICON="$WEA_FOG" ;;
  200|386|389|392|395)                                 ICON="$WEA_THUNDER" ;;
  176|263|266|293|296|353)                             ICON="$WEA_SHOWERS" ;;
  299|302|305|308|356|359)                             ICON="$WEA_RAIN" ;;
  179|227|230|323|326|329|332|335|338|368|371)         ICON="$WEA_SNOW" ;;
  182|185|281|284|311|314|317|320|350|362|365|374|377) ICON="$WEA_SLEET" ;;
  *)                                                   ICON="$WEA_CLOUDY" ;;
esac

[ -z "$TEMP" ] && exit 0

# bar: <icon> <temp>°F   (drop " ${TEMP}°F" if you want the icon only)
sketchybar --set "$NAME" label="$ICON ${TEMP}°F"

# cache the single-line hover tooltip
printf '%s' "${AREA:+$AREA · }${DESC} · feels ${FEELS}°F · ${HUM}% humidity · ${WDIR} ${WSPD}mph" > "$CACHE"