]> arthur.barton.de Git - ax-zsh.git/blobdiff - core/11_terminal/11_terminal.zshrc
Keep $AXZSH variable in axttyinfo alias
[ax-zsh.git] / core / 11_terminal / 11_terminal.zshrc
index 86974038b9a8644ffb54160a98a1d3537470cdda..3990129368f10b44c800fbb280b9e49f5cfabef8 100644 (file)
@@ -23,8 +23,18 @@ 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"
        read -s -d\[ garbage
@@ -36,10 +46,11 @@ function axzsh_is_widechar_terminal {
 
 # Test for "modern" terminal
 function axzsh_is_modern_terminal {
+       [[ "$TERM" = cygwin ]] && return 0
+       [[ "$TERM" = putty* ]] && return 0
        [[ "$TERM" = screen* ]] && return 0
        [[ "$TERM" = tmux* ]] && return 0
        [[ "$TERM" = xterm* ]] && return 0
-       [[ "$TERM" = cygwin ]] && return 0
        return 1
 }
 
@@ -72,14 +83,14 @@ function axzsh_terminal_set_window_title {
 # Update terminal titles befor echoing the shell prompt
 function axzsh_terminal_title_precmd {
        axzsh_is_modern_terminal || return
-       axzsh_terminal_set_icon_title 'zsh'
+       axzsh_terminal_set_window_title ''
        if [[ "$TERM_PROGRAM" == "Apple_Terminal" && "$TERM" != "screen"* ]]; then
-               axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST"
+               axzsh_terminal_set_icon_title "$LOGNAME@$SHORT_HOST"
                # Update CWD in Terminal.app
                local url=$(echo "file://$HOSTNAME$PWD" | sed -e 's| |%20|g')
                printf '\e]7;%s\a' "$url"
        else
-               axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST:$PWD"
+               axzsh_terminal_set_icon_title "$LOGNAME@$SHORT_HOST:$PWD"
        fi
 }
 
@@ -104,42 +115,96 @@ function axzsh_terminal_title_preexec {
                        printf '\e]7;%s\a' ''
                fi
        fi
-       if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
-               # iTerm.app ...
-               [[ -n "$cmd" ]] && TITLE_ADD=" – $cmd"
-       fi
 
-       axzsh_terminal_set_icon_title "$cmd"
+       if [[ -n "$cmd" ]]; then
+               # Add the command to the title
+               TITLE_ADD=" – $cmd"
+       fi
 
        if [[ -z "$remote" ]]; then
-               axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST$TITLE_ADD"
+               axzsh_terminal_set_icon_title "$LOGNAME@$SHORT_HOST$TITLE_ADD"
        else
-               axzsh_terminal_set_window_title "$1"
+               axzsh_terminal_set_icon_title "$1"
        fi
 }
 
 preexec_functions+=(axzsh_terminal_title_preexec)
 
+alias axttyinfo="zsh \"\$AXZSH/bin/axttyinfo\""
+
 axzsh_is_dumb_terminal && return 0
 
 # Colors
+# See <https://en.wikipedia.org/wiki/ANSI_escape_code#Colors>, for exmaple.
 
 autoload -Uz colors
 colors
 
-# Text effects (FX)
+if axzsh_is_modern_terminal; then
+       fg[default]="\e[39m"
+       bg[default]="\e[49m"
+else
+       fg[default]="\e[37m"
+       bg[default]="\e[47m"
+fi
+
+# Foreground (FG) and background (BG) colors.
+typeset -Ag FG BG
+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
 
-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"
+# Text effects (FX)
+# See <https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters>, for example.
+
+typeset -Ag FX
+FX=(
+       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..$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..$TERM_COLORS}; do
+               print -P -- "$code: $BG[$code]$ZSH_SPECTRUM_TEXT$FX[reset]"
+       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/ :-)