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