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