From 81401f8304dc05d44e7e90ce7de1bf0e5bcd7eb8 Mon Sep 17 00:00:00 2001 From: Alexander Barton Date: Mon, 22 Apr 2019 17:45:57 +0200 Subject: [PATCH] 11_terminal: Fix & enhance initialization of FG, BG, FX arrays This includes support for 16 colors as well as 256 colors. Other fixes: - Enhance documentation a little bit. - Don't use raw ESC codes, use \e instead! - Remove unneeded %{...%} escaping in spectrum_XXX() functions. --- core/11_terminal/11_terminal.zshrc | 51 +++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/core/11_terminal/11_terminal.zshrc b/core/11_terminal/11_terminal.zshrc index 5e23066..3a2099e 100644 --- a/core/11_terminal/11_terminal.zshrc +++ b/core/11_terminal/11_terminal.zshrc @@ -46,10 +46,10 @@ function _axzsh_is_widechar_terminal { # Test for "modern" terminal function axzsh_is_modern_terminal { + [[ "$TERM" = cygwin ]] && return 0 [[ "$TERM" = screen* ]] && return 0 [[ "$TERM" = tmux* ]] && return 0 [[ "$TERM" = xterm* ]] && return 0 - [[ "$TERM" = cygwin ]] && return 0 return 1 } @@ -135,6 +135,7 @@ alias axttyinfo="nocorrect zsh $AXZSH/bin/axttyinfo" axzsh_is_dumb_terminal && return 0 # Colors +# See , for exmaple. autoload -Uz colors colors @@ -149,36 +150,56 @@ fi # Foreground (FG) and background (BG) colors. typeset -Ag FG BG -for color in {000..255}; do - FG[$color]="%{[38;5;${color}m%}" - BG[$color]="%{[48;5;${color}m%}" -done +case "$TERM" in + *-256color) + TERM_COLORS=255 + for color in {000..$TERM_COLORS}; do + FG[$color]="%{\e[38;5;${color}m%}" + BG[$color]="%{\e[48;5;${color}m%}" + done + ;; + *) + TERM_COLORS=15 + typeset -i c + for color in {000..$TERM_COLORS}; do + c=$color + if [[ $c -ge 8 ]]; then + c=$c-8 + p="1;" + fi + FG[$color]="%{\e[${p}3${c}m%}" + BG[$color]="%{\e[${p}4${c}m%}" + done + unset c p +esac +export TERM_COLORS # Text effects (FX) +# See , for example. typeset -Ag FX FX=( - reset "%{%}" - bold "%{%}" no-bold "%{%}" - italic "%{%}" no-italic "%{%}" - underline "%{%}" no-underline "%{%}" - blink "%{%}" no-blink "%{%}" - reverse "%{%}" no-reverse "%{%}" + reset "%{\e[0m%}" + bold "%{\e[1m%}" no-bold "%{\e[22m%}" + italic "%{\e[3m%}" no-italic "%{\e[23m%}" + underline "%{\e[4m%}" no-underline "%{\e[24m%}" + blink "%{\e[5m%}" no-blink "%{\e[25m%}" + reverse "%{\e[7m%}" no-reverse "%{\e[27m%}" ) ZSH_SPECTRUM_TEXT=${ZSH_SPECTRUM_TEXT:-The quick brown fox jumps over the lazy dog} # Show all 256 foreground colors with color number function spectrum_ls() { - for code in {000..255}; do - print -P -- "$code: %{$FG[$code]%}$ZSH_SPECTRUM_TEXT%{$reset_color%}" + for code in {000..$TERM_COLORS}; do + print -P -- "$code: $FG[$code]$ZSH_SPECTRUM_TEXT$FX[reset]" done } # Show all 256 background colors with color number function spectrum_bls() { - for code in {000..255}; do - print -P -- "$code: %{$BG[$code]%}$ZSH_SPECTRUM_TEXT%{$reset_color%}" + for code in {000..$TERM_COLORS}; do + print -P -- "$code: $BG[$code]$ZSH_SPECTRUM_TEXT$FX[reset]" done } -- 2.39.2