Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin/battery: fix handling of multiple batteries with upower #2105

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions plugins/available/battery.plugin.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ about-plugin 'display info about your battery charge level'
function ac_adapter_connected() {
local batteries
if _command_exists upower; then
batteries="$(upower -e | grep --max-count=1 -i BAT)"
upower -i "${batteries}" | grep 'state' | grep -q 'charging\|fully-charged'
IFS=$'\n' read -d '' -ra batteries < <(upower -e | grep -i BAT)
upower -i "${batteries[0]:-}" | grep 'state' | grep -q 'charging\|fully-charged'
elif _command_exists acpi; then
acpi -a | grep -q "on-line"
elif _command_exists pmset; then
Expand All @@ -20,8 +20,8 @@ function ac_adapter_connected() {
function ac_adapter_disconnected() {
local batteries
if _command_exists upower; then
batteries="$(upower -e | grep --max-count=1 -i BAT)"
upower -i "${batteries}" | grep 'state' | grep -q 'discharging'
IFS=$'\n' read -d '' -ra batteries < <(upower -e | grep -i BAT)
upower -i "${batteries[0]:-}" | grep 'state' | grep -q 'discharging'
elif _command_exists acpi; then
acpi -a | grep -q "off-line"
elif _command_exists pmset; then
Expand All @@ -40,8 +40,8 @@ function battery_percentage() {
local command_output batteries

if _command_exists upower; then
batteries="$(upower --enumerate | grep --max-count=1 -i BAT)"
command_output="$(upower --show-info "${batteries:-}" | grep percentage | grep -o '[0-9]\+' | head -1)"
IFS=$'\n' read -d '' -ra batteries < <(upower -e | grep -i BAT)
command_output="$(upower --show-info "${batteries[0]:-}" | 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
Expand Down
3 changes: 1 addition & 2 deletions test/plugins/battery.plugin.bats
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ function setup_upower {
function upower {
case $1 in
'-e'|'--enumerate')
# don't just `echo` twice because `grep` will close the pipe after matching the first line...
echo "$BAT0"$'\n'"/org/freedesktop/UPower/devices/mouse_hid_${RANDOM}_battery"
printf '%s\n' "$BAT0" "/org/freedesktop/UPower/devices/mouse_hid_${RANDOM}_battery"
;;
'-i'|'--show-info')
if [[ $2 == "$BAT0" ]]
Expand Down