added more advance weather capabilities
This commit is contained in:
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
# Open the hourly weather forecast (wttr.in v2 view) in a ghostty window;
|
||||
# focus it if it is already open. Intended as the on-click action for the
|
||||
# waybar weather module.
|
||||
set -uo pipefail
|
||||
|
||||
if hyprctl clients -j 2>/dev/null | jq -e 'any(.[]; ((.title // "") + " " + (.class // "")) | ascii_downcase | contains("wttr"))' >/dev/null 2>&1; then
|
||||
hyprctl dispatch 'hl.dsp.focus({ window = "title:wttr" })' 2>/dev/null || true
|
||||
exit 0
|
||||
fi
|
||||
|
||||
CITY="${WEATHER_CITY:-Denver}"
|
||||
ghostty --title=wttr -e bash -c 'curl -fsS --max-time 15 "https://v2.wttr.in/'"${CITY}"'"; printf "\n"; read -r -p "Press Enter to close"' &
|
||||
disown
|
||||
@@ -1,17 +1,19 @@
|
||||
#!/usr/bin/env bash
|
||||
# Print the current weather as a short icon+temp string, e.g. "🌦️ +83°F".
|
||||
# Caches the wttr.in response so the lock screen and waybar don't hit the
|
||||
# network on every refresh. A cached value is served immediately -- even when
|
||||
# Print the current weather as a short icon+temp string, e.g. "🌦️ +83°F",
|
||||
# plus -- in --json mode (for waybar) a multi-line "current conditions"
|
||||
# tooltip. A single cached wttr.in response is served immediately -- even when
|
||||
# stale -- and refreshed in the background, so a slow network never delays the
|
||||
# lock screen's first frame. Override the location with WEATHER_CITY or edit
|
||||
# CITY below.
|
||||
# lock screen's first frame or waybar's tooltip. Override the location with
|
||||
# WEATHER_CITY or edit CITY below.
|
||||
set -u
|
||||
|
||||
CITY="${WEATHER_CITY:-Denver}"
|
||||
CACHE_DIR="${XDG_RUNTIME_DIR:-/tmp}"
|
||||
CACHE="${CACHE_DIR}/wttr.cache"
|
||||
CACHE_MAX_AGE=1800 # 30 minutes
|
||||
URL="https://wttr.in/${CITY}?format=%c+%t"
|
||||
# First line is the waybar text; the following lines are the hover tooltip.
|
||||
FORMAT='%c+%t%5Cn%5Cn%C%5CnTemperature:+%t+%28feels+like+%f%29%5CnHumidity:+%h%5CnWind:+%w%5CnPrecipitation:+%p%5CnPressure:+%P%5CnUV+index:+%u'
|
||||
URL="https://wttr.in/${CITY}?format=${FORMAT}"
|
||||
|
||||
fetch() {
|
||||
TMP="${CACHE}.tmp.$$"
|
||||
@@ -24,25 +26,30 @@ fetch() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Fresh cache: print it.
|
||||
# Fresh cache: use it as-is.
|
||||
if [ -f "${CACHE}" ] && [ "$(( $(date +%s) - $(stat -c %Y "${CACHE}") ))" -lt "${CACHE_MAX_AGE}" ]; then
|
||||
cat "${CACHE}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Stale cache: print it immediately, then refresh in the background so the
|
||||
BODY="$(cat "${CACHE}")"
|
||||
# Stale cache: use it immediately, then refresh in the background so the
|
||||
# caller never blocks. flock keeps a pile-up of refreshes from several clients
|
||||
# (lock screen, waybar) from running concurrent curls.
|
||||
if [ -f "${CACHE}" ]; then
|
||||
cat "${CACHE}"
|
||||
elif [ -f "${CACHE}" ]; then
|
||||
BODY="$(cat "${CACHE}")"
|
||||
(
|
||||
exec 9>"${CACHE}.lock"
|
||||
flock -n 9 || exit 0
|
||||
fetch >/dev/null 2>&1
|
||||
) &
|
||||
disown
|
||||
# No cache at all: fetch synchronously, but with tight timeouts.
|
||||
else
|
||||
BODY="$(fetch)"
|
||||
fi
|
||||
|
||||
if [ "${1:-text}" = "--json" ]; then
|
||||
TEXT="$(printf '%s' "${BODY}" | head -n 1)"
|
||||
TOOLTIP="$(printf '%s' "${BODY}" | tail -n +2)"
|
||||
jq -nc --arg text "${TEXT}" --arg tip "${TOOLTIP}" '{text: $text, tooltip: $tip}'
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# No cache at all: fetch synchronously, but with tight timeouts.
|
||||
fetch
|
||||
printf '%s' "${BODY}" | head -n 1
|
||||
|
||||
@@ -56,5 +56,5 @@
|
||||
}
|
||||
],
|
||||
"workspace_rules": [],
|
||||
"last_applied_time": 1789241254.0499482
|
||||
"last_applied_time": 1789279298.6281931
|
||||
}
|
||||
|
||||
@@ -30,9 +30,11 @@
|
||||
},
|
||||
"custom/weather": {
|
||||
"format": "{}",
|
||||
"exec": "~/.local/bin/weather.sh",
|
||||
"exec": "~/.local/bin/weather.sh --json",
|
||||
"return-type": "json",
|
||||
"interval": 600,
|
||||
"tooltip": false
|
||||
"tooltip": true,
|
||||
"on-click": "~/.local/bin/weather-forecast.sh"
|
||||
},
|
||||
"tray": {
|
||||
"icon-size": 16,
|
||||
|
||||
Reference in New Issue
Block a user