added description for text based desc

This commit is contained in:
spinach 2023-02-01 18:39:40 -05:00
parent 2641d77390
commit b8e65a6961

51
weather
View File

@ -11,6 +11,7 @@ options:
-a, --apikey set the api key to use to KEY -a, --apikey set the api key to use to KEY
-l, --location print the location being used -l, --location print the location being used
-p, --pretty enables nerdfont symbols -p, --pretty enables nerdfont symbols
-d, --desc prints the conditions text as well
-z, --zipcode returns the weather for ZIPCODE -z, --zipcode returns the weather for ZIPCODE
-s, --search returns the weather for QUERY -s, --search returns the weather for QUERY
-u, --units sets the temperature units -u, --units sets the temperature units
@ -47,10 +48,16 @@ print_weather() {
# check to see if it is expired # check to see if it is expired
check_expiration check_expiration
# trimming state # checking if description enabled
if [[ "$DESCRIPTION" = true ]] ; then
DESC=$(echo "$CONDITIONS" | awk '{for(i=1;i<=NF;i++){ $i=toupper(substr($i,1,1)) substr($i,2) }}1')
DESC=$(printf '%s ' "$DESC")
fi
if [[ "$PRETTY" = true ]] ; then if [[ "$PRETTY" = true ]] ; then
# print with NF icons # print with NF icons
icon=$(get_icon "$WEATHER_ICON") icon=$(get_icon "$WEATHER_ICON")
humidity_icon=$(echo -e '\ue373')
# checking units # checking units
if [[ "$UNIT" == "C" ]] ; then if [[ "$UNIT" == "C" ]] ; then
@ -58,13 +65,19 @@ print_weather() {
else else
temp_icon=$(echo -e '\ufa04') temp_icon=$(echo -e '\ufa04')
fi fi
# printing
printf '%s %.1f%s %d\ue373 in %s\n' $icon $TEMPERATURE "$temp_icon" "$HUMIDITY" "$CITY"
else else
# default printing # default printing
printf '%.1f*%s %d%% RH in %s\n' $TEMPERATURE $UNIT $HUMIDITY "$CITY" humidity_icon="% RH"
if [[ "$UNIT" == "C" ]] ; then
temp_icon="*C"
else
temp_icon="*F"
fi
fi fi
# printing
printf '%s%s %.1f%s %d%s in %s\n' "$DESC" "$icon" $TEMPERATURE "$temp_icon" $HUMIDITY "$humidity_icon" "$CITY"
# save before exiting # save before exiting
save_info save_info
} }
@ -192,6 +205,7 @@ get_weather() {
WEATHER=$(curl --silent -fL "$WEATHER_URL") WEATHER=$(curl --silent -fL "$WEATHER_URL")
CONDITIONS=$(echo $WEATHER | gojq -r '.weather[0].description')
TEMPERATURE=$(echo $WEATHER | gojq -r '.main.temp') TEMPERATURE=$(echo $WEATHER | gojq -r '.main.temp')
HUMIDITY=$(echo $WEATHER | gojq -r '.main.humidity') HUMIDITY=$(echo $WEATHER | gojq -r '.main.humidity')
WEATHER_ICON=$(echo $WEATHER | gojq -r '.weather[0].icon') WEATHER_ICON=$(echo $WEATHER | gojq -r '.weather[0].icon')
@ -218,17 +232,18 @@ update_key() {
save_info() { save_info() {
# saves set env vars to file # saves set env vars to file
printf '%s\n' \ printf '%s="%s"\n' \
"API_KEY=$API_KEY" \ "API_KEY" "$API_KEY" \
"PARSER=$PARSER" \ "PARSER" "$PARSER" \
"LAT=$LAT" \ "LAT" "$LAT" \
"LON=$LON" \ "LON" "$LON" \
"CITY=$CITY" \ "CITY" "$CITY" \
"TEMPERATURE=$TEMPERATURE" \ "TEMPERATURE" "$TEMPERATURE" \
"HUMIDITY=$HUMIDITY" \ "HUMIDITY" "$HUMIDITY" \
"UNIT=$UNIT" \ "UNIT" "$UNIT" \
"WEATHER_ICON=$WEATHER_ICON" \ "WEATHER_ICON" "$WEATHER_ICON" \
"EXPIRATION=$EXPIRATION" > "$WEATHER_FILE" "CONDITIONS" "$CONDITIONS" \
"EXPIRATION" "$EXPIRATION" > "$WEATHER_FILE"
} }
load_info() { load_info() {
@ -335,6 +350,7 @@ for arg in "$@"; do
'--search') set -- "$@" "-s" ;; '--search') set -- "$@" "-s" ;;
'--units') set -- "$@" "-u" ;; '--units') set -- "$@" "-u" ;;
'--pretty') set -- "$@" "-p" ;; '--pretty') set -- "$@" "-p" ;;
'--pretty') set -- "$@" "-p" ;;
'--icon_test') set -- "$@" "-i" ;; '--icon_test') set -- "$@" "-i" ;;
'--help') set -- "$@" "-h" ;; '--help') set -- "$@" "-h" ;;
*) set -- "$@" "$arg" ;; *) set -- "$@" "$arg" ;;
@ -344,7 +360,7 @@ done
# loading the info # loading the info
load_info load_info
while getopts "chpliz:s:u:a:" opt; do while getopts "chpldiz:s:u:a:" opt; do
case "$opt" in case "$opt" in
'a' ) 'a' )
API_KEY="$OPTARG" API_KEY="$OPTARG"
@ -366,6 +382,9 @@ while getopts "chpliz:s:u:a:" opt; do
'p' ) 'p' )
PRETTY=true PRETTY=true
;; ;;
'd' )
DESCRIPTION=true
;;
'i' ) 'i' )
icon_test icon_test
exit 0 exit 0