Add battery support for Linux system-info script

This commit is contained in:
2023-06-01 21:52:27 +01:00
parent d852c46bc8
commit b5c2f8343a
2 changed files with 23 additions and 4 deletions

View File

@@ -22,17 +22,36 @@ else
}
fi
if [ "`acpi -b`" != "No support for device type: power_supply" ]; then
function get_battery {
acpi -b | \
awk '{ print $4 }' | \
awk '$battery ~ /.*/ {
printf "%3d%% %s\n", $battery, substr("󰂎󰁺󰁻󰁼󰁽󰁾󰁿󰂀󰂁󰂂󰁹", int($battery / 9), 1)
}'
}
else
function get_battery {
echo ''
}
fi
# Cleanup cache file when interrupted.
trap '[ -f $cache_file ] && rm $cache_file; exit' INT
trap '[ -f $cache_file ] && rm $cache_file; exit' TERM
while true; do
# Parse the current CPU load on all cores/threads.
cpu_load=`mpstat -P ALL -n 1 -u 1 -o JSON | \
cpu_load=" `mpstat -P ALL -n 1 -u 1 -o JSON | \
jq '.sysstat.hosts[0].statistics[0]["cpu-load"][1:]|.[].idle' | \
awk '$idle ~ /[-.0-9]*/ { printf "%s", substr("█▇▆▅▄▃▂▁ ", int($idle / 11), 1) }'`
awk '$idle ~ /[-.0-9]*/ { printf "%s", substr("█▇▆▅▄▃▂▁ ", int($idle / 11), 1) }'`"
# Parse the current CPU package temperature.
cpu_temp=$(get_cpu_temp)
# Get the battery status if present.
battery=$(get_battery)
# Write to the cache file.
echo "$cpu_temp $cpu_load" > $cache_file
echo "$cpu_temp$cpu_load$battery" > $cache_file
done