From 304dd700c9db59aa1feec51d7913ec4132fc1b4f Mon Sep 17 00:00:00 2001 From: Hugo Licon Date: Mon, 13 Jul 2020 22:02:47 -0600 Subject: [PATCH] Show unicode characters for forecast, remove plus Show unicode characters for forecast instead of an emoji. Remove the plus sign for positive degrees. Show city, region instead of city, country --- scripts/weather.sh | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/scripts/weather.sh b/scripts/weather.sh index 58b03e2..95274fb 100755 --- a/scripts/weather.sh +++ b/scripts/weather.sh @@ -3,11 +3,18 @@ fahrenheit=$1 -weather_information() +load_request_params() +{ + city=$(curl -s https://ipinfo.io/city 2> /dev/null) + region=$(curl -s https://ipinfo.io/region 2> /dev/null) +} + + +fetch_weather_information() { display_weather=$1 - # it gets the weather condition (%c), the temperature (%t), and the location (%l) - curl -sL curl wttr.in\?format="+%c+%t+%l$display_weather" + # it gets the weather condition textual name (%C), the temperature (%t), and the location (%l) + curl -sL curl wttr.in\?format="+%C+%t$display_weather" } #get weather display @@ -18,14 +25,38 @@ display_weather() else display_weather='&m' # for metric system fi - echo $(weather_information $display_weather) + weather_information=$(fetch_weather_information $display_weather) + + weather_condition=$(echo $weather_information | awk '{print $1;}') # Sunny, Snow, etc + temperature=$(echo $weather_information | awk '{print $2;}') # +31°C, -3°F, etc + unicode=$(forecast_unicode $weather_condition) + + echo "$unicode ${temperature/+/}" # remove the plus sign to the temperature +} + +forecast_unicode() +{ + weather_condition=$1 + + 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 '' + else + echo '☀ ' + fi } main() { + load_request_params # process should be cancelled when session is killed if ping -q -c 1 -W 1 ipinfo.io &>/dev/null; then - echo "$(display_weather)" + echo "$(display_weather) $city, $region" else echo "Location Unavailable" fi