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