bash_functions Bash function called `sys` that displays a system dashboard in the terminal. It collects and color-codes CPU usage, memory usage, disk usage, load average, uptime, and next backup time, then prints them with icons and a storage breakdown. The function uses thresholds to color metrics green, yellow, or red based on usage levels. Created May 21, 2026 08:47 - - Save xhaythemx/573a1000e3ecb1a7a7e8cca622f50e68 to your computer and use it in GitHub Desktop. This file contains hidden or 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 https://github.co/hiddenchars | ~/.bash functions | | | sys { | | | ── Colors ──────────────────────────────────────────── | | | local R=$'\e 0m' | | | local B=$'\e 1m' | | | local DIM=$'\e 2m' | | | local CYA=$'\e 38;5;110m' | | | local WHT=$'\e 38;5;255m' | | | local LBL=$'\e 38;5;250m' | | | local YEL=$'\e 38;5;179m' | | | local GRN=$'\e 38;5;108m' | | | local RED=$'\e 38;5;167m' | | | local MAG=$'\e 38;5;183m' | | | local SEP=$'\e 38;5;59m' | | | ── Collect stats ────────────────────────────────────── | | | read load1 < /proc/loadavg | | | local cpu pct | | | cpu pct=$ awk '/^cpu /{u=$2+$4; t=$2+$3+$4+$5; printf "%.0f", u 100/t}' /proc/stat | | | local mem pct mem used mem total | | | read mem total mem used < < free -m | awk '/Mem:/{print $2, $3}' | | | mem pct=$ awk "BEGIN{printf \"%.0f\", $mem used/$mem total 100}" | | | local disk pct | | | disk pct=$ df / | awk 'NR==2{gsub /%/,"",$5 ; print $5}' | | | local up str | | | up str=$ awk '{ | | | s = int $1 | | | d = int s/86400 ; s -= d 86400 | | | h = int s/3600 ; s -= h 3600 | | | m = int s/60 | | | if d printf "%dd %dh %dm", d, h, m | | | else if h printf "%dh %dm", h, m | | | else printf "%dm", m | | | }' /proc/uptime | | | local next backup | | | next backup=$ systemctl show borgmatic.timer --property=NextElapseUSecRealtime | cut -d= -f2 | awk '{print $1, $2}' | | | ── Threshold colors ─────────────────────────────────── | | | local cpu col ram col dsk col | | | if "$cpu pct" -ge 80 ; then cpu col="$RED" | | | elif "$cpu pct" -ge 50 ; then cpu col="$YEL" | | | else cpu col="$GRN" | | | fi | | | if "$mem pct" -ge 80 ; then ram col="$RED" | | | elif "$mem pct" -ge 60 ; then ram col="$YEL" | | | else ram col="$GRN" | | | fi | | | if "$disk pct" -ge 80 ; then dsk col="$RED" | | | elif "$disk pct" -ge 50 ; then dsk col="$YEL" | | | else dsk col="$GRN" | | | fi | | | ── Header ───────────────────────────────────────────── | | | printf "\n" | | | printf "${SEP}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${R}\n" | | | printf "${CYA}${B} System Dashboard${R}\n" | | | printf "${SEP}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${R}\n" | | | ── Status bar ───────────────────────────────────────── | | | printf "\n" | | | printf " ${cpu col} ${B}${cpu pct}%%${R} " | | | printf "${SEP}│${R} " | | | printf "${ram col} ${B}${mem pct}%%${R} " | | | printf "${SEP}│${R} " | | | printf "${dsk col} ${B}${disk pct}%%${R} " | | | printf "${SEP}│${R} " | | | printf "${LBL} ${WHT}${load1}${R} " | | | printf "${SEP}│${R} " | | | printf "${LBL} ${WHT}${up str}${R} " | | | printf "${SEP}│${R} " | | | printf "${CYA} ${WHT}${next backup}${R}\n" | | | ── Storage ──────────────────────────────────────────── | | | printf "\n ${CYA}${B} Storage${R}\n" | | | printf " ${SEP}───────────────────────────────────────────────────${R}\n" | | | df -hT -x tmpfs -x devtmpfs | awk \ | | | -v R="$R" -v DIM="$DIM" \ | | | -v CYA="$CYA" -v WHT="$WHT" -v LBL="$LBL" \ | | | -v YEL="$YEL" -v GRN="$GRN" -v RED="$RED" \ | | | -v MAG="$MAG" -v SEP="$SEP" ' | | | $2 ~ /^ ext4|btrfs|xfs $/ { | | | gsub /%/, "", $6 | | | pct = $6 + 0 | | | if pct < 50 col = GRN | | | else if pct < 80 col = YEL | | | else col = RED | | | bar = "" | | | n = int pct / 5 | | | for i = 0; i < 20; i++ | | | bar = bar i < n ? "█" : "░" | | | if $7 == "/" { | | | icon = "" | | | label = "OS SSD" | | | } else { | | | icon = "" | | | label = "DATA SSD" | | | } | | | printf " %s%s %-10s%s %s%-5s%s %ssize%s %-7s %sused%s %-7s %sfree%s %-7s %s %s %s %s%3s%%%s %s%s%s\n", | | | MAG, icon, label, R, | | | DIM, $2, R, | | | LBL, WHT, $3, | | | LBL, WHT, $4, | | | LBL, WHT, $5, | | | col, bar, R, | | | col, pct, R, | | | SEP, $7, R | | | }' | | | ── Temperatures ─────────────────────────────────────── | | | printf "\n ${CYA}${B} Temperatures${R}\n" | | | printf " ${SEP}───────────────────────────────────────────────────${R}\n" | | | local labels= "OS SSD" "DATA SSD" | | | local i=0 | | | for disk in /dev/nvme0n1 /dev/nvme1n1; do | | | local raw temp c | | | raw=$ sudo nvme smart-log "$disk" 2 /dev/null | grep -i '^temperature' | | | if echo "$raw" | grep -qi 'celsius\|°C'; then | | | temp c=$ echo "$raw" | grep -oE ' 0-9 +' | head -n1 | | | elif echo "$raw" | grep -qi 'fahrenheit\|°F'; then | | | local f | | | f=$ echo "$raw" | grep -oE ' 0-9 +' | head -n1 | | | -n "$f" && temp c=$ awk "BEGIN{printf \"%.0f\", $f-32 5/9}" | | | else | | | temp c=$ echo "$raw" | grep -oE ' 0-9 +' | head -n1 | | | fi | | | local t col | | | if "${temp c:-0}" -ge 65 ; then t col="$RED" | | | elif "${temp c:-0}" -ge 50 ; then t col="$YEL" | | | else t col="$GRN" | | | fi | | | local fill=0 bar="" | | | -n "$temp c" && fill=$ temp c < 100 ? temp c : 99 20 / 100 | | | for j=0; j<20; j++ ; do | | | $j -lt $fill && bar+="█" || bar+="░" | | | done | | | printf " ${CYA} ${MAG}%-10s${R} ${t col}${B}%s°C${R} ${DIM} ${t col}%s${DIM} ${R}\n" \ | | | "${labels $i }" "${temp c:---}" "$bar" | | | i++ | | | done | | | ── Footer ───────────────────────────────────────────── | | | printf "\n${SEP}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${R}\n\n" | | | } |