diff --git a/scripts/sleep_weather.sh b/scripts/sleep_weather.sh deleted file mode 100755 index ac1f6ea..0000000 --- a/scripts/sleep_weather.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env bash -# setting the locale, some users have issues with different locales, this forces the correct one -export LC_ALL=en_US.UTF-8 - -#wrapper script for running weather on interval - -fahrenheit=$1 -location=$2 -fixedlocation=$3 - -LOCKFILE=/tmp/.tmux2k-tmux-weather.lock -DATAFILE=/tmp/.tmux2k-tmux-data - -ensure_single_process() { - # check for another running instance of this script and terminate it if found - [ -f $LOCKFILE ] && ps -p "$(cat $LOCKFILE)" -o cmd= | grep -F " ${BASH_SOURCE[0]}" && kill "$(cat $LOCKFILE)" - echo $$ >$LOCKFILE -} - -main() { - ensure_single_process - - current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - - if [ ! -f $DATAFILE ]; then - printf "Loading..." >$DATAFILE - fi - - "$current_dir"/weather.sh >$DATAFILE - - while tmux has-session &>/dev/null; do - "$current_dir"/weather.sh "$fahrenheit" "$location" "$fixedlocation" >$DATAFILE - if tmux has-session &>/dev/null; then - sleep 1200 - else - break - fi - done - - rm $LOCKFILE -} - -#run main driver function -main diff --git a/scripts/weather.sh b/scripts/weather.sh index 1976f85..4b7f26c 100755 --- a/scripts/weather.sh +++ b/scripts/weather.sh @@ -1,69 +1,81 @@ #!/usr/bin/env bash -# setting the locale, some users have issues with different locales, this forces the correct one + export LC_ALL=en_US.UTF-8 -fahrenheit=$1 -location=$2 -fixedlocation=$3 +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"]="󰖝" +) display_location() { - if $location && [[ -n "$fixedlocation" ]]; then - echo " $fixedlocation" - elif $location; then + if [[ -n "$fixed_location" ]]; then + echo "$fixed_location" + elif $show_location; then city=$(curl -s https://ipinfo.io/city 2>/dev/null) region=$(curl -s https://ipinfo.io/region 2>/dev/null) - echo " $city, $region" + echo "$city, $region" else echo '' fi } fetch_weather_information() { - display_weather=$1 - # it gets the weather condition textual name (%C), and the temperature (%t) - curl -sL wttr.in/"$fixedlocation"\?format="%C+%t$display_weather" -} - -#get weather display -display_weather() { - if $fahrenheit; then - display_weather='&u' # for USA system + if $show_fahrenheit; then + display_weather='&u' else - display_weather='&m' # for metric system + display_weather='&m' fi - weather_information=$(fetch_weather_information $display_weather) - - weather_condition=$(echo "$weather_information" | rev | cut -d ' ' -f2- | rev) # Sunny, Snow, etc - temperature=$(echo "$weather_information" | rev | cut -d ' ' -f 1 | rev) # +31°C, -3°F, etc - unicode=$(forecast_unicode "$weather_condition") - - echo "$unicode${temperature/+/}" # remove the plus sign to the temperature + curl -sL "wttr.in/$fixed_location?format=%C+%t$display_weather" } forecast_unicode() { - weather_condition=$(echo "$weather_condition" | awk '{print tolower($0)}') - - if [[ $weather_condition =~ 'snow' ]]; then - echo '❄ ' - elif [[ (($weather_condition =~ 'rain') || ($weather_condition =~ 'shower')) ]]; then - echo '☂ ' - elif [[ (($weather_condition =~ 'overcast') || ($weather_condition =~ 'cloud')) ]]; then - echo '☁ ' - elif [[ $weather_condition = 'NA' ]]; then - echo '' + local condition=$1 + weather_icon="${weather_icons[$condition]}" + if [[ -n $weather_icon ]]; then + echo "$weather_icon " else - echo '☀ ' + echo ' ' fi } +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" +} + main() { # process should be cancelled when session is killed if ping -q -c 1 -W 1 ipinfo.io &>/dev/null; then - echo "$(display_weather)$(display_location)" + echo "$(display_weather) $(display_location)" else echo "Location Unavailable" fi } -#run main driver program main