aurora35d4d85
waybar: add waycat/cpucat custom module
+85 −1
4 files changed
- M .config/waybar/config.jsonc +5 −1
- A .config/waybar/scripts/catloop.sh +72 −0
- M .config/waybar/style.css +8 −0
- A .local/share/fonts/Waycat.ttf
| @@ -5,7 +5,8 @@ | |||
| 5 | 5 | "width": 0, // Waybar width (0 == auto) | |
| 6 | 6 | "spacing": 0, // Gaps between modules | |
| 7 | 7 | "modules-left": [ | |
| 8 | − | "hyprland/workspaces" | |
| 8 | + | "hyprland/workspaces", | |
| 9 | + | "custom/cpucat" | |
| 9 | 10 | ], | |
| 10 | 11 | "modules-center" : [ | |
| 11 | 12 | "hyprland/window" | |
| @@ -86,6 +87,9 @@ | |||
| 86 | 87 | "interval": "once", | |
| 87 | 88 | "on-click": "~/.local/bin/envyswitch", | |
| 88 | 89 | }, | |
| 90 | + | "custom/cpucat": { | |
| 91 | + | "exec":"~/.config/waybar/scripts/catloop.sh", | |
| 92 | + | }, | |
| 89 | 93 | "cpu": { | |
| 90 | 94 | "format": " {usage:2}%", | |
| 91 | 95 | "interval": 1, | |
| @@ -0,0 +1,72 @@ | |||
| 1 | + | #!/usr/bin/env bash | |
| 2 | + | set -euo pipefail | |
| 3 | + | ||
| 4 | + | CPU_USAGE=0.0 | |
| 5 | + | SLEEP_AFTER=4 | |
| 6 | + | ||
| 7 | + | AWAKE_FRAMES=($(eval echo {A..E})) | |
| 8 | + | SLEEP_FRAMES=($(eval echo {G..N})) | |
| 9 | + | ||
| 10 | + | COUNT=0 | |
| 11 | + | ||
| 12 | + | read_cpu() { | |
| 13 | + | awk '/^cpu /{ | |
| 14 | + | user=$2; nice=$3; sys=$4; idle=$5; | |
| 15 | + | active = user + nice + sys; | |
| 16 | + | total = active + idle; | |
| 17 | + | printf "%d %d\n", active, total; | |
| 18 | + | exit | |
| 19 | + | }' /proc/stat | |
| 20 | + | } | |
| 21 | + | ||
| 22 | + | read prev_active prev_total < <(read_cpu) | |
| 23 | + | ||
| 24 | + | while true; do | |
| 25 | + | ||
| 26 | + | read active total < <(read_cpu) | |
| 27 | + | ||
| 28 | + | delta_active=$((active - prev_active)) | |
| 29 | + | delta_total=$((total - prev_total)) | |
| 30 | + | ||
| 31 | + | if [ "$delta_total" -le 0 ] || [ "$delta_active" -lt 0 ]; then | |
| 32 | + | utilization="0.0000" | |
| 33 | + | else | |
| 34 | + | utilization=$(awk -v a="$delta_active" -v t="$delta_total" 'BEGIN { printf "%.4f", (a / t) }') | |
| 35 | + | fi | |
| 36 | + | ||
| 37 | + | CPU_USAGE=$utilization | |
| 38 | + | ||
| 39 | + | SPEED=$(awk -v u="$CPU_USAGE" 'BEGIN { printf "%.2f", 1 / (4 + (u * 100)) }') | |
| 40 | + | # clip minimum speed | |
| 41 | + | if (( $(echo "$SPEED < 0.03" | bc -l) )); then | |
| 42 | + | SPEED=0.03 | |
| 43 | + | fi | |
| 44 | + | ||
| 45 | + | ### MAKE THE CAT SLEEP IF CPU USAGE IS LOW after SLEEP_AFTER ### | |
| 46 | + | if (( $(echo "$CPU_USAGE < 0.02" | bc -l) )); then | |
| 47 | + | COUNT=$((COUNT + 1)) | |
| 48 | + | else | |
| 49 | + | COUNT=0 | |
| 50 | + | fi | |
| 51 | + | ||
| 52 | + | ### CAT IS SLEEPING ### | |
| 53 | + | if [ $COUNT -ge $SLEEP_AFTER ]; then | |
| 54 | + | for s in "${SLEEP_FRAMES[@]}";do | |
| 55 | + | echo $s | |
| 56 | + | sleep $SPEED | |
| 57 | + | done | |
| 58 | + | ||
| 59 | + | ### CAT IS AWAKE ### | |
| 60 | + | else | |
| 61 | + | for i in "${AWAKE_FRAMES[@]}";do | |
| 62 | + | echo $i | |
| 63 | + | sleep $SPEED | |
| 64 | + | done | |
| 65 | + | ||
| 66 | + | fi | |
| 67 | + | ||
| 68 | + | prev_active=$active | |
| 69 | + | prev_total=$total | |
| 70 | + | done | |
| 71 | + | ||
| 72 | + | # vim: set ts=2 sw=2 | |
| @@ -113,6 +113,14 @@ window#waybar.hidden { | |||
| 113 | 113 | ||
| 114 | 114 | } | |
| 115 | 115 | ||
| 116 | + | #custom-cpucat { | |
| 117 | + | font-family: "Waycat", monospace; | |
| 118 | + | font-size: 14px; | |
| 119 | + | font-weight: bold; | |
| 120 | + | color: @foreground; | |
| 121 | + | padding: 0 12px 0 12px; | |
| 122 | + | } | |
| 123 | + | ||
| 116 | 124 | /* If workspaces is the leftmost module, omit left margin */ | |
| 117 | 125 | .modules-left>widget:first-child>#workspaces { | |
| 118 | 126 | margin-left: 0; | |
[Binary file]