]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/lib/termsupport.zsh
...
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / lib / termsupport.zsh
1 # Set terminal window and tab/icon title
2 #
3 # usage: title short_tab_title [long_window_title]
4 #
5 # See: http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#ss3.1
6 # Fully supports screen, iterm, and probably most modern xterm and rxvt
7 # (In screen, only short_tab_title is used)
8 # Limited support for Apple Terminal (Terminal can't set window and tab separately)
9 function title {
10   setopt localoptions nopromptsubst
11
12   # Don't set the title if inside emacs, unless using vterm
13   [[ -n "${INSIDE_EMACS:-}" && "$INSIDE_EMACS" != vterm ]] && return
14
15   # if $2 is unset use $1 as default
16   # if it is set and empty, leave it as is
17   : ${2=$1}
18
19   case "$TERM" in
20     cygwin|xterm*|putty*|rxvt*|konsole*|ansi|mlterm*|alacritty|st*|foot)
21       print -Pn "\e]2;${2:q}\a" # set window name
22       print -Pn "\e]1;${1:q}\a" # set tab name
23       ;;
24     screen*|tmux*)
25       print -Pn "\ek${1:q}\e\\" # set screen hardstatus
26       ;;
27     *)
28       if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
29         print -Pn "\e]2;${2:q}\a" # set window name
30         print -Pn "\e]1;${1:q}\a" # set tab name
31       else
32         # Try to use terminfo to set the title if the feature is available
33         if (( ${+terminfo[fsl]} && ${+terminfo[tsl]} )); then
34           print -Pn "${terminfo[tsl]}$1${terminfo[fsl]}"
35         fi
36       fi
37       ;;
38   esac
39 }
40
41 ZSH_THEME_TERM_TAB_TITLE_IDLE="%15<..<%~%<<" #15 char left truncated PWD
42 ZSH_THEME_TERM_TITLE_IDLE="%n@%m:%~"
43 # Avoid duplication of directory in terminals with independent dir display
44 if [[ "$TERM_PROGRAM" == Apple_Terminal ]]; then
45   ZSH_THEME_TERM_TITLE_IDLE="%n@%m"
46 fi
47
48 # Runs before showing the prompt
49 function omz_termsupport_precmd {
50   [[ "${DISABLE_AUTO_TITLE:-}" != true ]] || return
51   title "$ZSH_THEME_TERM_TAB_TITLE_IDLE" "$ZSH_THEME_TERM_TITLE_IDLE"
52 }
53
54 # Runs before executing the command
55 function omz_termsupport_preexec {
56   [[ "${DISABLE_AUTO_TITLE:-}" != true ]] || return
57
58   emulate -L zsh
59   setopt extended_glob
60
61   # split command into array of arguments
62   local -a cmdargs
63   cmdargs=("${(z)2}")
64   # if running fg, extract the command from the job description
65   if [[ "${cmdargs[1]}" = fg ]]; then
66     # get the job id from the first argument passed to the fg command
67     local job_id jobspec="${cmdargs[2]#%}"
68     # logic based on jobs arguments:
69     # http://zsh.sourceforge.net/Doc/Release/Jobs-_0026-Signals.html#Jobs
70     # https://www.zsh.org/mla/users/2007/msg00704.html
71     case "$jobspec" in
72       <->) # %number argument:
73         # use the same <number> passed as an argument
74         job_id=${jobspec} ;;
75       ""|%|+) # empty, %% or %+ argument:
76         # use the current job, which appears with a + in $jobstates:
77         # suspended:+:5071=suspended (tty output)
78         job_id=${(k)jobstates[(r)*:+:*]} ;;
79       -) # %- argument:
80         # use the previous job, which appears with a - in $jobstates:
81         # suspended:-:6493=suspended (signal)
82         job_id=${(k)jobstates[(r)*:-:*]} ;;
83       [?]*) # %?string argument:
84         # use $jobtexts to match for a job whose command *contains* <string>
85         job_id=${(k)jobtexts[(r)*${(Q)jobspec}*]} ;;
86       *) # %string argument:
87         # use $jobtexts to match for a job whose command *starts with* <string>
88         job_id=${(k)jobtexts[(r)${(Q)jobspec}*]} ;;
89     esac
90
91     # override preexec function arguments with job command
92     if [[ -n "${jobtexts[$job_id]}" ]]; then
93       1="${jobtexts[$job_id]}"
94       2="${jobtexts[$job_id]}"
95     fi
96   fi
97
98   # cmd name only, or if this is sudo or ssh, the next cmd
99   local CMD="${1[(wr)^(*=*|sudo|ssh|mosh|rake|-*)]:gs/%/%%}"
100   local LINE="${2:gs/%/%%}"
101
102   title "$CMD" "%100>...>${LINE}%<<"
103 }
104
105 autoload -Uz add-zsh-hook
106
107 if [[ -z "$INSIDE_EMACS" || "$INSIDE_EMACS" = vterm ]]; then
108   add-zsh-hook precmd omz_termsupport_precmd
109   add-zsh-hook preexec omz_termsupport_preexec
110 fi
111
112 # Keep Apple Terminal.app's current working directory updated
113 # Based on this answer: https://superuser.com/a/315029
114 # With extra fixes to handle multibyte chars and non-UTF-8 locales
115
116 if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
117   # Emits the control sequence to notify Terminal.app of the cwd
118   # Identifies the directory using a file: URI scheme, including
119   # the host name to disambiguate local vs. remote paths.
120   function update_terminalapp_cwd() {
121     emulate -L zsh
122
123     # Percent-encode the host and path names.
124     local URL_HOST URL_PATH
125     URL_HOST="$(omz_urlencode -P $HOST)" || return 1
126     URL_PATH="$(omz_urlencode -P $PWD)" || return 1
127
128     # Undocumented Terminal.app-specific control sequence
129     printf '\e]7;%s\a' "file://$URL_HOST$URL_PATH"
130   }
131
132   # Use a precmd hook instead of a chpwd hook to avoid contaminating output
133   add-zsh-hook precmd update_terminalapp_cwd
134   # Run once to get initial cwd set
135   update_terminalapp_cwd
136 fi