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