plugin/battery: `shellcheck`

pull/2064/head
John D Pell 2022-01-25 22:13:19 -08:00
parent 2575d64deb
commit 3cb5f3f7e6
2 changed files with 112 additions and 135 deletions

View File

@ -90,6 +90,7 @@ plugins/available/alias-completion.plugin.bash
plugins/available/autojump.plugin.bash plugins/available/autojump.plugin.bash
plugins/available/base.plugin.bash plugins/available/base.plugin.bash
plugins/available/basher.plugin.bash plugins/available/basher.plugin.bash
plugins/available/battery.plugin.bash
plugins/available/blesh.plugin.bash plugins/available/blesh.plugin.bash
plugins/available/cmd-returned-notify.plugin.bash plugins/available/cmd-returned-notify.plugin.bash
plugins/available/direnv.plugin.bash plugins/available/direnv.plugin.bash

View File

@ -1,149 +1,125 @@
cite about-plugin # shellcheck shell=bash
about-plugin 'display info about your battery charge level' about-plugin 'display info about your battery charge level'
ac_adapter_connected(){ function ac_adapter_connected() {
if _command_exists upower; if _command_exists upower; then
then upower -i "$(upower -e | grep -i BAT)" | grep 'state' | grep -q 'charging\|fully-charged'
upower -i $(upower -e | grep -i BAT) | grep 'state' | grep -q 'charging\|fully-charged' elif _command_exists acpi; then
return $?
elif _command_exists acpi;
then
acpi -a | grep -q "on-line" acpi -a | grep -q "on-line"
return $? elif _command_exists pmset; then
elif _command_exists pmset;
then
pmset -g batt | grep -q 'AC Power' pmset -g batt | grep -q 'AC Power'
return $? elif _command_exists ioreg; then
elif _command_exists ioreg;
then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = Yes' ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = Yes'
return $? elif _command_exists WMIC; then
elif _command_exists WMIC;
then
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=2' WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=2'
return $?
fi fi
} }
ac_adapter_disconnected(){ function ac_adapter_disconnected() {
if _command_exists upower; if _command_exists upower; then
then upower -i "$(upower -e | grep -i BAT)" | grep 'state' | grep -q 'discharging'
upower -i $(upower -e | grep -i BAT) | grep 'state' | grep -q 'discharging' elif _command_exists acpi; then
return $?
elif _command_exists acpi;
then
acpi -a | grep -q "off-line" acpi -a | grep -q "off-line"
return $? elif _command_exists pmset; then
elif _command_exists pmset;
then
pmset -g batt | grep -q 'Battery Power' pmset -g batt | grep -q 'Battery Power'
return $? elif _command_exists ioreg; then
elif _command_exists ioreg;
then
ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = No' ioreg -n AppleSmartBattery -r | grep -q '"ExternalConnected" = No'
return $? elif _command_exists WMIC; then
elif _command_exists WMIC;
then
WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=1' WMIC Path Win32_Battery Get BatteryStatus /Format:List | grep -q 'BatteryStatus=1'
return $?
fi fi
} }
battery_percentage(){ 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'
declare COMMAND_OUTPUT="no" local command_output="no"
if _command_exists upower; if _command_exists upower; then
then command_output=$(upower --show-info "$(upower --enumerate | grep -i BAT)" | grep percentage | grep -o "[0-9]\+" | head -1)
COMMAND_OUTPUT=$(upower --show-info $(upower --enumerate | grep -i BAT) | grep percentage | grep -o "[0-9]\+" | head -1) elif _command_exists acpi; then
elif _command_exists acpi; command_output=$(acpi -b | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}')
then elif _command_exists pmset; then
COMMAND_OUTPUT=$(acpi -b | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}' ) command_output=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p' | grep -o "[0-9]\+" | head -1)
elif _command_exists pmset; elif _command_exists ioreg; then
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=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p' | grep -o "[0-9]\+" | head -1) elif _command_exists WMIC; then
elif _command_exists ioreg; command_output=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List | grep -o '[0-9]\+' | head -1)
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 else
COMMAND_OUTPUT="no" command_output="no"
fi fi
if [ "${COMMAND_OUTPUT}" != "no" ]; then if [[ "${command_output}" != "no" ]]; then
printf "%02d" "${COMMAND_OUTPUT:--1}" printf "%02d" "${command_output:--1}"
else else
echo "${COMMAND_OUTPUT}" echo "${command_output}"
fi fi
} }
battery_charge(){ function battery_charge() {
about 'graphical display of your battery charge' about 'graphical display of your battery charge'
group 'battery' group 'battery'
# Full char # Full char
local F_C='▸' local f_c='▸'
# Depleted char # Depleted char
local D_C='▹' local d_c='▹'
local DEPLETED_COLOR="${normal}" local depleted_color="${normal?}"
local FULL_COLOR="${green}" local full_color="${green?}"
local HALF_COLOR="${yellow}" local half_color="${yellow?}"
local DANGER_COLOR="${red}" local danger_color="${red?}"
local BATTERY_OUTPUT="${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${D_C}" #local battery_output="${depleted_color}${d_c}${d_c}${d_c}${d_c}${d_c}"
local BATTERY_PERC=$(battery_percentage) local battery_percentage
battery_percentage=$(battery_percentage)
case $BATTERY_PERC in case $battery_percentage in
no) no)
echo "" echo ""
;; ;;
9*) 9*)
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${F_C}${F_C}${normal}" echo "${full_color}${f_c}${f_c}${f_c}${f_c}${f_c}${normal?}"
;; ;;
8*) 8*)
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${F_C}${HALF_COLOR}${F_C}${normal}" echo "${full_color}${f_c}${f_c}${f_c}${f_c}${half_color}${f_c}${normal?}"
;; ;;
7*) 7*)
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${F_C}${DEPLETED_COLOR}${D_C}${normal}" echo "${full_color}${f_c}${f_c}${f_c}${f_c}${depleted_color}${d_c}${normal?}"
;; ;;
6*) 6*)
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${normal}" echo "${full_color}${f_c}${f_c}${f_c}${half_color}${f_c}${depleted_color}${d_c}${normal?}"
;; ;;
5*) 5*)
echo "${FULL_COLOR}${F_C}${F_C}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${normal}" echo "${full_color}${f_c}${f_c}${f_c}${depleted_color}${d_c}${d_c}${normal?}"
;; ;;
4*) 4*)
echo "${FULL_COLOR}${F_C}${F_C}${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${normal}" echo "${full_color}${f_c}${f_c}${half_color}${f_c}${depleted_color}${d_c}${d_c}${normal?}"
;; ;;
3*) 3*)
echo "${FULL_COLOR}${F_C}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${normal}" echo "${full_color}${f_c}${f_c}${depleted_color}${d_c}${d_c}${d_c}${normal?}"
;; ;;
2*) 2*)
echo "${FULL_COLOR}${F_C}${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${normal}" echo "${full_color}${f_c}${half_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${normal?}"
;; ;;
1*) 1*)
echo "${FULL_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}" echo "${full_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;; ;;
05) 05)
echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}" echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;; ;;
04) 04)
echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}" echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;; ;;
03) 03)
echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}" echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;; ;;
02) 02)
echo "${DANGER_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}" echo "${danger_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;; ;;
0*) 0*)
echo "${HALF_COLOR}${F_C}${DEPLETED_COLOR}${D_C}${D_C}${D_C}${D_C}${normal}" echo "${half_color}${f_c}${depleted_color}${d_c}${d_c}${d_c}${d_c}${normal?}"
;; ;;
*) *)
echo "${DANGER_COLOR}UNPLG${normal}" echo "${danger_color}UNPLG${normal?}"
;; ;;
esac esac
} }