Last active
June 28, 2023 06:23
-
-
Save AlexAtkinson/b8f244970209e46604c9b4c33e205e88 to your computer and use it in GitHub Desktop.
BASH: Weather Overlay for htop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# HTOP Weather Overlay | |
# NOTES: | |
# - STOP! Not a script. | |
# - Add to ~/.bashrc or similar. | |
# - Refreshes the weather "overlay" every ~10s | |
# - Pulls from wttr.in ONLY every 300s (5m) | |
# - has a kill-htop-weather command to break the while loop if needed | |
# | |
# Requires an empty space in the htop meters setup to be added. | |
# Recommended Meters layout (F2): | |
# Column 1 Column 2 | |
# ---------------------------------------------- | |
# Date and Time [LED] CPUs (1-8/16) [Graph] | |
# Blank [Text] CPUs (9-16/16) [Graph] | |
# Blank [Text] Blank [Text] | |
# Blank [Text] Memory & Swap [Bar] | |
# Blank [Text] Disk IO [Graph] | |
# Blank [Text] Network IO [Graph] | |
# Blank [Text] | |
# Hostname [Text] | |
# Uptime [Text] | |
# Blank [Text] | |
# System [Text] | |
# SELinux [Text] | |
# Systemd state [Text] | |
# Task counter [Text] | |
# Load average [Text] | |
function overlay-weather-for-htop() { | |
if [[ ! $(jobs | grep run-htop-weather) ]]; then | |
sleep 0.25 | |
while [[ $(ps ax | grep -v grep | grep " htop\|$(whereis htop | awk '{print $2}')" | wc -l) > 0 ]]; do | |
_TAG="run-htop-weather" | |
while read -r pts; do | |
_TAG="run-htop-weather" | |
weatherFile="$HOME/.htopweather" | |
maxAge=300 | |
locale="Toronto" | |
[[ ! -f $weatherFile ]] && (curl -s "wttr.in/${locale}?0pQ" | head -n -1 > $weatherFile) | |
[[ $(( $(date +"%s") - $(stat -c%Y $weatherFile) )) -gt $maxAge ]] && (curl -s 'wttr.in/Toronto?0pQ' | head -n -1 > $weatherFile) | |
( | |
row=4 | |
tput sc | |
tput cup $row 0 | |
while IFS= read -r line; do | |
printf "$line" | |
tput cup $((row+++1)) 0 | |
done<<<$(cat $weatherFile) | |
tput rc | |
tput cnorm | |
) <>/dev/$pts >&0 2>&1 | |
# Worked in Fedora # done<<<$(ps ax | grep -v grep | grep " htop\|$(whereis htop | awk '{print $2}')" | awk '{print $2}') | |
# The following works in Pop!_OS/Ubuntu | |
done<<<$(ps aux | grep -v grep | grep $USER | grep " htop\|$(whereis htop | awk '{print $2}')" | awk '{print $7}') | |
sleep 2 | |
done | |
sleep 2.1 | |
fi | |
} | |
# run in another term to aid debug: watch 'echo "AGE:$(( $(date +"%s") - $(stat -c%Y ~/.htopweather) ))"; cat ~/.htopweather' | |
function run-htop-weather() { | |
_TAG="run-htop-weather" overlay-weather-for-htop & | |
} | |
# NOTE: To kill htop weather proc: | |
# ps -ef | grep sleep # Kill ppid of sleep 5 proc. | |
function kill-htop-weather() { | |
kill -9 $(ps -o ppid= -p$(grep -l "\b_TAG=run-htop-weather\b" /proc/*/environ | awk -F/ '{print $3}')) | |
} | |
function htopw() { | |
#[[ ! $(jobs | grep run-htop-weather) ]] && exec -a htopweather run-htop-weather & | |
[[ $(grep -l "\b_TAG=run-htop-weather\b" /proc/*/environ | wc -l) == 0 ]] && run-htop-weather & | |
/usr/bin/htop $* | |
} | |
alias htop='htopw' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the ~/.config/htop/htoprc file for any who would like to skip the conifg screen.