]> arthur.barton.de Git - ax-zsh.git/blob - core/11_terminal/11_terminal.zshrc
P10k: Read config after enabling instant prompt
[ax-zsh.git] / core / 11_terminal / 11_terminal.zshrc
1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # 11_terminal.zshrc: Initialize terminal settings
3
4 # Fix up TERM. Do this here (and not in zprofile), because terminal emulators
5 # often don't start a new login shell but "only" a new interactive shell.
6
7 # VTE based terminals (like GNOME Terminal) support 256 colors, but old(er)
8 # versions of GNOME Terminal (at least) set TERM=xterm ...
9 [[ "$TERM" = "xterm" && "$VTE_VERSION" != "" ]] && TERM="xterm-256color"
10
11 # Common helper functions
12
13 # Check if terminal supports Unicode.
14 # <https://wiki.grml.org/doku.php?id=utf8>
15 function axzsh_is_utf_terminal {
16         case "$LANG $CHARSET $LANGUAGE" in
17                 (*utf*) return 0 ;;
18                 (*UTF*) return 0 ;;
19                 (*) return 1 ;;
20         esac
21 }
22 alias isutfenv=axzsh_is_utf_terminal
23
24 # Get the length of a string when shown on the terminal. The return code of the
25 # function is the length in "cells". Note: Echo'ing the length to the terminal,
26 # which looks cleaner at first, doesn't work: this command can't be called with
27 # its stdin and/or stdout redirected, as it it must be able to interact with the
28 # terminal (write to and read from it).
29 function axzsh_get_displayed_length {
30         echo -ne "$*\033[6n"
31         read -s -d\[ garbage
32         read -s -d R pos
33         echo -ne "\033[1K\r"
34         return $((${pos#*;} - 1))
35 }
36
37 # Check if terminal correctly handles "wide" characters, which means, displays
38 # them with the correct width (>1).
39 # <https://unix.stackexchange.com/questions/184345/detect-how-much-of-unicode-my-terminal-supports-even-through-screen>
40 typeset -g _axzsh_is_widechar_terminal_cache
41 function axzsh_is_widechar_terminal {
42         if [[ -z "$_axzsh_is_widechar_terminal_cache" ]]; then
43                 # No cached result, call test function ...
44                 _axzsh_is_widechar_terminal
45                 _axzsh_is_widechar_terminal_cache=$?
46         fi
47         return $_axzsh_is_widechar_terminal_cache
48 }
49 function _axzsh_is_widechar_terminal {
50         [[ -t 1 ]] || return 1
51         [[ -z "$AXZSH_PLUGIN_CHECK" ]] || return 1
52         axzsh_is_utf_terminal || return 1
53         axzsh_get_displayed_length "🍀"
54         [[ $? -eq 2 ]] && return 0 || return 1
55 }
56
57 # Test for "modern" terminal
58 function axzsh_is_modern_terminal {
59         [[ "$TERM" = cygwin ]] && return 0
60         [[ "$TERM" = putty* ]] && return 0
61         [[ "$TERM" = screen* ]] && return 0
62         [[ "$TERM" = tmux* ]] && return 0
63         [[ "$TERM" = xterm* ]] && return 0
64         return 1
65 }
66
67 # Test for "dumb" terminal
68 function axzsh_is_dumb_terminal {
69         axzsh_is_modern_terminal && return 1
70         [[ "$TERM" = dumb* ]] && return 0
71         [[ "$TERM" = "vt52" ]] && return 0
72         return 1
73 }
74
75 # Resize terminal window (when possible)
76 function axzsh_resize_terminal {
77         printf '\e[8;%d;%dt' "$2" "$1"
78 }
79
80 # Set terminal title
81
82 # Set terminal "hardstatus" and "icon title"
83 function axzsh_terminal_set_icon_title {
84         [[ "$TERM" == "screen"* ]] && printf '\ek%s\e\\' "$1"
85         printf '\e]1;%s\a' "$1"
86 }
87
88 # Set terminal window title
89 function axzsh_terminal_set_window_title {
90         printf '\e]2;%s\a' "$1"
91 }
92
93 # Update terminal titles befor echoing the shell prompt
94 function axzsh_terminal_title_precmd {
95         axzsh_is_modern_terminal || return
96         axzsh_terminal_set_window_title ''
97         if [[ "$TERM_PROGRAM" == "Apple_Terminal" && "$TERM" != "screen"* ]]; then
98                 axzsh_terminal_set_icon_title "$LOGNAME@$SHORT_HOST"
99                 # Update CWD in Terminal.app
100                 local url=$(echo "file://$HOST$PWD" | sed -e 's| |%20|g')
101                 printf '\e]7;%s\a' "$url"
102         else
103                 axzsh_terminal_set_icon_title "$LOGNAME@$SHORT_HOST:$PWD"
104         fi
105 }
106
107 precmd_functions+=(axzsh_terminal_title_precmd)
108
109 # Update terminal titles befor executing a command
110 function axzsh_terminal_title_preexec {
111         axzsh_is_modern_terminal || return
112
113         local cmd="${1[(w)1]}"
114         local remote=""
115
116         case "$cmd" in
117           "mosh"*|"root"*|"ssh"*|"telnet"*)
118                 remote=1
119                 ;;
120         esac
121         if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
122                 # Apple Terminal.app ...
123                 if [[ -n "$remote" ]]; then
124                         # Reset CWD for remote commands
125                         printf '\e]7;%s\a' ''
126                 fi
127         fi
128
129         if [[ -n "$cmd" ]]; then
130                 # Add the command to the title
131                 TITLE_ADD=" – $cmd"
132         fi
133
134         if [[ -z "$remote" ]]; then
135                 axzsh_terminal_set_icon_title "$LOGNAME@$SHORT_HOST$TITLE_ADD"
136         else
137                 axzsh_terminal_set_icon_title "$1"
138         fi
139 }
140
141 preexec_functions+=(axzsh_terminal_title_preexec)
142
143 alias axttyinfo="zsh \"\$AXZSH/bin/axttyinfo\""
144
145 axzsh_is_dumb_terminal && return 0
146
147 # Colors
148 # See <https://en.wikipedia.org/wiki/ANSI_escape_code#Colors>, for exmaple.
149
150 autoload -Uz colors
151 colors
152
153 if axzsh_is_modern_terminal; then
154         fg[default]="\e[39m"
155         bg[default]="\e[49m"
156 else
157         fg[default]="\e[37m"
158         bg[default]="\e[47m"
159 fi
160
161 # Foreground (FG) and background (BG) colors.
162 typeset -Ag FG BG
163 case "$TERM" in
164         *-256color)
165                 TERM_COLORS=255
166                 for color in {000..$TERM_COLORS}; do
167                         FG[$color]="%{\e[38;5;${color}m%}"
168                         BG[$color]="%{\e[48;5;${color}m%}"
169                 done
170                 ;;
171         *)
172                 TERM_COLORS=15
173                 typeset -i c
174                 for color in {000..$TERM_COLORS}; do
175                         c=$color
176                         if [[ $c -ge 8 ]]; then
177                                 c=$c-8
178                                 p="1;"
179                         fi
180                         FG[$color]="%{\e[${p}3${c}m%}"
181                         BG[$color]="%{\e[${p}4${c}m%}"
182                 done
183                 unset c p
184 esac
185 export TERM_COLORS
186
187 # Text effects (FX)
188 # See <https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters>, for example.
189
190 typeset -Ag FX
191 FX=(
192         reset     "%{\e[0m%}"
193         bold      "%{\e[1m%}"   no-bold      "%{\e[22m%}"
194         italic    "%{\e[3m%}"   no-italic    "%{\e[23m%}"
195         underline "%{\e[4m%}"   no-underline "%{\e[24m%}"
196         blink     "%{\e[5m%}"   no-blink     "%{\e[25m%}"
197         reverse   "%{\e[7m%}"   no-reverse   "%{\e[27m%}"
198 )
199
200 ZSH_SPECTRUM_TEXT=${ZSH_SPECTRUM_TEXT:-The quick brown fox jumps over the lazy dog}
201
202 # Show all 256 foreground colors with color number
203 function spectrum_ls() {
204         for code in {000..$TERM_COLORS}; do
205                 print -P -- "$code: $FG[$code]$ZSH_SPECTRUM_TEXT$FX[reset]"
206         done
207 }
208
209 # Show all 256 background colors with color number
210 function spectrum_bls() {
211         for code in {000..$TERM_COLORS}; do
212                 print -P -- "$code: $BG[$code]$ZSH_SPECTRUM_TEXT$FX[reset]"
213         done
214 }
215
216 # NOTE for FG, BG and FX arrays, and spectrum_ls() and spectrum_bls() functions:
217 # Based on a script to make using 256 colors in zsh less painful, written by
218 # P.C. Shyamshankar <sykora@lucentbeing.com>.
219 # Copied from OhMyZsh https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/spectrum.zsh
220 # which was copied from https://github.com/sykora/etc/blob/master/zsh/functions/spectrum/ :-)