Skip to content

Commit

Permalink
Merge pull request #2064 from gaelicWizard/plugin/battery
Browse files Browse the repository at this point in the history
plugin/battery: `shellcheck`
  • Loading branch information
NoahGorny committed Jan 26, 2022
2 parents 3c2bc3d + 3cb5f3f commit 0c24eda
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 135 deletions.
1 change: 1 addition & 0 deletions clean_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ plugins/available/alias-completion.plugin.bash
plugins/available/autojump.plugin.bash
plugins/available/base.plugin.bash
plugins/available/basher.plugin.bash
plugins/available/battery.plugin.bash
plugins/available/blesh.plugin.bash
plugins/available/cmd-returned-notify.plugin.bash
plugins/available/direnv.plugin.bash
Expand Down
246 changes: 111 additions & 135 deletions plugins/available/battery.plugin.bash
Original file line number Diff line number Diff line change
@@ -1,149 +1,125 @@
cite about-plugin
# shellcheck shell=bash
about-plugin 'display info about your battery charge level'

ac_adapter_connected(){
if _command_exists upower;
then
upower -i $(upower -e | grep -i BAT) | grep 'state' | grep -q 'charging\|fully-charged'
return $?
elif _command_exists acpi;
then
acpi -a | grep -q "on-line"
return $?
elif _command_exists pmset;
then
pmset -g batt | grep -q 'AC Power'
return $?
elif _command_exists ioreg;
then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = Yes'
return $?
elif _command_exists WMIC;
then
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=2'
return $?
fi
function ac_adapter_connected() {
if _command_exists upower; then
upower -i "$(upower -e | grep -i BAT)" | grep 'state' | grep -q 'charging\|fully-charged'
elif _command_exists acpi; then
acpi -a | grep -q "on-line"
elif _command_exists pmset; then
pmset -g batt | grep -q 'AC Power'
elif _command_exists ioreg; then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = Yes'
elif _command_exists WMIC; then
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=2'
fi
}

ac_adapter_disconnected(){
if _command_exists upower;
then
upower -i $(upower -e | grep -i BAT) | grep 'state' | grep -q 'discharging'
return $?
elif _command_exists acpi;
then
acpi -a | grep -q "off-line"
return $?
elif _command_exists pmset;
then
pmset -g batt | grep -q 'Battery Power'
return $?
elif _command_exists ioreg;
then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = No'
return $?
elif _command_exists WMIC;
then
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=1'
return $?
fi
function ac_adapter_disconnected() {
if _command_exists upower; then
upower -i "$(upower -e | grep -i BAT)" | grep 'state' | grep -q 'discharging'
elif _command_exists acpi; then
acpi -a | grep -q "off-line"
elif _command_exists pmset; then
pmset -g batt | grep -q 'Battery Power'
elif _command_exists ioreg; then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = No'
elif _command_exists WMIC; then
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=1'
fi
}

battery_percentage(){
about 'displays battery charge as a percentage of full (100%)'
group 'battery'
function battery_percentage() {
about 'displays battery charge as a percentage of full (100%)'
group 'battery'

declare COMMAND_OUTPUT="no"
local command_output="no"

if _command_exists upower;
then
COMMAND_OUTPUT=$(upower --show-info $(upower --enumerate | grep -i BAT) | 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)
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)
elif _command_exists WMIC;
then
COMMAND_OUTPUT=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List | grep -o '[0-9]\+' | head -1)
else
COMMAND_OUTPUT="no"
fi
if _command_exists upower; then
command_output=$(upower --show-info "$(upower --enumerate | grep -i BAT)" | 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)
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)
elif _command_exists WMIC; then
command_output=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List | grep -o '[0-9]\+' | head -1)
else
command_output="no"
fi

if [ "${COMMAND_OUTPUT}" != "no" ]; then
printf "%02d" "${COMMAND_OUTPUT:--1}"
else
echo "${COMMAND_OUTPUT}"
fi
if [[ "${command_output}" != "no" ]]; then
printf "%02d" "${command_output:--1}"
else
echo "${command_output}"
fi
}

battery_charge(){
about 'graphical display of your battery charge'
group 'battery'
function battery_charge() {
about 'graphical display of your battery charge'
group 'battery'

# Full char
local F_C=''
# Depleted char
local D_C=''
local DEPLETED_COLOR="${normal}"
local FULL_COLOR="${green}"
local HALF_COLOR="${yellow}"
local DANGER_COLOR="${red}"
local BATTERY_OUTPUT="${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${D_C}"
local BATTERY_PERC=$(battery_percentage)
# Full char
local f_c=''
# Depleted char
local d_c=''
local depleted_color="${normal?}"
local full_color="${green?}"
local half_color="${yellow?}"
local danger_color="${red?}"
#local battery_output="${depleted_color}${d_c}${d_c}${d_c}${d_c}${d_c}"
local battery_percentage
battery_percentage=$(battery_percentage)

case $BATTERY_PERC in
no)
echo ""
;;
9*)
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${F_C}${F_C}${normal}"
;;
8*)
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${F_C}${HALF_COLOR}${F_C}${normal}"
;;
7*)
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${F_C}${DEPLETED_COLOR}${D_C}${normal}"
;;
6*)
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${normal}"
;;
5*)
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${normal}"
;;
4*)
echo "${FULL_COLOR}${F_C}${F_C}${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${normal}"
;;
3*)
echo "${FULL_COLOR}${F_C}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${normal}"
;;
2*)
echo "${FULL_COLOR}${F_C}${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${normal}"
;;
1*)
echo "${FULL_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}"
;;
05)
echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}"
;;
04)
echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}"
;;
03)
echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}"
;;
02)
echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}"
;;
0*)
echo "${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}"
;;
*)
echo "${DANGER_COLOR}UNPLG${normal}"
;;
esac
case $battery_percentage in
no)
echo ""
;;
9*)
echo "${full_color}${f_c}${f_c}${f_c}${f_c}${f_c}${normal?}"
;;
8*)
echo "${full_color}${f_c}${f_c}${f_c}${f_c}${half_color}${f_c}${normal?}"
;;
7*)
echo "${full_color}${f_c}${f_c}${f_c}${f_c}${depleted_color}${d_c}${normal?}"
;;
6*)
echo "${full_color}${f_c}${f_c}${f_c}${half_color}${f_c}${depleted_color}${d_c}${normal?}"
;;
5*)
echo "${full_color}${f_c}${f_c}${f_c}${depleted_color}${d_c}${d_c}${normal?}"
;;
4*)
echo "${full_color}${f_c}${f_c}${half_color}${f_c}${depleted_color}${d_c}${d_c}${normal?}"
;;
3*)
echo "${full_color}${f_c}${f_c}${depleted_color}${d_c}${d_c}${d_c}${normal?}"
;;
2*)
echo "${full_color}${f_c}${half_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${normal?}"
;;
1*)
echo "${full_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
05)
echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
04)
echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
03)
echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
02)
echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
0*)
echo "${half_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;;
*)
echo "${danger_color}UNPLG${normal?}"
;;
esac
}

0 comments on commit 0c24eda

Please sign in to comment.