Always use color codes instead of named colors

This Code was to check if the color is supported by the Terminal
Emulator. This is not necessary, if we always use the numerical code.
This makes the code much clearer.
pull/22/head
Dominik Ritter 6 years ago
parent beacb0ad35
commit 19235b2359

@ -294,17 +294,8 @@ function termColors() {
function getColor() { function getColor() {
# If Color is not numerical, try to get the color code. # If Color is not numerical, try to get the color code.
if [[ "$1" != <-> ]]; then if [[ "$1" != <-> ]]; then
# named color added to parameter expansion print -P to test if the name exists in terminal
local named="%K{$1}"
# https://misc.flogisoft.com/bash/tip_colors_and_formatting
local default="$'\033'\[49m"
# http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
local quoted=$(printf "%q" $(print -P "$named"))
if [[ $quoted == "$'\033'\[49m" && $1 != "black" ]]; then
# color not found, so try to get the code
1=$(getColorCode $1) 1=$(getColorCode $1)
fi fi
fi
echo -n "$1" echo -n "$1"
} }
@ -321,32 +312,32 @@ function foregroundColor() {
# Get numerical color codes. That way we translate ANSI codes # Get numerical color codes. That way we translate ANSI codes
# into ZSH-Style color codes. # into ZSH-Style color codes.
function getColorCode() { function getColorCode() {
# Check if given value is already numerical # Early exit: Check if given value is already numerical
if [[ "$1" = <-> ]]; then if [[ "$1" = <-> ]]; then
echo -n "$1" echo -n "$1"
return
fi fi
else
local colorName="${1}"
# for testing purposes in terminal # for testing purposes in terminal
if [[ "$1" == "foreground" ]]; then if [[ "${colorName}" == "foreground" ]]; then
# call via `getColorCode foreground` # call via `getColorCode foreground`
for i in "${(k@)__P9K_COLORS}"; do for i in "${(k@)__P9K_COLORS}"; do
print -P "$(foregroundColor $i)$(getColor $i) - $i%f" print -P "$(foregroundColor $i)$(getColor $i) - $i%f"
done done
elif [[ "$1" == "background" ]]; then elif [[ "${colorName}" == "background" ]]; then
# call via `getColorCode background` # call via `getColorCode background`
for i in "${(k@)__P9K_COLORS}"; do for i in "${(k@)__P9K_COLORS}"; do
print -P "$(backgroundColor $i)$(getColor $i) - $i%k" print -P "$(backgroundColor $i)$(getColor $i) - $i%k"
done done
else else
#[[ -n "$1" ]] bg="%K{$1}" || bg="%k"
# Strip eventual "bg-" prefixes # Strip eventual "bg-" prefixes
1=${1#bg-} colorName=${colorName#bg-}
# Strip eventual "fg-" prefixes # Strip eventual "fg-" prefixes
1=${1#fg-} colorName=${colorName#fg-}
# Strip eventual "br" prefixes ("bright" colors) # Strip eventual "br" prefixes ("bright" colors)
1=${1#br} colorName=${colorName#br}
echo -n $__P9K_COLORS[$1] echo -n $__P9K_COLORS[$colorName]
fi
fi fi
} }

Loading…
Cancel
Save