users/ryan/modules/sketchybar/bar-config/plugins/weather.sh
0c8fa3a2a270747f3dbb54fe8792f3f0aec997e3
· 2.5 KB · 56 lines
raw
| 1 | #!/usr/bin/env bash |
| 2 | # custom/weather -> condition icon + temp, with a hover popup ("tooltip"). |
| 3 | # Uses wttr.in JSON (format=j2, the smaller no-hourly variant). Needs jq + curl. |
| 4 | source "$CONFIG_DIR/colors.sh" |
| 5 | source "$CONFIG_DIR/icons.sh" |
| 6 | |
| 7 | CACHE="/tmp/sketchybar_weather.txt" |
| 8 | |
| 9 | # --- hover: show/hide the popup, populated from the cached line --- |
| 10 | case "$SENDER" in |
| 11 | mouse.entered) |
| 12 | [ -f "$CACHE" ] && sketchybar --set weather.details label="$(cat "$CACHE")" |
| 13 | sketchybar --set "$NAME" popup.drawing=on |
| 14 | exit 0 ;; |
| 15 | mouse.exited) |
| 16 | sketchybar --set "$NAME" popup.drawing=off |
| 17 | exit 0 ;; |
| 18 | esac |
| 19 | |
| 20 | command -v jq >/dev/null 2>&1 || { sketchybar --set "$NAME" label="wttr?"; exit 0; } |
| 21 | |
| 22 | JSON="$(curl -sf --max-time 6 'wttr.in/?format=j2')" \ |
| 23 | || JSON="$(curl -sf --max-time 6 'wttr.in/?format=j1')" \ |
| 24 | || { sketchybar --set "$NAME" label="--"; exit 0; } |
| 25 | |
| 26 | cc() { printf '%s' "$JSON" | jq -r ".current_condition[0].$1 // empty"; } |
| 27 | CODE="$(cc weatherCode)"; TEMP="$(cc temp_F)"; FEELS="$(cc FeelsLikeF)" |
| 28 | HUM="$(cc humidity)"; WSPD="$(cc windspeedMiles)"; WDIR="$(cc winddir16Point)" |
| 29 | DESC="$(printf '%s' "$JSON" | jq -r '.current_condition[0].weatherDesc[0].value // empty')" |
| 30 | AREA="$(printf '%s' "$JSON" | jq -r '.nearest_area[0].areaName[0].value // empty')" |
| 31 | |
| 32 | # daytime? simple local-hour heuristic (06:00–18:59) for clear/partly-cloudy variants |
| 33 | HOUR=$(date +%H); HOUR=${HOUR#0}; : "${HOUR:=0}" |
| 34 | if [ "$HOUR" -ge 6 ] && [ "$HOUR" -lt 19 ]; then DAY=1; else DAY=0; fi |
| 35 | |
| 36 | # WWO weatherCode -> nf-weather glyph |
| 37 | case "$CODE" in |
| 38 | 113) [ "$DAY" = 1 ] && ICON="$WEA_SUNNY" || ICON="$WEA_NIGHT" ;; |
| 39 | 116) [ "$DAY" = 1 ] && ICON="$WEA_DAY_CLOUDY" || ICON="$WEA_NIGHT_CLOUDY" ;; |
| 40 | 119|122) ICON="$WEA_CLOUDY" ;; |
| 41 | 143|248|260) ICON="$WEA_FOG" ;; |
| 42 | 200|386|389|392|395) ICON="$WEA_THUNDER" ;; |
| 43 | 176|263|266|293|296|353) ICON="$WEA_SHOWERS" ;; |
| 44 | 299|302|305|308|356|359) ICON="$WEA_RAIN" ;; |
| 45 | 179|227|230|323|326|329|332|335|338|368|371) ICON="$WEA_SNOW" ;; |
| 46 | 182|185|281|284|311|314|317|320|350|362|365|374|377) ICON="$WEA_SLEET" ;; |
| 47 | *) ICON="$WEA_CLOUDY" ;; |
| 48 | esac |
| 49 | |
| 50 | [ -z "$TEMP" ] && exit 0 |
| 51 | |
| 52 | # bar: <icon> <temp>°F (drop " ${TEMP}°F" if you want the icon only) |
| 53 | sketchybar --set "$NAME" label="$ICON ${TEMP}°F" |
| 54 | |
| 55 | # cache the single-line hover tooltip |
| 56 | printf '%s' "${AREA:+$AREA · }${DESC} · feels ${FEELS}°F · ${HUM}% humidity · ${WDIR} ${WSPD}mph" > "$CACHE" |