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