Skip to content

Instantly share code, notes, and snippets.

@AlexAtkinson
Last active June 28, 2023 06:23
Show Gist options
  • Save AlexAtkinson/b8f244970209e46604c9b4c33e205e88 to your computer and use it in GitHub Desktop.
Save AlexAtkinson/b8f244970209e46604c9b4c33e205e88 to your computer and use it in GitHub Desktop.
BASH: Weather Overlay for htop
# 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'
@AlexAtkinson
Copy link
Author

Screenshot_20220718_180714

@AlexAtkinson
Copy link
Author

Here's the ~/.config/htop/htoprc file for any who would like to skip the conifg screen.

# Beware! This file is rewritten by htop when settings are changed in the interface.
# The parser is also very primitive, and not human-friendly.
htop_version=3.2.2
config_reader_min_version=3
fields=0 48 17 18 38 39 40 2 46 47 49 1
hide_kernel_threads=0
hide_userland_threads=1
hide_running_in_container=0
shadow_other_users=1
show_thread_names=0
show_program_path=1
highlight_base_name=1
highlight_deleted_exe=1
shadow_distribution_path_prefix=0
highlight_megabytes=1
highlight_threads=1
highlight_changes=0
highlight_changes_delay_secs=5
find_comm_in_cmdline=1
strip_exe_from_cmdline=1
show_merged_command=1
header_margin=1
screen_tabs=1
detailed_cpu_time=0
cpu_count_from_one=0
show_cpu_usage=1
show_cpu_frequency=1
show_cpu_temperature=1
degree_fahrenheit=1
update_process_names=0
account_guest_in_cpu_meter=0
color_scheme=0
enable_mouse=0
delay=15
hide_function_bar=1
header_layout=two_33_67
column_meters_0=DateTime Blank Blank Blank Blank Blank Blank Blank Hostname Uptime Blank System SELinux Systemd Tasks LoadAverage
column_meter_modes_0=4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
column_meters_1=LeftCPUs8 RightCPUs8 Blank MemorySwap DiskIO NetworkIO
column_meter_modes_1=3 3 2 1 3 3
tree_view=0
sort_key=46
tree_sort_key=46
sort_direction=-1
tree_sort_direction=-1
tree_view_always_by_pid=0
all_branches_collapsed=0
screen:Main=PID USER PRIORITY NICE M_VIRT M_RESIDENT M_SHARE STATE PERCENT_CPU PERCENT_MEM TIME Command
.sort_key=PERCENT_CPU
.tree_sort_key=PERCENT_CPU
.tree_view=0
.tree_view_always_by_pid=0
.sort_direction=-1
.tree_sort_direction=-1
.all_branches_collapsed=0
screen:I/O=PID USER IO_PRIORITY IO_RATE IO_READ_RATE IO_WRITE_RATE
.sort_key=IO_RATE
.tree_sort_key=PID
.tree_view=0
.tree_view_always_by_pid=0
.sort_direction=-1
.tree_sort_direction=1
.all_branches_collapsed=0
screen:Foo=PID USER PERCENT_MEM TTY OOM Command
.sort_key=PERCENT_MEM
.tree_sort_key=PID
.tree_view=0
.tree_view_always_by_pid=0
.sort_direction=-1
.tree_sort_direction=1
.all_branches_collapsed=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment