-
Notifications
You must be signed in to change notification settings - Fork 15
/
titles.plugin.zsh
executable file
·49 lines (42 loc) · 1.33 KB
/
titles.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Copyright 2022 Amethyst Reese
# Licensed under the MIT license
#
# Update terminal/tmux window titles based on location/command
function update_title() {
local a
# escape '%' in $1, make nonprintables visible
a=${(V)1//\%/\%\%}
print -nz "%20>...>$a"
read -rz a
# remove newlines
a=${a//$'\n'/}
if [[ -n "$TMUX" ]] && [[ $TERM == screen* || $TERM == tmux* ]]; then
print -n "\ek${(%)a}:${(%)2}\e\\"
elif [[ "$TERM" =~ "screen*" ]]; then
print -n "\ek${(%)a}:${(%)2}\e\\"
elif [[ "$TERM" =~ "xterm*" || "$TERM" =~ "alacritty|wezterm" || "$TERM" =~ "st*" ]]; then
print -n "\e]0;${(%)a}:${(%)2}\a"
elif [[ "$TERM" =~ "^rxvt-unicode.*" ]]; then
printf '\33]2;%s:%s\007' ${(%)a} ${(%)2}
fi
}
# called just before the prompt is printed
function _zsh_title__precmd() {
update_title "zsh" "%20<...<%~"
}
# called just before a command is executed
function _zsh_title__preexec() {
local -a cmd
# Escape '\'
1=${1//\\/\\\\\\\\}
cmd=(${(z)1}) # Re-parse the command line
# Construct a command that will output the desired job number.
case $cmd[1] in
fg) cmd="${(z)jobtexts[${(Q)cmd[2]:-%+}]}" ;;
%*) cmd="${(z)jobtexts[${(Q)cmd[1]:-%+}]}" ;;
esac
update_title "$cmd" "%20<...<%~"
}
autoload -Uz add-zsh-hook
add-zsh-hook precmd _zsh_title__precmd
add-zsh-hook preexec _zsh_title__preexec