]> arthur.barton.de Git - ax-zsh.git/blob - core/11_terminal/11_terminal.zshrc
11_terminal: Make axzsh_is_widechar_terminal() safer
[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 function axzsh_is_widechar_terminal {
27         [[ -t 1 ]] || return 1
28         axzsh_is_utf_terminal || return 1
29         echo -ne "🍀\033[6n"
30         read -s -d\[ garbage
31         read -s -d R pos
32         echo -ne "\033[1K\r"
33         [[ "${pos#*;}" -eq 2 ]] || return 1
34         return 0
35 }
36
37 # Test for "modern" terminal
38 function axzsh_is_modern_terminal {
39         [[ "$TERM" = screen* ]] && return 0
40         [[ "$TERM" = tmux* ]] && return 0
41         [[ "$TERM" = xterm* ]] && return 0
42         [[ "$TERM" = cygwin ]] && return 0
43         return 1
44 }
45
46 # Test for "dumb" terminal
47 function axzsh_is_dumb_terminal {
48         axzsh_is_modern_terminal && return 1
49         [[ "$TERM" = dumb* ]] && return 0
50         [[ "$TERM" = "vt52" ]] && return 0
51         return 1
52 }
53
54 # Resize terminal window (when possible)
55 function axzsh_resize_terminal {
56         printf '\e[8;%d;%dt' "$2" "$1"
57 }
58
59 # Set terminal title
60
61 # Set terminal "hardstatus" and "icon title"
62 function axzsh_terminal_set_icon_title {
63         [[ "$TERM" == "screen"* ]] && printf '\ek%s\e\\' "$1"
64         printf '\e]1;%s\a' "$1"
65 }
66
67 # Set terminal window title
68 function axzsh_terminal_set_window_title {
69         printf '\e]2;%s\a' "$1"
70 }
71
72 # Update terminal titles befor echoing the shell prompt
73 function axzsh_terminal_title_precmd {
74         axzsh_is_modern_terminal || return
75         axzsh_terminal_set_icon_title 'zsh'
76         if [[ "$TERM_PROGRAM" == "Apple_Terminal" && "$TERM" != "screen"* ]]; then
77                 axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST"
78                 # Update CWD in Terminal.app
79                 local url=$(echo "file://$HOSTNAME$PWD" | sed -e 's| |%20|g')
80                 printf '\e]7;%s\a' "$url"
81         else
82                 axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST:$PWD"
83         fi
84 }
85
86 precmd_functions+=(axzsh_terminal_title_precmd)
87
88 # Update terminal titles befor executing a command
89 function axzsh_terminal_title_preexec {
90         axzsh_is_modern_terminal || return
91
92         local cmd="${1[(w)1]}"
93         local remote=""
94
95         case "$cmd" in
96           "mosh"*|"root"*|"ssh"*|"telnet"*)
97                 remote=1
98                 ;;
99         esac
100         if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
101                 # Apple Terminal.app ...
102                 if [[ -n "$remote" ]]; then
103                         # Reset CWD for remote commands
104                         printf '\e]7;%s\a' ''
105                 fi
106         fi
107         if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
108                 # iTerm.app ...
109                 [[ -n "$cmd" ]] && TITLE_ADD=" – $cmd"
110         fi
111
112         axzsh_terminal_set_icon_title "$cmd"
113
114         if [[ -z "$remote" ]]; then
115                 axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST$TITLE_ADD"
116         else
117                 axzsh_terminal_set_window_title "$1"
118         fi
119 }
120
121 preexec_functions+=(axzsh_terminal_title_preexec)
122
123 axzsh_is_dumb_terminal && return 0
124
125 # Colors
126
127 autoload -Uz colors
128 colors
129
130 # Text effects (FX)
131
132 typeset -Ag fx
133 fx=(
134         reset           "\e[00m"
135         bold            "\e[01m"
136         no-bold         "\e[22m"
137         italic          "\e[03m"
138         no-italic       "\e[23m"
139         underline       "\e[04m"
140         no-underline    "\e[24m"
141         blink           "\e[05m"
142         no-blink        "\e[25m"
143         reverse         "\e[07m"
144         no-reverse      "\e[27m"
145 )