Skip to content

Instantly share code, notes, and snippets.

@tomsmeding
Last active July 8, 2020 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomsmeding/e1da9e25aeae9382dc68 to your computer and use it in GitHub Desktop.
Save tomsmeding/e1da9e25aeae9382dc68 to your computer and use it in GitHub Desktop.
Buienalarm in de terminal. Compileer zelf buienalarm van https://github.com/lieuwex/buienalarm
#!/usr/bin/env bash
BARWID=3 # if not 3, make sure the hour ticks alignment matches up
BARHEI=3
BARTOP=24 # ensure this is divisible by $BARHEI
function print_spark() {
local y=$((BARHEI - 1 - $1))
local alphabet=" ▁▂▃▄▅▆▇█"
shift
for value; do
value=$(((value - y * BARTOP / BARHEI) * 8 / (BARTOP / BARHEI)))
[[ $value -lt 0 ]] && value=0
[[ $value -gt 8 ]] && value=8
for (( i=0; i<"$BARWID"; i+=1 )); do
echo -n "${alphabet:$value:1}"
done
done
}
function repspace() {
printf "%${1}s"
}
# Parse flags
fullday=0
location="Den Haag"
while test "$#" -gt 0; do
if test "$1" = "-24"
then fullday=1
else location="$1"
fi
shift
done
# Get data
if test "$fullday" -eq 1
then alarm="$(buienalarm -24 "$location")"
else alarm="$(buienalarm "$location")"
fi
# Parse data
hours=( $(cut -c 1-5 <<<"$alarm") )
amounts=( $(sed 's/.* \([^m]*\).*/\1/' <<<"$alarm") )
numhours="${#hours[@]}"
if test "$numhours" -lt 2; then
echo >&2 "No data received from 'buienalarm'"
exit 1
fi
# Determine ticks and tick spacing
numticks=$(( (numhours + 3) / 6 + 1 ))
ticks=()
tickspaces=()
[[ $numticks -le 1 ]] && divisor=1 || divisor=$((numticks-1))
for (( i=0; i<"$numticks"; i+=1 )); do
index=$(( (numhours-1) * i / $divisor ))
[[ $i -eq 0 ]] && previndex=0 || previndex=$(( (numhours-1) * (i-1) / $divisor ))
ticks[${#ticks[@]}]="${hours[$index]}"
nsp=$((index - previndex - 1))
[[ $nsp -lt 0 ]] && nsp=0
tickspaces[${#tickspaces[@]}]="$nsp"
done
# Build spark graph data
maxval=0
tospark=()
for num in "${amounts[@]}"; do
[[ $num =~ ([^.]*)\.(.).* ]] && intval="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
if [[ "${intval:0:1}" = "0" ]]; then intval="${intval:1}"; fi
tospark[${#tospark[@]}]="$intval"
[[ $intval -gt $maxval ]] && maxval="$intval"
done
# Print the lines of the graph
for (( y=0; y<"$BARHEI"; y+=1 )); do
[[ $y -ne 0 ]] && echo
echo -n '│'
print_spark $y "${tospark[@]}"
echo -n '│'
done
# Print maximum tag after lowest line of spark graph
echo " (max $(bc <<<"scale=1; $maxval / 10" | sed 's/^\./0./')mm/h)"
# Print tick lines
echo -n ' '
for (( i=0; i<"$numticks"; i+=1 )); do
repspace "$((BARWID * tickspaces[$i]))"
echo -n ' | '
done
echo
# Print tick labels
for (( i=0; i<"$numticks"; i+=1 )); do
[[ "${tickspaces[$i]}" -ne 0 ]] && repspace "$((BARWID * tickspaces[$i] - 2))"
echo -n "${ticks[$i]}"
done
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment