Merge pull request #1080 from nwinkler/battery-low

Fixed battery low
pull/1082/head
Nils Winkler 2017-11-05 12:57:19 +01:00 committed by GitHub
commit 5df98aeac9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 392 additions and 56 deletions

View File

@ -53,72 +53,31 @@ battery_percentage(){
about 'displays battery charge as a percentage of full (100%)'
group 'battery'
declare COMMAND_OUTPUT="no"
if _command_exists upower;
then
local UPOWER_OUTPUT=$(upower --show-info $(upower --enumerate | grep BAT) | grep percentage | tail --bytes 5)
echo ${UPOWER_OUTPUT: : -1}
COMMAND_OUTPUT=$(upower --show-info $(upower --enumerate | grep BAT) | grep percentage | grep -o "[0-9]\+" | head -1)
elif _command_exists acpi;
then
local ACPI_OUTPUT=$(acpi -b)
case $ACPI_OUTPUT in
*" Unknown"*)
local PERC_OUTPUT=$(echo $ACPI_OUTPUT | head -c 22 | tail -c 2)
case $PERC_OUTPUT in
*%)
echo "0${PERC_OUTPUT}" | head -c 2
;;
*)
echo ${PERC_OUTPUT}
;;
esac
;;
*" Charging"* | *" Discharging"*)
local PERC_OUTPUT=$(echo $ACPI_OUTPUT | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}' )
echo ${PERC_OUTPUT}
;;
*" Full"*)
echo '100'
;;
*)
echo '-1'
;;
esac
COMMAND_OUTPUT=$(acpi -b | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}' )
elif _command_exists pmset;
then
local PMSET_OUTPUT=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p')
case $PMSET_OUTPUT in
100*)
echo '100'
;;
*)
echo $PMSET_OUTPUT | head -c 2
;;
esac
COMMAND_OUTPUT=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p' | grep -o "[0-9]\+" | head -1)
elif _command_exists ioreg;
then
local IOREG_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: "?")}')
case $IOREG_OUTPUT in
100*)
echo '100'
;;
*)
echo $IOREG_OUTPUT | head -c 2
;;
esac
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
local WINPC=$(echo porcent=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List) | grep -o '[0-9]*')
case $WINPC in
100*)
echo '100'
;;
*)
echo $WINPC
;;
esac
COMMAND_OUTPUT=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List | grep -o '[0-9]\+' | head -1)
else
echo "no"
COMMAND_OUTPUT="no"
fi
if [ "${COMMAND_OUTPUT}" != "no" ]; then
printf "%02d" "${COMMAND_OUTPUT:--1}"
else
echo "${COMMAND_OUTPUT}"
fi
}

View File

@ -0,0 +1,371 @@
#!/usr/bin/env bats
load ../test_helper
load ../../lib/composure
cite _about _param _example _group _author _version
load ../../plugins/available/battery.plugin
# Sets up the `_command_exists` function so that it only responds `true` if called with
# the name of the function that was passed in as an argument to `setup_command_exists`.
# This is used to ensure that the test cases can test the various energy management tools
# without actually having them. When called like
#
# setup_command_exists "pmset"
#
# then calling `_command_exists "pmset"` will return `true`,
# while calling `_command_exists "ioreg"` (or other commands) will return `false`.
#
# It's cool that Bash allows to define functions within functions, works almost like
# a closure in JavaScript.
function setup_command_exists {
success_command="$1"
function _command_exists {
case "$1" in
"${success_command}")
true
;;
*)
false
;;
esac
}
}
#######################
#
# no tool
#
@test 'plugins battery: battery-percentage with no tool' {
setup_command_exists "fooooo"
run battery_percentage
assert_output "no"
}
#######################
#
# pmset
#
# Creates a `pmset` function that simulates output like the real `pmset` command.
# The passed in parameter is used for the remaining battery percentage.
function setup_pmset {
percent="$1"
function pmset {
printf "\-InternalBattery-0 (id=12345) %s; discharging; 16:00 remaining present: true" "${percent}"
}
}
@test 'plugins battery: battery-percentage with pmset, 100%' {
setup_command_exists "pmset"
setup_pmset "100%"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with pmset, 98%' {
setup_command_exists "pmset"
setup_pmset "98%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with pmset, 98.5%' {
setup_command_exists "pmset"
setup_pmset "98.5%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with pmset, 4%' {
setup_command_exists "pmset"
setup_pmset "4%"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with pmset, no status' {
setup_command_exists "pmset"
setup_pmset ""
run battery_percentage
assert_output "-1"
}
#######################
#
# acpi
#
# Creates a `acpi` function that simulates output like the real `acpi` command.
# The passed in parameters are used for
# 1) the remaining battery percentage.
# 2) the battery status
function setup_acpi {
percent="$1"
status="$2"
function acpi {
printf "Battery 0: %s, %s, 01:02:48 until charged" "${status}" "${percent}"
}
}
@test 'plugins battery: battery-percentage with acpi, 100% Full' {
setup_command_exists "acpi"
setup_acpi "100%" "Full"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with acpi, 98% Charging' {
setup_command_exists "acpi"
setup_acpi "98%" "Charging"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with acpi, 98% Discharging' {
setup_command_exists "acpi"
setup_acpi "98%" "Discharging"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with acpi, 98% Unknown' {
setup_command_exists "acpi"
setup_acpi "98%" "Unknown"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with acpi, 4% Charging' {
setup_command_exists "acpi"
setup_acpi "4%" "Charging"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with acpi, 4% no status' {
setup_command_exists "acpi"
setup_acpi "4%" ""
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with acpi, no status' {
setup_command_exists "acpi"
setup_acpi "" ""
run battery_percentage
assert_output "-1"
}
#######################
#
# upower
#
# Creates a `upower` function that simulates output like the real `upower` command.
# The passed in parameter is used for the remaining battery percentage.
function setup_upower {
percent="$1"
function upower {
printf "voltage: 12.191 V\n time to full: 57.3 minutes\n percentage: %s\n capacity: 84.6964" "${percent}"
}
}
@test 'plugins battery: battery-percentage with upower, 100%' {
setup_command_exists "upower"
setup_upower "100.00%"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with upower, 98%' {
setup_command_exists "upower"
setup_upower "98.4567%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with upower, 98.5%' {
setup_command_exists "upower"
setup_upower "98.5%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with upower, 4%' {
setup_command_exists "upower"
setup_upower "4.2345%"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with upower, no output' {
setup_command_exists "upower"
setup_upower ""
run battery_percentage
assert_output "-1"
}
#######################
#
# ioreg
#
# Creates a `ioreg` function that simulates output like the real `ioreg` command.
# The passed in parameter is used for the remaining battery percentage.
function setup_ioreg {
percent="$1"
function ioreg {
printf "\"MaxCapacity\" = 100\n\"CurrentCapacity\" = %s" "${percent}"
}
}
@test 'plugins battery: battery-percentage with ioreg, 100%' {
setup_command_exists "ioreg"
setup_ioreg "100%"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with ioreg, 98%' {
setup_command_exists "ioreg"
setup_ioreg "98%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with ioreg, 98.5%' {
setup_command_exists "ioreg"
setup_ioreg "98.5%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with ioreg, 4%' {
setup_command_exists "ioreg"
setup_ioreg "4%"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with ioreg, no status' {
setup_command_exists "ioreg"
# Simulate that no battery is present
function ioreg {
printf ""
}
run battery_percentage
assert_output "-1"
}
#######################
#
# WMIC
#
# Creates a `WMIC` function that simulates output like the real `WMIC` command.
# The passed in parameter is used for the remaining battery percentage.
function setup_WMIC {
percent="$1"
function WMIC {
printf "Charge: %s" "${percent}"
}
}
@test 'plugins battery: battery-percentage with WMIC, 100%' {
setup_command_exists "WMIC"
setup_WMIC "100%"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with WMIC, 98%' {
setup_command_exists "WMIC"
setup_WMIC "98%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with WMIC, 98.5%' {
setup_command_exists "WMIC"
setup_WMIC "98.5%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with WMIC, 4%' {
setup_command_exists "WMIC"
setup_WMIC "4%"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with WMIC, no status' {
setup_command_exists "WMIC"
setup_WMIC ""
run battery_percentage
assert_output "-1"
}

View File

@ -10,4 +10,10 @@ if [ -z "${BASH_IT}" ]; then
export BASH_IT
fi
exec $bats_executable ${CI:+--tap} ${test_directory}/{bash_it,completion,install,lib,plugins,themes}
if [ -z "$1" ]; then
test_dirs=( ${test_directory}/{bash_it,completion,install,lib,plugins,themes} )
else
test_dirs=( "$1" )
fi
exec $bats_executable ${CI:+--tap} "${test_dirs[@]}"