1
0
Fork 0
archlinux/home/oli/bin/i3blocks/cpu

70 lines
1.7 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# Displays per-core CPU usage as Unicode block characters.
# Uses a temp file to track deltas between intervals.
#
# ▄ = load below 50%
# █ = load at or above 50%
# Colors escalate through warn1 -> warn2 -> crit thresholds.
PREV_FILE="/tmp/i3blocks_cpu_prev"
. ~/bin/i3blocks/_utils
# Read current idle and total ticks for each core from /proc/stat
declare -a cur_idle cur_total
i=0
while IFS=' ' read -r cpu user nice system idle iowait irq softirq rest; do
[[ "$cpu" =~ ^cpu[0-9]+$ ]] || continue
cur_idle[$i]=$((idle + iowait))
cur_total[$i]=$((user + nice + system + idle + iowait + irq + softirq))
((i++))
done < /proc/stat
num_cores=$i
# Load previous snapshot if available
declare -a prev_idle prev_total
if [[ -f "$PREV_FILE" ]]; then
i=0
while IFS=' ' read -r p_idle p_total; do
prev_idle[$i]=$p_idle
prev_total[$i]=$p_total
((i++))
done < "$PREV_FILE"
fi
# Save current snapshot for next run
for ((i=0; i<num_cores; i++)); do
echo "${cur_idle[$i]} ${cur_total[$i]}"
done > "$PREV_FILE"
# Build output — one block character per core
output=""
for ((i=0; i<num_cores; i++)); do
delta_total=$(( cur_total[i] - ${prev_total[$i]:-0} ))
delta_idle=$(( cur_idle[i] - ${prev_idle[$i]:-0} ))
if [[ $delta_total -eq 0 ]]; then
pct=0
else
pct=$(( (delta_total - delta_idle) * 100 / delta_total ))
fi
color=""
if [[ $pct -ge 50 ]]; then
color=$crit_bg
char="█"
elif [[ $pct -ge 15 ]]; then
color=$warn1_bg
char="▄"
else
char="_"
fi
if [[ -n "$color" ]]; then
output+="<span color=\"${color}\">${char}</span>"
else
output+="${char}"
fi
done
echo -e "CPU $output\n"