Merge pull request #2074 from NariyasuHeseri/master

plugin/battery: bug fix
pull/2086/head^2
Noah Gorny 2022-02-14 08:22:33 +02:00 committed by GitHub
commit 0619c19f65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 10 deletions

View File

@ -2,8 +2,10 @@
about-plugin 'display info about your battery charge level' about-plugin 'display info about your battery charge level'
function ac_adapter_connected() { function ac_adapter_connected() {
local batteries
if _command_exists upower; then if _command_exists upower; then
upower -i "$(upower -e | grep -i BAT)" | grep 'state' | grep -q 'charging\|fully-charged' batteries="$(upower -e | grep --max-count=1 -i BAT)"
upower -i "${batteries}" | grep 'state' | grep -q 'charging\|fully-charged'
elif _command_exists acpi; then elif _command_exists acpi; then
acpi -a | grep -q "on-line" acpi -a | grep -q "on-line"
elif _command_exists pmset; then elif _command_exists pmset; then
@ -16,8 +18,10 @@ function ac_adapter_connected() {
} }
function ac_adapter_disconnected() { function ac_adapter_disconnected() {
local batteries
if _command_exists upower; then if _command_exists upower; then
upower -i "$(upower -e | grep -i BAT)" | grep 'state' | grep -q 'discharging' batteries="$(upower -e | grep --max-count=1 -i BAT)"
upower -i "${batteries}" | grep 'state' | grep -q 'discharging'
elif _command_exists acpi; then elif _command_exists acpi; then
acpi -a | grep -q "off-line" acpi -a | grep -q "off-line"
elif _command_exists pmset; then elif _command_exists pmset; then
@ -33,16 +37,17 @@ function battery_percentage() {
about 'displays battery charge as a percentage of full (100%)' about 'displays battery charge as a percentage of full (100%)'
group 'battery' group 'battery'
local command_output="no" local command_output batteries
if _command_exists upower; then if _command_exists upower; then
command_output=$(upower --show-info "$(upower --enumerate | grep -i BAT)" | grep percentage | grep -o "[0-9]\+" | head -1) batteries="$(upower --enumerate | grep --max-count=1 -i BAT)"
command_output="$(upower --show-info "${batteries:-}" | grep percentage | grep -o '[0-9]\+' | head -1)"
elif _command_exists acpi; then elif _command_exists acpi; then
command_output=$(acpi -b | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}') command_output=$(acpi -b | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}')
elif _command_exists pmset; then elif _command_exists pmset; then
command_output=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p' | grep -o "[0-9]\+" | head -1) command_output=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p' | grep -o '[0-9]\+' | head -1)
elif _command_exists ioreg; then elif _command_exists ioreg; then
command_output=$(ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%05.2f"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}' | grep -o "[0-9]\+" | head -1) command_output=$(ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%05.2f"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}' | grep -o '[0-9]\+' | head -1)
elif _command_exists WMIC; then elif _command_exists WMIC; then
command_output=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List | grep -o '[0-9]\+' | head -1) command_output=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List | grep -o '[0-9]\+' | head -1)
else else

15
test/plugins/battery.plugin.bats 100644 → 100755
View File

@ -194,9 +194,24 @@ function setup_acpi {
# The passed in parameter is used for the remaining battery percentage. # The passed in parameter is used for the remaining battery percentage.
function setup_upower { function setup_upower {
percent="$1" percent="$1"
BAT0="/org/freedesktop/UPower/devices/battery_BAT$RANDOM"
function upower { function upower {
case $1 in
'-e'|'--enumerate')
echo "$BAT0"
echo "/org/freedesktop/UPower/devices/mouse_hid_${RANDOM}_battery"
;;
'-i'|'--show-info')
if [[ $2 == "$BAT0" ]]
then
printf "voltage: 12.191 V\n time to full: 57.3 minutes\n percentage: %s\n capacity: 84.6964" "${percent}" printf "voltage: 12.191 V\n time to full: 57.3 minutes\n percentage: %s\n capacity: 84.6964" "${percent}"
else
false
fi
;;
esac
} }
} }