Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use oh-my-zsh title() function to support more terminals #307

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions geometry.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,60 @@ geometry::hostcolor() {
echo ${colors[${index}]}
}

# Set terminal window and tab/icon title
#
# usage: title short_tab_title [long_window_title]
#
# See: http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#ss3.1
# Fully supports screen, iterm, and probably most modern xterm and rxvt
# (In screen, only short_tab_title is used)
# Limited support for Apple Terminal (Terminal can't set window and tab separately)

# This is the title() function taken directly from oh-my-zsh
# https://github.com/ohmyzsh/ohmyzsh/blob/c44b99e901d7ef58f60247995152de1b937e2e9c/lib/termsupport.zsh
geometry::echo_title_sequence() {
emulate -L zsh
setopt prompt_subst

[[ "$INSIDE_EMACS" == *term* ]] && return

# if $2 is unset use $1 as default
# if it is set and empty, leave it as is
: ${2=$1}

case "$TERM" in
cygwin|xterm*|putty*|rxvt*|konsole*|ansi|mlterm*|alacritty|st*)
print -Pn "\e]2;${2:q}\a" # set window name
print -Pn "\e]1;${1:q}\a" # set tab name
;;
screen*|tmux*)
print -Pn "\ek${1:q}\e\\" # set screen hardstatus
;;
*)
if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
print -Pn "\e]2;${2:q}\a" # set window name
print -Pn "\e]1;${1:q}\a" # set tab name
else
# Try to use terminfo to set the title
# If the feature is available set title
if [[ -n "$terminfo[fsl]" ]] && [[ -n "$terminfo[tsl]" ]]; then
echoti tsl
print -Pn "$1"
echoti fsl
fi
fi
;;
esac
}

# set cmd title (while command is running)
geometry::set_cmdtitle() {
# Make command title available for optional consumption by geometry_cmd
GEOMETRY_LAST_CMD=$2
local ansiCmdTitle=$(print -P $(geometry::wrap $PWD $GEOMETRY_CMDTITLE))
local cmdTitle=$(deansi "$ansiCmdTitle")

echo -ne "\e]1;$cmdTitle\a"
geometry::echo_title_sequence "$cmdTitle"
}
add-zsh-hook preexec geometry::set_cmdtitle

Expand All @@ -79,7 +125,7 @@ geometry::set_title() {
local ansiTitle=$(print -P $(geometry::wrap $PWD $GEOMETRY_TITLE))
local title=$(deansi "$ansiTitle")

echo -ne "\e]1;$title\a"
geometry::echo_title_sequence "$title"
}
add-zsh-hook precmd geometry::set_title

Expand Down