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