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