From 235bf058e57d77743a377d85cd7bf2dc4136c415 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Fri, 6 Jan 2023 21:44:05 +0000 Subject: [PATCH] system-info: Enable CPU load and battery icon on macOS --- system-info/system-info-WSL.sh | 6 +++--- system-info/system-info-macOS.sh | 28 +++++++++++++++++++++------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/system-info/system-info-WSL.sh b/system-info/system-info-WSL.sh index 70ecda3..230aac1 100755 --- a/system-info/system-info-WSL.sh +++ b/system-info/system-info-WSL.sh @@ -36,9 +36,9 @@ while true; do "(Get-WmiObject win32_battery).EstimatedChargeRemaining" \ | sed 's/\r//') if [ "" != "$raw_battery" ]; then - battery="$(printf "%3d%%" "$raw_battery") $(echo $raw_battery | \ - awk '$raw_battery ~ /.*/ { printf "%s\n", \ - substr("", int($raw_battery / 9), 1) }')" + battery="$(echo $raw_battery | awk '$battery ~ /.*/ { + printf "%3d%% %s\n", $battery, substr("", int($battery / 9), 1) + }')" fi echo "$cpu_temp$cpu_load$battery" > $cache_file diff --git a/system-info/system-info-macOS.sh b/system-info/system-info-macOS.sh index 38ce9fb..3fca948 100755 --- a/system-info/system-info-macOS.sh +++ b/system-info/system-info-macOS.sh @@ -17,13 +17,27 @@ ioreg -w0 -l | grep BatteryInstalled &> /dev/null && \ has_battery=true || has_battery=false while true; do - # Get the current CPU temperature. - cpu_temp="`/usr/local/bin/osx-cpu-temp`" + # # Get the current CPU temperature. + # cpu_temp="`/usr/local/bin/osx-cpu-temp`" + + cpu_load=$(sudo powermetrics --format text \ + --sample-rate 1200 --sample-count 1 --samplers cpu_power | + grep --color=never -E 'CPU \d idle residency:' | + grep --color=never -Eo '\d+\.\d+' | + gawk '$idle ~ /[-.0-9]*/ { printf "%s", substr("█▇▆▅▄▃▂▁ ", int($idle / 10), 1) }' + ) + # Parse the current battery charge percentage. - $has_battery && \ - battery=" `pmset -g batt | grep --color=never -Eo '\d+%'` ↯" + if $has_battery; then + raw_battery="$(pmset -g batt | \ + grep --color=never 'InternalBattery' | \ + grep --color=never -Eo '\d+%' | \ + grep --color=never -Eo '\d+')" + battery="$(echo $raw_battery | gawk '$battery ~ /.*/ { + printf "%3d%% %s\n", $battery, substr("", int($battery / 9), 1) + }')" + fi + # Write to the cache file. - echo "$cpu_temp$battery" > $cache_file - # Don't spin, sleep instead. - sleep 2 + echo "$cpu_temp$cpu_load$battery" > $cache_file done