#!/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 "$PREV_FILE" # Build output — one block character per core output="" for ((i=0; i${char}" else output+="${char}" fi done echo -e "CPU $output\n"