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