From 2a8d8ba540d70c51c9822f106bead74d496f80eb Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 26 Jan 2022 12:32:10 -0800 Subject: [PATCH 1/5] lib/colors: rename `theme/colors` --- bash_it.sh | 4 ---- themes/colors.theme.bash => lib/colors.bash | 0 test/lib/log.bats | 2 +- test/plugins/base.plugin.bats | 2 +- test/test_helper_libs.bash | 1 + test/themes/base.theme.bats | 1 - 6 files changed, 3 insertions(+), 7 deletions(-) rename themes/colors.theme.bash => lib/colors.bash (100%) diff --git a/bash_it.sh b/bash_it.sh index 527c9f53..03fd0bf5 100755 --- a/bash_it.sh +++ b/bash_it.sh @@ -66,10 +66,6 @@ done # Load theme, if a theme was set if [[ -n "${BASH_IT_THEME}" ]]; then _log_debug "Loading \"${BASH_IT_THEME}\" theme..." - # Load colors and helpers first so they can be used in base theme - BASH_IT_LOG_PREFIX="themes: colors: " - # shellcheck source=./themes/colors.theme.bash - source "${BASH_IT}/themes/colors.theme.bash" BASH_IT_LOG_PREFIX="themes: githelpers: " # shellcheck source=./themes/githelpers.theme.bash source "${BASH_IT}/themes/githelpers.theme.bash" diff --git a/themes/colors.theme.bash b/lib/colors.bash similarity index 100% rename from themes/colors.theme.bash rename to lib/colors.bash diff --git a/test/lib/log.bats b/test/lib/log.bats index 00efbc2d..bd118999 100644 --- a/test/lib/log.bats +++ b/test/lib/log.bats @@ -1,7 +1,7 @@ #!/usr/bin/env bats load ../test_helper -load ../../themes/colors.theme +load ../../lib/colors load ../../lib/log load ../../lib/helpers diff --git a/test/plugins/base.plugin.bats b/test/plugins/base.plugin.bats index f866199d..3d60986b 100755 --- a/test/plugins/base.plugin.bats +++ b/test/plugins/base.plugin.bats @@ -19,7 +19,7 @@ load ../../plugins/available/base.plugin run myip assert_success declare -r mask_ip=$(echo $output | tr -s '[0-9]' '?') - [[ $mask_ip == 'Your public IP is: ?.?.?.?' ]] + [[ $mask_ip == 'Your public IP is:'*'?.?.?.?'* ]] } @test 'plugins base: pickfrom()' { diff --git a/test/test_helper_libs.bash b/test/test_helper_libs.bash index cc585fad..fac2a9eb 100644 --- a/test/test_helper_libs.bash +++ b/test/test_helper_libs.bash @@ -5,3 +5,4 @@ load "${BASH_IT}/lib/utilities.bash" load "${BASH_IT}/lib/helpers.bash" load "${BASH_IT}/lib/search.bash" load "${BASH_IT}/lib/preexec.bash" +load "${BASH_IT}/lib/colors.bash" diff --git a/test/themes/base.theme.bats b/test/themes/base.theme.bats index 50098c1d..63f25133 100644 --- a/test/themes/base.theme.bats +++ b/test/themes/base.theme.bats @@ -2,7 +2,6 @@ load ../test_helper load ../test_helper_libs -load ../../themes/colors.theme load ../../themes/base.theme @test 'themes base: battery_percentage should not exist' { From 16cee1956d62decee5b9c6d6c9ae5819d7f667b1 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 25 Jan 2022 14:41:00 -0800 Subject: [PATCH 2/5] lib/colors: revert #99 This reverts bash-it/bash-it#99, a metaprogramming adventure in terminal color code escape computation. It was functionally reverted in bash-it/bash-it#699; I'm just finishing the job. --- clean_files.txt | 1 + lib/colors.bash | 185 +----------------------------------------- test/lib/helpers.bats | 2 +- 3 files changed, 4 insertions(+), 184 deletions(-) mode change 100644 => 100755 test/lib/helpers.bats diff --git a/clean_files.txt b/clean_files.txt index 06b19f5d..5f4029fc 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -80,6 +80,7 @@ completion/available/vuejs.completion.bash completion/available/wpscan.completion.bash # libraries +lib/colors.bash lib/helpers.bash lib/log.bash lib/preexec.bash diff --git a/lib/colors.bash b/lib/colors.bash index d5044d05..e96bba92 100644 --- a/lib/colors.bash +++ b/lib/colors.bash @@ -1,188 +1,7 @@ # shellcheck shell=bash -# shellcheck disable=SC2005 # shellcheck disable=SC2034 - -function __ { - echo "$@" -} - -function __make_ansi { - next=$1; shift - echo "\[\e[$(__$next $@)m\]" -} - -function __make_echo { - next=$1; shift - echo "\033[$(__$next $@)m" -} - - -function __reset { - next=$1; shift - out="$(__$next $@)" - echo "0${out:+;${out}}" -} - -function __bold { - next=$1; shift - out="$(__$next $@)" - echo "${out:+${out};}1" -} - -function __faint { - next=$1; shift - out="$(__$next $@)" - echo "${out:+${out};}2" -} - -function __italic { - next=$1; shift - out="$(__$next $@)" - echo "${out:+${out};}3" -} - -function __underline { - next=$1; shift - out="$(__$next $@)" - echo "${out:+${out};}4" -} - -function __negative { - next=$1; shift - out="$(__$next $@)" - echo "${out:+${out};}7" -} - -function __crossed { - next=$1; shift - out="$(__$next $@)" - echo "${out:+${out};}8" -} - - -function __color_normal_fg { - echo "3$1" -} - -function __color_normal_bg { - echo "4$1" -} - -function __color_bright_fg { - echo "9$1" -} - -function __color_bright_bg { - echo "10$1" -} - - -function __color_black { - echo "0" -} - -function __color_red { - echo "1" -} - -function __color_green { - echo "2" -} - -function __color_yellow { - echo "3" -} - -function __color_blue { - echo "4" -} - -function __color_magenta { - echo "5" -} - -function __color_cyan { - echo "6" -} - -function __color_white { - echo "7" -} - -function __color_rgb { - r=$1 && g=$2 && b=$3 - [[ $r == $g && $g == $b ]] && echo $(( $r / 11 + 232 )) && return # gray range above 232 - echo "8;5;$(( ($r * 36 + $b * 6 + $g) / 51 + 16 ))" -} - -function __color { - color=$1; shift - case "$1" in - fg|bg) side="$1"; shift ;; - *) side=fg;; - esac - case "$1" in - normal|bright) mode="$1"; shift;; - *) mode=normal;; - esac - [[ $color == "rgb" ]] && rgb="$1 $2 $3"; shift 3 - - next=$1; shift - out="$(__$next $@)" - echo "$(__color_${mode}_${side} $(__color_${color} $rgb))${out:+;${out}}" -} - - -function __black { - echo "$(__color black $@)" -} - -function __red { - echo "$(__color red $@)" -} - -function __green { - echo "$(__color green $@)" -} - -function __yellow { - echo "$(__color yellow $@)" -} - -function __blue { - echo "$(__color blue $@)" -} - -function __magenta { - echo "$(__color magenta $@)" -} - -function __cyan { - echo "$(__color cyan $@)" -} - -function __white { - echo "$(__color white $@)" -} - -function __rgb { - echo "$(__color rgb $@)" -} - - -function __color_parse { - next=$1; shift - echo "$(__$next $@)" -} - -function color { - echo "$(__color_parse make_ansi $@)" -} - -function echo_color { - echo "$(__color_parse make_echo $@)" -} - +# +# A set of pre-defined color escape codes for use in prompts and with `echo`. black="\[\e[0;30m\]" red="\[\e[0;31m\]" diff --git a/test/lib/helpers.bats b/test/lib/helpers.bats old mode 100644 new mode 100755 index 2386b4b1..bd339d04 --- a/test/lib/helpers.bats +++ b/test/lib/helpers.bats @@ -3,7 +3,7 @@ load ../test_helper load ../test_helper_libs load ../../plugins/available/base.plugin -load ../../themes/colors.theme +load ../../lib/colors function local_setup { setup_test_fixture From 5f19de8d518f55e5790a249b36346a281c4882ad Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 5 Jan 2022 12:40:15 -0800 Subject: [PATCH 3/5] plugin/colors: rename `theme/colors` --- themes/colors.theme.bash => plugins/available/colors.plugin.bash | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename themes/colors.theme.bash => plugins/available/colors.plugin.bash (100%) diff --git a/themes/colors.theme.bash b/plugins/available/colors.plugin.bash similarity index 100% rename from themes/colors.theme.bash rename to plugins/available/colors.plugin.bash From 399f0265aa46113de5670344ad3bab1cb887db61 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 5 Jan 2022 12:49:21 -0800 Subject: [PATCH 4/5] plugin/colors: revert #699 This reverts commit 2a3fde2b14aa6162c3474e84f5f7ac1bd7dcd1b5 but does *not* restore the previous variables. Those are still provided by `lib/colors`. This plugin exists for anyone who likes the metaprogramming adventure of computing colors dynamically rather than using hard-coded value. Potentially this could be used by themes, or possibly by a theme color-scheme randomizer? --- plugins/available/colors.plugin.bash | 89 ---------------------------- 1 file changed, 89 deletions(-) diff --git a/plugins/available/colors.plugin.bash b/plugins/available/colors.plugin.bash index d5044d05..04550a1f 100644 --- a/plugins/available/colors.plugin.bash +++ b/plugins/available/colors.plugin.bash @@ -1,6 +1,5 @@ # shellcheck shell=bash # shellcheck disable=SC2005 -# shellcheck disable=SC2034 function __ { echo "$@" @@ -182,91 +181,3 @@ function color { function echo_color { echo "$(__color_parse make_echo $@)" } - - -black="\[\e[0;30m\]" -red="\[\e[0;31m\]" -green="\[\e[0;32m\]" -yellow="\[\e[0;33m\]" -blue="\[\e[0;34m\]" -purple="\[\e[0;35m\]" -cyan="\[\e[0;36m\]" -white="\[\e[0;37m\]" -orange="\[\e[0;91m\]" - -bold_black="\[\e[30;1m\]" -bold_red="\[\e[31;1m\]" -bold_green="\[\e[32;1m\]" -bold_yellow="\[\e[33;1m\]" -bold_blue="\[\e[34;1m\]" -bold_purple="\[\e[35;1m\]" -bold_cyan="\[\e[36;1m\]" -bold_white="\[\e[37;1m\]" -bold_orange="\[\e[91;1m\]" - -underline_black="\[\e[30;4m\]" -underline_red="\[\e[31;4m\]" -underline_green="\[\e[32;4m\]" -underline_yellow="\[\e[33;4m\]" -underline_blue="\[\e[34;4m\]" -underline_purple="\[\e[35;4m\]" -underline_cyan="\[\e[36;4m\]" -underline_white="\[\e[37;4m\]" -underline_orange="\[\e[91;4m\]" - -background_black="\[\e[40m\]" -background_red="\[\e[41m\]" -background_green="\[\e[42m\]" -background_yellow="\[\e[43m\]" -background_blue="\[\e[44m\]" -background_purple="\[\e[45m\]" -background_cyan="\[\e[46m\]" -background_white="\[\e[47;1m\]" -background_orange="\[\e[101m\]" - -normal="\[\e[0m\]" -reset_color="\[\e[39m\]" - -# These colors are meant to be used with `echo -e` -echo_black="\033[0;30m" -echo_red="\033[0;31m" -echo_green="\033[0;32m" -echo_yellow="\033[0;33m" -echo_blue="\033[0;34m" -echo_purple="\033[0;35m" -echo_cyan="\033[0;36m" -echo_white="\033[0;37;1m" -echo_orange="\033[0;91m" - -echo_bold_black="\033[30;1m" -echo_bold_red="\033[31;1m" -echo_bold_green="\033[32;1m" -echo_bold_yellow="\033[33;1m" -echo_bold_blue="\033[34;1m" -echo_bold_purple="\033[35;1m" -echo_bold_cyan="\033[36;1m" -echo_bold_white="\033[37;1m" -echo_bold_orange="\033[91;1m" - -echo_underline_black="\033[30;4m" -echo_underline_red="\033[31;4m" -echo_underline_green="\033[32;4m" -echo_underline_yellow="\033[33;4m" -echo_underline_blue="\033[34;4m" -echo_underline_purple="\033[35;4m" -echo_underline_cyan="\033[36;4m" -echo_underline_white="\033[37;4m" -echo_underline_orange="\033[91;4m" - -echo_background_black="\033[40m" -echo_background_red="\033[41m" -echo_background_green="\033[42m" -echo_background_yellow="\033[43m" -echo_background_blue="\033[44m" -echo_background_purple="\033[45m" -echo_background_cyan="\033[46m" -echo_background_white="\033[47;1m" -echo_background_orange="\033[101m" - -echo_normal="\033[0m" -echo_reset_color="\033[39m" From bf4ddf59513e5cb151a18776bd4575c50e1b8be6 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Wed, 5 Jan 2022 13:40:49 -0800 Subject: [PATCH 5/5] plugin/colors: `shfmt` --- clean_files.txt | 1 + plugins/available/colors.plugin.bash | 230 ++++++++++++++------------- 2 files changed, 123 insertions(+), 108 deletions(-) diff --git a/clean_files.txt b/clean_files.txt index 06b19f5d..e1933315 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -94,6 +94,7 @@ 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 plugins/available/direnv.plugin.bash plugins/available/dirs.plugin.bash plugins/available/docker-machine.plugin.bash diff --git a/plugins/available/colors.plugin.bash b/plugins/available/colors.plugin.bash index 04550a1f..47f55609 100644 --- a/plugins/available/colors.plugin.bash +++ b/plugins/available/colors.plugin.bash @@ -1,183 +1,197 @@ # shellcheck shell=bash # shellcheck disable=SC2005 -function __ { - echo "$@" +function __() { + echo "$@" } -function __make_ansi { - next=$1; shift - echo "\[\e[$(__$next $@)m\]" +function __make_ansi() { + next=$1 + shift + echo "\[\e[$("__$next" "$@")m\]" } -function __make_echo { - next=$1; shift - echo "\033[$(__$next $@)m" +function __make_echo() { + next=$1 + shift + echo "\033[$("__$next" "$@")m" } - -function __reset { - next=$1; shift - out="$(__$next $@)" - echo "0${out:+;${out}}" +function __reset() { + next=$1 + shift + out="$("__$next" "$@")" + echo "0${out:+;${out}}" } -function __bold { - next=$1; shift - out="$(__$next $@)" - echo "${out:+${out};}1" +function __bold() { + next=$1 + shift + out="$("__$next" "$@")" + echo "${out:+${out};}1" } -function __faint { - next=$1; shift - out="$(__$next $@)" - echo "${out:+${out};}2" +function __faint() { + next=$1 + shift + out="$("__$next" "$@")" + echo "${out:+${out};}2" } -function __italic { - next=$1; shift - out="$(__$next $@)" - echo "${out:+${out};}3" +function __italic() { + next=$1 + shift + out="$("__$next" "$@")" + echo "${out:+${out};}3" } -function __underline { - next=$1; shift - out="$(__$next $@)" - echo "${out:+${out};}4" +function __underline() { + next=$1 + shift + out="$("__$next" "$@")" + echo "${out:+${out};}4" } -function __negative { - next=$1; shift - out="$(__$next $@)" - echo "${out:+${out};}7" +function __negative() { + next=$1 + shift + out="$("__$next" "$@")" + echo "${out:+${out};}7" } -function __crossed { - next=$1; shift - out="$(__$next $@)" - echo "${out:+${out};}8" +function __crossed() { + next=$1 + shift + out="$("__$next" "$@")" + echo "${out:+${out};}8" } - -function __color_normal_fg { - echo "3$1" +function __color_normal_fg() { + echo "3$1" } -function __color_normal_bg { - echo "4$1" +function __color_normal_bg() { + echo "4$1" } -function __color_bright_fg { - echo "9$1" +function __color_bright_fg() { + echo "9$1" } -function __color_bright_bg { - echo "10$1" +function __color_bright_bg() { + echo "10$1" } - -function __color_black { - echo "0" +function __color_black() { + echo "0" } -function __color_red { - echo "1" +function __color_red() { + echo "1" } -function __color_green { - echo "2" +function __color_green() { + echo "2" } -function __color_yellow { - echo "3" +function __color_yellow() { + echo "3" } -function __color_blue { - echo "4" +function __color_blue() { + echo "4" } -function __color_magenta { - echo "5" +function __color_magenta() { + echo "5" } -function __color_cyan { - echo "6" +function __color_cyan() { + echo "6" } -function __color_white { - echo "7" +function __color_white() { + echo "7" } -function __color_rgb { - r=$1 && g=$2 && b=$3 - [[ $r == $g && $g == $b ]] && echo $(( $r / 11 + 232 )) && return # gray range above 232 - echo "8;5;$(( ($r * 36 + $b * 6 + $g) / 51 + 16 ))" +function __color_rgb() { + r=$1 && g=$2 && b=$3 + [[ $r == "$g" && $g == "$b" ]] && echo $((r / 11 + 232)) && return # gray range above 232 + echo "8;5;$(((r * 36 + b * 6 + g) / 51 + 16))" } -function __color { - color=$1; shift - case "$1" in - fg|bg) side="$1"; shift ;; - *) side=fg;; - esac - case "$1" in - normal|bright) mode="$1"; shift;; - *) mode=normal;; - esac - [[ $color == "rgb" ]] && rgb="$1 $2 $3"; shift 3 +function __color() { + color="$1" + shift + case "$1" in + fg | bg) + side="$1" + shift + ;; + *) side="fg" ;; + esac + case "$1" in + normal | bright) + mode="$1" + shift + ;; + *) mode=normal ;; + esac + [[ $color == "rgb" ]] && rgb="$1 $2 $3" + shift 3 - next=$1; shift - out="$(__$next $@)" - echo "$(__color_${mode}_${side} $(__color_${color} $rgb))${out:+;${out}}" + next=$1 + shift + out="$("__$next" "$@")" + echo "$("__color_${mode}_${side}" "$("__color_${color}" "$rgb")")${out:+;${out}}" } - -function __black { - echo "$(__color black $@)" +function __black() { + echo "$(__color black "$@")" } -function __red { - echo "$(__color red $@)" +function __red() { + echo "$(__color red "$@")" } -function __green { - echo "$(__color green $@)" +function __green() { + echo "$(__color green "$@")" } -function __yellow { - echo "$(__color yellow $@)" +function __yellow() { + echo "$(__color yellow "$@")" } -function __blue { - echo "$(__color blue $@)" +function __blue() { + echo "$(__color blue "$@")" } -function __magenta { - echo "$(__color magenta $@)" +function __magenta() { + echo "$(__color magenta "$@")" } -function __cyan { - echo "$(__color cyan $@)" +function __cyan() { + echo "$(__color cyan "$@")" } -function __white { - echo "$(__color white $@)" +function __white() { + echo "$(__color white "$@")" } -function __rgb { - echo "$(__color rgb $@)" +function __rgb() { + echo "$(__color rgb "$@")" } - -function __color_parse { - next=$1; shift - echo "$(__$next $@)" +function __color_parse() { + next=$1 + shift + echo "$("__$next" "$@")" } -function color { - echo "$(__color_parse make_ansi $@)" +function color() { + echo "$(__color_parse make_ansi "$@")" } -function echo_color { - echo "$(__color_parse make_echo $@)" +function echo_color() { + echo "$(__color_parse make_echo "$@")" }