]> arthur.barton.de Git - ax-zsh.git/blobdiff - core/11_terminal/11_terminal.zshrc
Implement FG, BG and FX arrays, compatible to OhMyZsh & spectrum
[ax-zsh.git] / core / 11_terminal / 11_terminal.zshrc
index 0c7bce0a8bed651aa6d5dd0b6ec9cab9ae5eb838..4259b23af4535ce93f79984d49eaf79afb8f28c2 100644 (file)
@@ -23,12 +23,23 @@ alias isutfenv=axzsh_is_utf_terminal
 
 # Check if terminal supports "wide" characters.
 # <https://unix.stackexchange.com/questions/184345/detect-how-much-of-unicode-my-terminal-supports-even-through-screen>
+typeset -g _axzsh_is_widechar_terminal_cache
 function axzsh_is_widechar_terminal {
+       if [[ -z "$_axzsh_is_widechar_terminal_cache" ]]; then
+               # No cached result, call test function ...
+               _axzsh_is_widechar_terminal
+               _axzsh_is_widechar_terminal_cache=$?
+       fi
+       return $_axzsh_is_widechar_terminal_cache
+}
+function _axzsh_is_widechar_terminal {
        [[ -t 1 ]] || return 1
+       [[ -z "$AXZSH_PLUGIN_CHECK" ]] || return 1
        axzsh_is_utf_terminal || return 1
-       echo -ne "🍀\033[6n\033[1K\r"
+       echo -ne "🍀\033[6n"
        read -s -d\[ garbage
        read -s -d R pos
+       echo -ne "\033[1K\r"
        [[ "${pos#*;}" -eq 2 ]] || return 1
        return 0
 }
@@ -119,6 +130,8 @@ function axzsh_terminal_title_preexec {
 
 preexec_functions+=(axzsh_terminal_title_preexec)
 
+alias axttyinfo="nocorrect zsh $AXZSH/bin/axttyinfo"
+
 axzsh_is_dumb_terminal && return 0
 
 # Colors
@@ -126,19 +139,43 @@ axzsh_is_dumb_terminal && return 0
 autoload -Uz colors
 colors
 
+# Foreground (FG) and background (BG) colors.
+typeset -Ag FG BG
+for color in {000..255}; do
+       FG[$color]="%{\e[38;5;${color}m%}"
+       BG[$color]="%{\e[48;5;${color}m%}"
+done
+
 # Text effects (FX)
 
-typeset -Ag fx
-fx=(
-       reset           "\e[00m"
-       bold            "\e[01m"
-       no-bold         "\e[22m"
-       italic          "\e[03m"
-       no-italic       "\e[23m"
-       underline       "\e[04m"
-       no-underline    "\e[24m"
-       blink           "\e[05m"
-       no-blink        "\e[25m"
-       reverse         "\e[07m"
-       no-reverse      "\e[27m"
+typeset -Ag FX
+FX=(
+       reset     "%{\e[00m%}"
+       bold      "%{\e[01m%}"   no-bold      "%{\e[22m%}"
+       italic    "%{\e[03m%}"   no-italic    "%{\e[23m%}"
+       underline "%{\e[04m%}"   no-underline "%{\e[24m%}"
+       blink     "%{\e[05m%}"   no-blink     "%{\e[25m%}"
+       reverse   "%{\e[07m%}"   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%}"
+       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%}"
+       done
+}
+
+# NOTE for FG, BG and FX arrays, and spectrum_ls() and spectrum_bls() functions:
+# Based on a script to make using 256 colors in zsh less painful, written by
+# P.C. Shyamshankar <sykora@lucentbeing.com>.
+# Copied from OhMyZsh https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/spectrum.zsh
+# which was copied from https://github.com/sykora/etc/blob/master/zsh/functions/spectrum/ :-)