#!/usr/bin/env bash datasource=$1 outputmode=$2 IFS=',' read -r -a outputargs <<< "$3" # IFS = Intenal field seperatior, special var IFS=',' read -r -a opt_outputargs <<< "$4" LOCALECASHE=$HOME/.cache/curl_weather declare -A DISCRIPTOR_MAP DISCRIPTOR_MAP["icon"]="" DISCRIPTOR_MAP["temp"]="" DISCRIPTOR_MAP["humidity"]=" " DISCRIPTOR_MAP["rain"]=" " DISCRIPTOR_MAP["wind"]=" " DISCRIPTOR_MAP["pressure"]="󱤊 " DISCRIPTOR_MAP["moon"]="" DISCRIPTOR_MAP["uv"]="UVI " SEPERATOR="\n" # Docs: https://github.com/chubin/wttr.in # # c Weather condition, # C Weather condition textual name, # x Weather condition, plain-text symbol, # h Humidity, # t Temperature (Actual), # f Temperature (Feels Like), # w Wind, # l Location, # m Moon phase 🌑🌒🌓🌔🌕🌖🌗🌘, # M Moon day, # p Precipitation (mm/3 hours), # P Pressure (hPa), # u UV index (1-12), # # D Dawn*, # S Sunrise*, # z Zenith*, # s Sunset*, # d Dusk*, # T Current time*, # Z Local timezone. # # (*times are shown in the local timezone) #c, t, h, p, w, m if [ "$datasource" = "local" ] && [ -f "$LOCALECASHE" ]; then data=($(cat $LOCALECASHE)) else data=$(curl -s 'wttr.in/Bremen?format="%c\n%t\n%h\n%p\n%w\n%u\n%P\n%m"') touch $LOCALECASHE echo $data > $LOCALECASHE data=($data) fi icon=$(echo ${data[0]} | tr -d "\"" ) temp=${data[1]} humidity=${data[2]} rain=${data[3]} wind=${data[4]} uv=${data[5]} pressure=${data[6]} moon=$(echo ${data[7]} | tr -d "\"") function get_output_value() { # !var return the value of the variable which name is stored in vat # Pointer to value # if var=test and test = Hello worrld: # ${var} = test BUT ${!var} =Hello World local out="" local array=("${!1}") for element in "${array[@]}"; do out+="${DISCRIPTOR_MAP[$element]}${!element}${SEPERATOR}" done echo "${out::-${#SEPERATOR}}" # Remove last char } function waybar_json() { local text_value=$(get_output_value outputargs[@]) local tooltip_value=$(get_output_value opt_outputargs[@]) echo "{\"text\": \"$text_value\", \"tooltip\": \"$tooltip_value\", \"class\": \"weather\" }" | jq --unbuffered --compact-output # \"alt\": \"$moon\r$humidity\", } if [ "$outputmode" == "waybar" ]; then echo $(waybar_json) else echo "Not implemented" fi