People have asked me how to display the weather on the status bar of my tmux session, as shown in a previous post of this blog.
It’s actually very easy, and it uses two web services: one requests my current public IP address, the other requests the weather at that location1.
#!/usr/bin/env bash
# This script requires curl:
IP=$(curl https://ipinfo.io/ip --silent)
# Build a suitable URL and call it
RESULT=$(curl --silent https://wttr.in/"$IP"?format="%c+%t" | xargs)
# Deal with some strange possible outcomes
if [[ $RESULT == Unknow* ]]; then
echo "n/a"
elif [[ $RESULT == \<!DOCTYPE* ]]; then
echo "n/a"
elif [[ $RESULT == Sorr* ]]; then
echo "n/a"
elif [[ $RESULT == This* ]]; then
echo "n/a"
else
echo "$RESULT"
fi
Save the contents of the snippet above as weather.sh
, save it on your $PATH
, make it executable with a good old chmod +x weather.sh
and then add the following line to your .tmux.conf
configuration:
set -g status-right "| #(weather.sh) | %a %b %e, %H:%M "
This will display the output of the script and then the current date and time.
In the screenshot shown in my previous post there are a few more pieces of information shown, including a nice graphical display of the current battery level, provided by other scripts than the one explained here, of which I’ll talk about next week.
I know, I know, this approach is quite rough and not very precise, but in my experience it works quite well. Also, be mindful that if you use a VPNā¦ well, you’ll get the weather at another place on Earth, not necessarily close to where one is. ↩︎