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