plugin/colors: `shfmt`

pull/2019/head
John D Pell 2022-01-05 13:40:49 -08:00 committed by John D Pell
parent 399f0265aa
commit bf4ddf5951
2 changed files with 123 additions and 108 deletions

View File

@ -94,6 +94,7 @@ plugins/available/basher.plugin.bash
plugins/available/battery.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/colors.plugin.bash
plugins/available/direnv.plugin.bash plugins/available/direnv.plugin.bash
plugins/available/dirs.plugin.bash plugins/available/dirs.plugin.bash
plugins/available/docker-machine.plugin.bash plugins/available/docker-machine.plugin.bash

View File

@ -1,183 +1,197 @@
# shellcheck shell=bash # shellcheck shell=bash
# shellcheck disable=SC2005 # shellcheck disable=SC2005
function __ { function __() {
echo "$@" echo "$@"
} }
function __make_ansi { function __make_ansi() {
next=$1; shift next=$1
echo "\[\e[$(__$next $@)m\]" shift
echo "\[\e[$("__$next" "$@")m\]"
} }
function __make_echo { function __make_echo() {
next=$1; shift next=$1
echo "\033[$(__$next $@)m" shift
echo "\033[$("__$next" "$@")m"
} }
function __reset() {
function __reset { next=$1
next=$1; shift shift
out="$(__$next $@)" out="$("__$next" "$@")"
echo "0${out:+;${out}}" echo "0${out:+;${out}}"
} }
function __bold { function __bold() {
next=$1; shift next=$1
out="$(__$next $@)" shift
echo "${out:+${out};}1" out="$("__$next" "$@")"
echo "${out:+${out};}1"
} }
function __faint { function __faint() {
next=$1; shift next=$1
out="$(__$next $@)" shift
echo "${out:+${out};}2" out="$("__$next" "$@")"
echo "${out:+${out};}2"
} }
function __italic { function __italic() {
next=$1; shift next=$1
out="$(__$next $@)" shift
echo "${out:+${out};}3" out="$("__$next" "$@")"
echo "${out:+${out};}3"
} }
function __underline { function __underline() {
next=$1; shift next=$1
out="$(__$next $@)" shift
echo "${out:+${out};}4" out="$("__$next" "$@")"
echo "${out:+${out};}4"
} }
function __negative { function __negative() {
next=$1; shift next=$1
out="$(__$next $@)" shift
echo "${out:+${out};}7" out="$("__$next" "$@")"
echo "${out:+${out};}7"
} }
function __crossed { function __crossed() {
next=$1; shift next=$1
out="$(__$next $@)" shift
echo "${out:+${out};}8" out="$("__$next" "$@")"
echo "${out:+${out};}8"
} }
function __color_normal_fg() {
function __color_normal_fg { echo "3$1"
echo "3$1"
} }
function __color_normal_bg { function __color_normal_bg() {
echo "4$1" echo "4$1"
} }
function __color_bright_fg { function __color_bright_fg() {
echo "9$1" echo "9$1"
} }
function __color_bright_bg { function __color_bright_bg() {
echo "10$1" echo "10$1"
} }
function __color_black() {
function __color_black { echo "0"
echo "0"
} }
function __color_red { function __color_red() {
echo "1" echo "1"
} }
function __color_green { function __color_green() {
echo "2" echo "2"
} }
function __color_yellow { function __color_yellow() {
echo "3" echo "3"
} }
function __color_blue { function __color_blue() {
echo "4" echo "4"
} }
function __color_magenta { function __color_magenta() {
echo "5" echo "5"
} }
function __color_cyan { function __color_cyan() {
echo "6" echo "6"
} }
function __color_white { function __color_white() {
echo "7" echo "7"
} }
function __color_rgb { function __color_rgb() {
r=$1 && g=$2 && b=$3 r=$1 && g=$2 && b=$3
[[ $r == $g && $g == $b ]] && echo $(( $r / 11 + 232 )) && return # gray range above 232 [[ $r == "$g" && $g == "$b" ]] && echo $((r / 11 + 232)) && return # gray range above 232
echo "8;5;$(( ($r * 36 + $b * 6 + $g) / 51 + 16 ))" echo "8;5;$(((r * 36 + b * 6 + g) / 51 + 16))"
} }
function __color { function __color() {
color=$1; shift color="$1"
case "$1" in shift
fg|bg) side="$1"; shift ;; case "$1" in
*) side=fg;; fg | bg)
esac side="$1"
case "$1" in shift
normal|bright) mode="$1"; shift;; ;;
*) mode=normal;; *) side="fg" ;;
esac esac
[[ $color == "rgb" ]] && rgb="$1 $2 $3"; shift 3 case "$1" in
normal | bright)
mode="$1"
shift
;;
*) mode=normal ;;
esac
[[ $color == "rgb" ]] && rgb="$1 $2 $3"
shift 3
next=$1; shift next=$1
out="$(__$next $@)" shift
echo "$(__color_${mode}_${side} $(__color_${color} $rgb))${out:+;${out}}" out="$("__$next" "$@")"
echo "$("__color_${mode}_${side}" "$("__color_${color}" "$rgb")")${out:+;${out}}"
} }
function __black() {
function __black { echo "$(__color black "$@")"
echo "$(__color black $@)"
} }
function __red { function __red() {
echo "$(__color red $@)" echo "$(__color red "$@")"
} }
function __green { function __green() {
echo "$(__color green $@)" echo "$(__color green "$@")"
} }
function __yellow { function __yellow() {
echo "$(__color yellow $@)" echo "$(__color yellow "$@")"
} }
function __blue { function __blue() {
echo "$(__color blue $@)" echo "$(__color blue "$@")"
} }
function __magenta { function __magenta() {
echo "$(__color magenta $@)" echo "$(__color magenta "$@")"
} }
function __cyan { function __cyan() {
echo "$(__color cyan $@)" echo "$(__color cyan "$@")"
} }
function __white { function __white() {
echo "$(__color white $@)" echo "$(__color white "$@")"
} }
function __rgb { function __rgb() {
echo "$(__color rgb $@)" echo "$(__color rgb "$@")"
} }
function __color_parse() {
function __color_parse { next=$1
next=$1; shift shift
echo "$(__$next $@)" echo "$("__$next" "$@")"
} }
function color { function color() {
echo "$(__color_parse make_ansi $@)" echo "$(__color_parse make_ansi "$@")"
} }
function echo_color { function echo_color() {
echo "$(__color_parse make_echo $@)" echo "$(__color_parse make_echo "$@")"
} }