tmux2k/scripts/weather.sh

82 lines
2 KiB
Bash
Raw Normal View History

2022-08-09 06:28:34 +05:30
#!/usr/bin/env bash
2024-03-17 02:39:45 +05:30
2022-08-09 06:28:34 +05:30
export LC_ALL=en_US.UTF-8
2024-03-17 02:39:45 +05:30
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$current_dir"/utils.sh
show_fahrenheit=$(get_tmux_option "@tmux2k-show-fahrenheit" false)
show_location=$(get_tmux_option "@tmux2k-show-location" false)
fixed_location=$(get_tmux_option "@tmux2k-fixed-location" "Rampurhat")
declare -A weather_icons=(
["Clear"]="󰖙"
["Cloud"]=""
["Drizzle"]="󰖗"
["Fog"]=""
["Haze"]="󰼰"
["Mist"]=""
["Overcast"]=""
["Rain"]=""
["Sand"]=""
["Shower"]=""
["Smoke"]=""
["Snow"]=""
["Sunny"]="󰖙"
["Thunderstorm"]=""
["Tornado"]="󰼸"
["Windy"]="󰖝"
)
2022-08-09 06:28:34 +05:30
2023-03-29 14:52:27 +05:30
display_location() {
2024-03-17 02:39:45 +05:30
if [[ -n "$fixed_location" ]]; then
echo "$fixed_location"
elif $show_location; then
2023-03-29 14:52:27 +05:30
city=$(curl -s https://ipinfo.io/city 2>/dev/null)
region=$(curl -s https://ipinfo.io/region 2>/dev/null)
2024-03-17 02:39:45 +05:30
echo "$city, $region"
2023-03-29 14:52:27 +05:30
else
echo ''
fi
2022-08-09 06:28:34 +05:30
}
2023-03-29 14:52:27 +05:30
fetch_weather_information() {
2024-03-17 02:39:45 +05:30
if $show_fahrenheit; then
display_weather='&u'
2023-03-29 14:52:27 +05:30
else
2024-03-17 02:39:45 +05:30
display_weather='&m'
2023-03-29 14:52:27 +05:30
fi
2024-03-17 02:39:45 +05:30
curl -sL "wttr.in/$fixed_location?format=%C+%t$display_weather"
2022-08-09 06:28:34 +05:30
}
2023-03-29 14:52:27 +05:30
forecast_unicode() {
2024-03-17 02:39:45 +05:30
local condition=$1
weather_icon="${weather_icons[$condition]}"
if [[ -n $weather_icon ]]; then
echo "$weather_icon "
2023-03-29 14:52:27 +05:30
else
2024-03-17 02:39:45 +05:30
echo ' '
2023-03-29 14:52:27 +05:30
fi
2022-08-09 06:28:34 +05:30
}
2024-03-17 02:39:45 +05:30
display_weather() {
weather_information=$(fetch_weather_information "$display_weather")
condition=$(echo "$weather_information" | rev | cut -d ' ' -f2- | tr -d '[:space:]' | rev)
temperature=$(echo "$weather_information" | rev | cut -d ' ' -f 1 | rev)
unicode=$(forecast_unicode "$condition")
echo "$unicode${temperature/+/} $condition"
}
2023-03-29 14:52:27 +05:30
main() {
# process should be cancelled when session is killed
if ping -q -c 1 -W 1 ipinfo.io &>/dev/null; then
2024-03-17 02:39:45 +05:30
echo "$(display_weather) $(display_location)"
2023-03-29 14:52:27 +05:30
else
echo "Location Unavailable"
fi
2022-08-09 06:28:34 +05:30
}
main