pull/2085/merge
John D Pell 2022-02-08 16:45:28 -08:00 committed by GitHub
commit 1a9c37d2b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 181 additions and 231 deletions

View File

@ -81,6 +81,7 @@ completion/available/vuejs.completion.bash
completion/available/wpscan.completion.bash
# libraries
lib/battery.bash
lib/colors.bash
lib/helpers.bash
lib/log.bash
@ -94,7 +95,6 @@ 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/colors.plugin.bash

125
lib/battery.bash 100644
View File

@ -0,0 +1,125 @@
# shellcheck shell=bash
about-plugin 'display info about your battery charge level'
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
}
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
}
function battery_percentage() {
about 'displays battery charge as a percentage of full (100%)'
group 'battery'
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_output}" != "no" ]]; then
printf "%02d" "${command_output:--1}"
else
echo "${command_output}"
fi
}
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_percentage
battery_percentage=$(battery_percentage)
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
}

View File

@ -1,125 +1,4 @@
# shellcheck shell=bash
about-plugin 'display info about your battery charge level'
# stub for renamed file
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
}
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
}
function battery_percentage() {
about 'displays battery charge as a percentage of full (100%)'
group 'battery'
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_output}" != "no" ]]; then
printf "%02d" "${command_output:--1}"
else
echo "${command_output}"
fi
}
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_percentage
battery_percentage=$(battery_percentage)
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
}
_disable-plugin battery

View File

