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