From 8ef853cf578b41d3a27e58cd673f7b8c09d6287c Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 8 Feb 2022 16:17:46 -0800 Subject: [PATCH 1/3] lib/battery: rename `plugin/battery` This plugin *only* provides utility functions, so it has zero cost to just being enabled. This allows us to eliminate assumptions from `lib/theme` and several themes. --- clean_files.txt | 2 +- .../battery.plugin.bash => lib/battery.bash | 0 .../battery.plugin.bats => lib/battery.bats} | 0 themes/base.theme.bash | 17 ----------------- themes/demula/demula.theme.bash | 12 ++---------- themes/rana/rana.theme.bash | 12 ++---------- 6 files changed, 5 insertions(+), 38 deletions(-) rename plugins/available/battery.plugin.bash => lib/battery.bash (100%) rename test/{plugins/battery.plugin.bats => lib/battery.bats} (100%) diff --git a/clean_files.txt b/clean_files.txt index 8f9c173a29..79241529ef 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -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 diff --git a/plugins/available/battery.plugin.bash b/lib/battery.bash similarity index 100% rename from plugins/available/battery.plugin.bash rename to lib/battery.bash diff --git a/test/plugins/battery.plugin.bats b/test/lib/battery.bats similarity index 100% rename from test/plugins/battery.plugin.bats rename to test/lib/battery.bats diff --git a/themes/base.theme.bash b/themes/base.theme.bash index a7e999617b..0d4abac80e 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -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}" diff --git a/themes/demula/demula.theme.bash b/themes/demula/demula.theme.bash index 7d8d6cc8fe..17e068cd2c 100644 --- a/themes/demula/demula.theme.bash +++ b/themes/demula/demula.theme.bash @@ -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 diff --git a/themes/rana/rana.theme.bash b/themes/rana/rana.theme.bash index 3da74d6db7..e9703c417e 100644 --- a/themes/rana/rana.theme.bash +++ b/themes/rana/rana.theme.bash @@ -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 From bf5e9476a802b2a044bceecb061b1ebd89b22e40 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 8 Feb 2022 16:25:04 -0800 Subject: [PATCH 2/3] lib/battery: fix tests --- test/lib/battery.bats | 60 +++++++++++++++++++------------------ test/themes/base.theme.bats | 53 ++++++++++---------------------- 2 files changed, 46 insertions(+), 67 deletions(-) diff --git a/test/lib/battery.bats b/test/lib/battery.bats index eac6d02150..a7950e45c8 100644 --- a/test/lib/battery.bats +++ b/test/lib/battery.bats @@ -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 "" diff --git a/test/themes/base.theme.bats b/test/themes/base.theme.bats index 63f25133e6..6599f07234 100644 --- a/test/themes/base.theme.bats +++ b/test/themes/base.theme.bats @@ -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 +@test 'themes base: battery_percentage should exist if battery plugin loaded' { + run type -t battery_percentage assert_failure -} -@test 'themes base: battery_percentage should exist if battery plugin loaded' { - load ../../plugins/available/battery.plugin + load "../../lib/battery.bash" - run type -a battery_percentage &> /dev/null + run type -t battery_percentage assert_success + assert_output "function" } -@test 'themes base: battery_char should exist' { +@test 'themes base: battery_char should exist if battery plugin loaded' { run type -t battery_char assert_success - assert_line "function" + assert_output "function" - run battery_char - assert_line -n 0 "" -} - -@test 'themes base: battery_char should exist if battery plugin loaded' { - unset -f battery_char - - load ../../plugins/available/battery.plugin + load "../../lib/battery.bash" 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" 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)' } From 357a5725bf3878cf6f99346967bb6e10efcd2ad4 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 8 Feb 2022 16:45:16 -0800 Subject: [PATCH 3/3] lib/battery: add stub file - Avoid an error for users who had the battery plugin enabled. --- plugins/available/battery.plugin.bash | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/available/battery.plugin.bash diff --git a/plugins/available/battery.plugin.bash b/plugins/available/battery.plugin.bash new file mode 100644 index 0000000000..1663cfeac7 --- /dev/null +++ b/plugins/available/battery.plugin.bash @@ -0,0 +1,4 @@ +# shellcheck shell=bash +# stub for renamed file + +_disable-plugin battery