X-Git-Url: https://arthur.barton.de/gitweb/?p=ax-zsh.git;a=blobdiff_plain;f=core%2F11_terminal%2F11_terminal.zshrc;h=a15c1d4137ca54a1dcfed630d5a8efa6b5b8a366;hp=5e2306694fed1f45a6086fb3898cff0f72b41d6c;hb=d9321031384d5202023772a03225bd7df01f3490;hpb=602d2931b1cd78bbfb75a4e1fd8de7e6a8553689 diff --git a/core/11_terminal/11_terminal.zshrc b/core/11_terminal/11_terminal.zshrc index 5e23066..a15c1d4 100644 --- a/core/11_terminal/11_terminal.zshrc +++ b/core/11_terminal/11_terminal.zshrc @@ -21,7 +21,21 @@ function axzsh_is_utf_terminal { } alias isutfenv=axzsh_is_utf_terminal -# Check if terminal supports "wide" characters. +# Get the length of a string when shown on the terminal. The return code of the +# function is the length in "cells". Note: Echo'ing the length to the terminal, +# which looks cleaner at first, doesn't work: this command can't be called with +# its stdin and/or stdout redirected, as it it must be able to interact with the +# terminal (write to and read from it). +function axzsh_get_displayed_length { + echo -ne "$*\033[6n" + read -s -d\[ garbage + read -s -d R pos + echo -ne "\033[1K\r" + return $((${pos#*;} - 1)) +} + +# Check if terminal correctly handles "wide" characters, which means, displays +# them with the correct width (>1). # typeset -g _axzsh_is_widechar_terminal_cache function axzsh_is_widechar_terminal { @@ -36,20 +50,17 @@ 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 - read -s -d R pos - echo -ne "\033[1K\r" - [[ "${pos#*;}" -eq 2 ]] || return 1 - return 0 + axzsh_get_displayed_length "🍀" + [[ $? -eq 2 ]] && return 0 || return 1 } # 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 } @@ -82,14 +93,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 } @@ -114,27 +125,27 @@ 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="nocorrect zsh $AXZSH/bin/axttyinfo" +alias axttyinfo="zsh \"\$AXZSH/bin/axttyinfo\"" axzsh_is_dumb_terminal && return 0 # Colors +# See , for exmaple. autoload -Uz colors colors @@ -149,36 +160,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 }