@ -3,7 +3,9 @@
load ../test_helper
load ../test_helper_libs
load ../../plugins/available/battery.plugin
function local_setup() {
load "../../lib/battery.bash"
}
# 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`.
@ -37,7 +39,7 @@ function setup_command_exists {
# no tool
#
@test 'plugins battery: battery-percentage with no tool' {
@test 'lib battery: battery-percentage with no tool' {
setup_command_exists "fooooo"
run battery_percentage
@ -59,7 +61,7 @@ function setup_pmset {
}
}
@test 'plugins battery: battery-percentage with pmset, 100%' {
@test 'lib battery: battery-percentage with pmset, 100%' {
setup_command_exists "pmset"
setup_pmset "100%"
@ -68,7 +70,7 @@ function setup_pmset {
assert_output "100"
}
@test 'plugins battery: battery-percentage with pmset, 98%' {
@test 'lib battery: battery-percentage with pmset, 98%' {
setup_command_exists "pmset"
setup_pmset "98%"
@ -77,7 +79,7 @@ function setup_pmset {
assert_output "98"
}
@test 'plugins battery: battery-percentage with pmset, 98.5%' {
@test 'lib battery: battery-percentage with pmset, 98.5%' {
setup_command_exists "pmset"
setup_pmset "98.5%"
@ -86,7 +88,7 @@ function setup_pmset {
assert_output "98"
}
@test 'plugins battery: battery-percentage with pmset, 4%' {
@test 'lib battery: battery-percentage with pmset, 4%' {
setup_command_exists "pmset"
setup_pmset "4%"
@ -95,7 +97,7 @@ function setup_pmset {
assert_output "04"
}
@test 'plugins battery: battery-percentage with pmset, no status' {
@test 'lib battery: battery-percentage with pmset, no status' {
setup_command_exists "pmset"
setup_pmset ""
@ -122,7 +124,7 @@ function setup_acpi {
}
}
@test 'plugins battery: battery-percentage with acpi, 100% Full' {
@test 'lib battery: battery-percentage with acpi, 100% Full' {
setup_command_exists "acpi"
setup_acpi "100%" "Full"
@ -131,7 +133,7 @@ function setup_acpi {
assert_output "100"
}
@test 'plugins battery: battery-percentage with acpi, 98% Charging' {
@test 'lib battery: battery-percentage with acpi, 98% Charging' {
setup_command_exists "acpi"
setup_acpi "98%" "Charging"
@ -140,7 +142,7 @@ function setup_acpi {
assert_output "98"
}
@test 'plugins battery: battery-percentage with acpi, 98% Discharging' {
@test 'lib battery: battery-percentage with acpi, 98% Discharging' {
setup_command_exists "acpi"
setup_acpi "98%" "Discharging"
@ -149,7 +151,7 @@ function setup_acpi {
assert_output "98"
}
@test 'plugins battery: battery-percentage with acpi, 98% Unknown' {
@test 'lib battery: battery-percentage with acpi, 98% Unknown' {
setup_command_exists "acpi"
setup_acpi "98%" "Unknown"
@ -158,7 +160,7 @@ function setup_acpi {
assert_output "98"
}
@test 'plugins battery: battery-percentage with acpi, 4% Charging' {
@test 'lib battery: battery-percentage with acpi, 4% Charging' {
setup_command_exists "acpi"
setup_acpi "4%" "Charging"
@ -167,7 +169,7 @@ function setup_acpi {
assert_output "04"
}
@test 'plugins battery: battery-percentage with acpi, 4% no status' {
@test 'lib battery: battery-percentage with acpi, 4% no status' {
setup_command_exists "acpi"
setup_acpi "4%" ""
@ -176,7 +178,7 @@ function setup_acpi {
assert_output "04"
}
@test 'plugins battery: battery-percentage with acpi, no status' {
@test 'lib battery: battery-percentage with acpi, no status' {
setup_command_exists "acpi"
setup_acpi "" ""
@ -200,7 +202,7 @@ function setup_upower {
}
}
@test 'plugins battery: battery-percentage with upower, 100%' {
@test 'lib battery: battery-percentage with upower, 100%' {
setup_command_exists "upower"
setup_upower "100.00%"
@ -209,7 +211,7 @@ function setup_upower {
assert_output "100"
}
@test 'plugins battery: battery-percentage with upower, 98%' {
@test 'lib battery: battery-percentage with upower, 98%' {
setup_command_exists "upower"
setup_upower "98.4567%"
@ -218,7 +220,7 @@ function setup_upower {
assert_output "98"
}
@test 'plugins battery: battery-percentage with upower, 98.5%' {
@test 'lib battery: battery-percentage with upower, 98.5%' {
setup_command_exists "upower"
setup_upower "98.5%"
@ -227,7 +229,7 @@ function setup_upower {
assert_output "98"
}
@test 'plugins battery: battery-percentage with upower, 4%' {
@test 'lib battery: battery-percentage with upower, 4%' {
setup_command_exists "upower"
setup_upower "4.2345%"
@ -236,7 +238,7 @@ function setup_upower {
assert_output "04"
}
@test 'plugins battery: battery-percentage with upower, no output' {
@test 'lib battery: battery-percentage with upower, no output' {
setup_command_exists "upower"
setup_upower ""
@ -260,7 +262,7 @@ function setup_ioreg {
}
}
@test 'plugins battery: battery-percentage with ioreg, 100%' {
@test 'lib battery: battery-percentage with ioreg, 100%' {
setup_command_exists "ioreg"
setup_ioreg "100%"
@ -269,7 +271,7 @@ function setup_ioreg {
assert_output "100"
}
@test 'plugins battery: battery-percentage with ioreg, 98%' {
@test 'lib battery: battery-percentage with ioreg, 98%' {
setup_command_exists "ioreg"
setup_ioreg "98%"
@ -278,7 +280,7 @@ function setup_ioreg {
assert_output "98"
}
@test 'plugins battery: battery-percentage with ioreg, 98.5%' {
@test 'lib battery: battery-percentage with ioreg, 98.5%' {
setup_command_exists "ioreg"
setup_ioreg "98.5%"
@ -287,7 +289,7 @@ function setup_ioreg {
assert_output "98"
}
@test 'plugins battery: battery-percentage with ioreg, 4%' {
@test 'lib battery: battery-percentage with ioreg, 4%' {
setup_command_exists "ioreg"
setup_ioreg "4%"
@ -296,7 +298,7 @@ function setup_ioreg {
assert_output "04"
}
@test 'plugins battery: battery-percentage with ioreg, no status' {
@test 'lib battery: battery-percentage with ioreg, no status' {
setup_command_exists "ioreg"
# Simulate that no battery is present
@ -323,7 +325,7 @@ function setup_WMIC {
}
}
@test 'plugins battery: battery-percentage with WMIC, 100%' {
@test 'lib battery: battery-percentage with WMIC, 100%' {
setup_command_exists "WMIC"
setup_WMIC "100%"
@ -332,7 +334,7 @@ function setup_WMIC {
assert_output "100"
}
@test 'plugins battery: battery-percentage with WMIC, 98%' {
@test 'lib battery: battery-percentage with WMIC, 98%' {
setup_command_exists "WMIC"
setup_WMIC "98%"
@ -341,7 +343,7 @@ function setup_WMIC {
assert_output "98"
}
@test 'plugins battery: battery-percentage with WMIC, 98.5%' {
@test 'lib battery: battery-percentage with WMIC, 98.5%' {
setup_command_exists "WMIC"
setup_WMIC "98.5%"
@ -350,7 +352,7 @@ function setup_WMIC {
assert_output "98"
}
@test 'plugins battery: battery-percentage with WMIC, 4%' {
@test 'lib battery: battery-percentage with WMIC, 4%' {
setup_command_exists "WMIC"
setup_WMIC "4%"
@ -359,7 +361,7 @@ function setup_WMIC {
assert_output "04"
}
@test 'plugins battery: battery-percentage with WMIC, no status' {
@test 'lib battery: battery-percentage with WMIC, no status' {
setup_command_exists "WMIC"
setup_WMIC ""

View File

@ -4,39 +4,26 @@ load ../test_helper
load ../test_helper_libs
load ../../themes/base.theme
@test 'themes base: battery_percentage should not exist' {
run type -a battery_percentage &> /dev/null
assert_failure
}
@test 'themes base: battery_percentage should exist if battery plugin loaded' {
load ../../plugins/available/battery.plugin
run type -t battery_percentage
assert_failure
run type -a battery_percentage &> /dev/null
load "../../lib/battery.bash"
run type -t battery_percentage
assert_success
}
@test 'themes base: battery_char should exist' {
run type -t battery_char
assert_success
assert_line "function"
run battery_char
assert_line -n 0 ""
assert_output "function"
}
@test 'themes base: battery_char should exist if battery plugin loaded' {
unset -f battery_char
load ../../plugins/available/battery.plugin
run type -t battery_percentage
assert_success
assert_line "function"
load ../../themes/base.theme
run type -t battery_char
assert_success
assert_line "function"
assert_output "function"
load "../../lib/battery.bash"
run type -t battery_percentage
assert_success
assert_output "function"
run battery_char
assert_success
@ -45,26 +32,16 @@ load ../../themes/base.theme
assert_output --partial 'THEME_BATTERY_PERCENTAGE_CHECK'
}
@test 'themes base: battery_charge should exist' {
run type -a battery_charge &> /dev/null
assert_success
run battery_charge
assert_success
assert_line -n 0 ""
}
@test 'themes base: battery_charge should exist if battery plugin loaded' {
unset -f battery_charge
load ../../plugins/available/battery.plugin
load ../../themes/base.theme
run type -t battery_charge
assert_failure
run type -a battery_charge &> /dev/null
load "../../lib/battery.bash"
run type -t battery_charge
assert_success
assert_output "function"
run battery_charge
assert_success
run type -a battery_charge
assert_line ' no)'
}

View File

@ -558,23 +558,6 @@ function battery_char {
fi
}
if ! _command_exists battery_charge; then
# if user has installed battery plugin, skip this...
function battery_charge() {
# no op
echo -n
}
fi
# The battery_char function depends on the presence of the battery_percentage function.
# If battery_percentage is not defined, then define battery_char as a no-op.
if ! _command_exists battery_percentage; then
function battery_char() {
# no op
echo -n
}
fi
function aws_profile {
if [[ $AWS_DEFAULT_PROFILE ]]; then
echo -e "${AWS_DEFAULT_PROFILE}"

View File

@ -82,14 +82,6 @@ ${D_BRANCH_COLOR}%b %r ${D_CHANGES_COLOR}%m%u ${D_DEFAULT_COLOR}"
fi
}
# checks if the plugin is installed before calling battery_charge
safe_battery_charge() {
if _command_exists battery_charge ;
then
battery_charge
fi
}
# -------------------------------------------------------------- PROMPT OUTPUT
prompt() {
local LAST_COMMAND_FAILED=$(mitsuhikos_lastcommandfailed)
@ -101,7 +93,7 @@ prompt() {
if [[ "$OSTYPE" = 'linux'* ]]
then
PS1="${TITLEBAR}${SAVE_CURSOR}${MOVE_CURSOR_RIGHTMOST}${MOVE_CURSOR_5_LEFT}
$(safe_battery_charge)${RESTORE_CURSOR}\
$(battery_charge)${RESTORE_CURSOR}\
${D_USER_COLOR}\u ${D_INTERMEDIATE_COLOR}\
at ${D_MACHINE_COLOR}\h ${D_INTERMEDIATE_COLOR}\
in ${D_DIR_COLOR}\w ${D_INTERMEDIATE_COLOR}\
@ -117,7 +109,7 @@ in ${D_DIR_COLOR}\w ${D_INTERMEDIATE_COLOR}\
${LAST_COMMAND_FAILED}\
$(demula_vcprompt)\
$(is_vim_shell)\
$(safe_battery_charge)
$(battery_charge)
${D_INTERMEDIATE_COLOR}$ ${D_DEFAULT_COLOR}"
fi

View File

@ -115,14 +115,6 @@ ${D_BRANCH_COLOR}%b %r ${D_CHANGES_COLOR}%m%u ${D_DEFAULT_COLOR}"
fi
}
# checks if the plugin is installed before calling battery_charge
safe_battery_charge() {
if _command_exists battery_charge ;
then
battery_charge
fi
}
prompt_git() {
local s='';
local branchName='';
@ -185,7 +177,7 @@ prompt() {
then
PS1="${TITLEBAR}
${SAVE_CURSOR}${MOVE_CURSOR_RIGHTMOST}${MOVE_CURSOR_5_LEFT}\
$(safe_battery_charge)${RESTORE_CURSOR}\
$(battery_charge)${RESTORE_CURSOR}\
${D_USER_COLOR}\u ${D_INTERMEDIATE_COLOR}\
at ${D_MACHINE_COLOR}\h ${D_INTERMEDIATE_COLOR}\
in ${D_DIR_COLOR}\w ${D_INTERMEDIATE_COLOR}\
@ -203,7 +195,7 @@ $(prompt_git "$D_INTERMEDIATE_COLOR on $D_GIT_COLOR")\
${LAST_COMMAND_FAILED}\
$(demula_vcprompt)\
$(is_vim_shell)\
$(safe_battery_charge)
$(battery_charge)
${D_INTERMEDIATE_COLOR}$ ${D_DEFAULT_COLOR}"
fi