You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.1 KiB
80 lines
2.1 KiB
4 years ago
|
#!/bin/bash
|
||
|
|
||
|
# Enter your zipcode here for local weather.
|
||
|
zipcode=20001
|
||
|
|
||
|
# Colors are nice
|
||
|
blk=$(tput setaf 0)
|
||
|
red=$(tput setaf 1)
|
||
|
grn=$(tput setaf 2)
|
||
|
ylw=$(tput setaf 3)
|
||
|
blu=$(tput setaf 4)
|
||
|
cyn=$(tput setaf 6)
|
||
|
wht=$(tput setaf 7)
|
||
|
und=$(tput smul)
|
||
|
bld=$(tput bold)
|
||
|
# Uncoloring is important
|
||
|
rst=$(tput sgr0)
|
||
|
# Get the number of columns and rows to center the
|
||
|
# last updated time on the bottom of the window.
|
||
|
cols=$(tput cols)
|
||
|
rows=$(tput lines)
|
||
|
|
||
|
# trap ctrl-c and call ctrl_c()
|
||
|
# https://rimuhosting.com/knowledgebase/linux/misc/trapping-ctrl-c-in-bash
|
||
|
trap ctrl_c INT
|
||
|
|
||
|
function ctrl_c() {
|
||
|
clear
|
||
|
tput cnorm
|
||
|
echo "${rst}${red}CTRL+C Detected, exiting.${rst}"
|
||
|
exit
|
||
|
}
|
||
|
|
||
|
function dothething() {
|
||
|
# Grab the size of the window again as the window size
|
||
|
# may have changed since the script was started.
|
||
|
cols=$(tput cols)
|
||
|
rows=$(tput lines)
|
||
|
clear
|
||
|
updatedstring="Last updated: `date \"+%F %X\"`"
|
||
|
# Sometimes this may not complete due to wttr.in running
|
||
|
# out of available queries to their weather provider.
|
||
|
# This should only last for a day or so.
|
||
|
curl wttr.in/${zipcode}?1QnuFA
|
||
|
# Make sure the updated line is at the bottom of the screen
|
||
|
down=$rows
|
||
|
# Do some math to center the string horizontally
|
||
|
let over="$cols/2-${#updatedstring}/2"
|
||
|
# Move the cursor over to the correct starting location
|
||
|
tput cup $down $over
|
||
|
# Print the last time the weather was updated
|
||
|
echo -n "${rst}${ylw}${updatedstring}${rst}"
|
||
|
# Reset the cursor back to 0,0
|
||
|
tput cup 0 0
|
||
|
}
|
||
|
|
||
|
# Clear the screen and let's get going
|
||
|
clear
|
||
|
# Make the cursor invisible for the terminal
|
||
|
tput civis
|
||
|
# Programmers are great at naming things
|
||
|
dothething
|
||
|
# Loop forever. Use ctrl+c to exit.
|
||
|
while true; do
|
||
|
# We want to make sure we only check once per hour
|
||
|
# in order to not overwhelm the wttr.in service.
|
||
|
|
||
|
# Octal fix: https://stackoverflow.com/a/24777667
|
||
|
if [ `date +%M` -eq "00" ]; then
|
||
|
# Don't forget to check the seconds or it'll run
|
||
|
# the command every second of the 0th minute.
|
||
|
if [ `date +%S` -eq "00" ]; then
|
||
|
dothething
|
||
|
fi
|
||
|
fi
|
||
|
# Sleep for a second so we're not hammering the system
|
||
|
# with date checks.
|
||
|
sleep 1
|
||
|
done
|