plugin/battery: split `upower` to two variables

pull/2074/head
John D Pell 2022-02-01 10:15:35 -08:00
parent c794f4f0e7
commit cade0a1e7a
1 changed files with 11 additions and 6 deletions

View File

@ -2,8 +2,10 @@
about-plugin 'display info about your battery charge level'
function ac_adapter_connected() {
local batteries
if _command_exists upower; then
upower -i "$(upower -e | grep --max-count=1 -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
acpi -a | grep -q "on-line"
elif _command_exists pmset; then
@ -16,8 +18,10 @@ function ac_adapter_connected() {
}
function ac_adapter_disconnected() {
local batteries
if _command_exists upower; then
upower -i "$(upower -e | grep --max-count=1 -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
acpi -a | grep -q "off-line"
elif _command_exists pmset; then
@ -33,16 +37,17 @@ function battery_percentage() {
about 'displays battery charge as a percentage of full (100%)'
group 'battery'
local command_output="no"
local command_output batteries
if _command_exists upower; then
command_output=$(upower --show-info "$(upower --enumerate | grep --max-count=1 -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
command_output=$(acpi -b | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}')
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
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
command_output=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List | grep -o '[0-9]\+' | head -1)
else