feat (zsh/term_title): add support for displaying terminal titles in a nice way

This commit is contained in:
eeemsi 2023-06-15 20:29:05 +02:00
parent 9713982a79
commit 50d50ff069
2 changed files with 44 additions and 0 deletions

43
zsh/term_title Normal file
View File

@ -0,0 +1,43 @@
set_termtitle() {
# escape '%' chars in "${1}", make nonprintables visible
local a="${(V)1//\%/\%\%}"
# Truncate command, and join lines.
a="${a//[$'\r'$'\n']/}"
[[ "${a}" = "zsh" ]] && { a="${(%)${:-%~}}" }
case "${TERM}" in
screen)
# plain xterm title
print -rn -- $'\e'"]2;${(%)${:-%m}}: ${a}"$'\a'
# screen title (in ^A")
print -rn -- $'\e'"k${a}"$'\e'$'\\'
# screen location
print -rn -- $'\e'"_${(%)${:-%m}}: ${a}"$'\e'$'\\'
;;
xterm*|rxvt*)
# plain xterm title
print -rn -- $'\e'"]2;${(%)${:-%m}}: ${a}"$'\a'
;;
*)
;;
esac
}
my_prompt_precmd() {
set_termtitle "zsh"
}
my_prompt_preexec() {
set_termtitle "${1}"
}
typeset -ga precmd_functions
precmd_functions+=my_prompt_precmd
typeset -ga preexec_functions
preexec_functions+=my_prompt_preexec

1
zshrc
View File

@ -92,3 +92,4 @@ precmd() {
fi
}
source ~/.zsh/term_title