Rewrite terminal auto-title

This commit is contained in:
Sorin Ionescu 2013-08-26 12:12:44 -04:00
parent f6bd76a90b
commit b98c7469e5
5 changed files with 63 additions and 31 deletions

View file

@ -11,6 +11,8 @@ Functions
- `is-autoloadable` checks if a file can be autoloaded by trying to load it
in a subshell.
- `is-callable` checks if a name is a command, function, or alias.
- `is-terminal-inside-multiplexer` checks if the currently running terminal is
inside a terminal multiplexer.
- `is-true` checks a boolean variable for "true".
- `coalesce` prints the first non-empty string in the arguments array.

View file

@ -21,6 +21,11 @@ function is-true {
[[ -n "$1" && "$1" == (1|[Yy]([Ee][Ss]|)|[Tt]([Rr][Uu][Ee]|)|[Oo]([Nn]|)) ]]
}
# Checks if the currently running terminal is inside a terminal multiplexer.
function is-terminal-inside-multiplexer {
[[ -n "$STY" || -n "$TMUX" || -n "$DVTM" ]]
}
# Prints the first non-empty string in the arguments array.
function coalesce {
for arg in $argv; do

View file

@ -8,10 +8,15 @@ Settings
### Auto-Title
To auto set the terminal window and tab titles with the current command or
To auto set the terminal emulator window and tab titles with the current command
or directory, add the following to *zpreztorc*:
zstyle ':prezto:module:terminal:auto-title' emulator 'yes'
To auto set the terminal multiplexer window title with the current command or
directory, add the following to *zpreztorc*:
zstyle ':prezto:module:terminal' auto-title 'yes'
zstyle ':prezto:module:terminal:auto-title' multiplexer 'yes'
Functions
---------

View file

@ -6,6 +6,9 @@
# Sorin Ionescu <sorin.ionescu@gmail.com>
#
# Load dependencies.
pmodload 'helper'
# Return if requirements are not found.
if [[ "$TERM" == 'dumb' ]]; then
return 1
@ -20,21 +23,23 @@ function set-screen-window-title {
# Sets the terminal window title.
function set-terminal-window-title {
if [[ "$TERM" == ((x|a|ml|dt|E)term*|(u|)rxvt*) ]]; then
if [[ "$TERM" == ((x|a|ml|dt|E)term*|(u|)rxvt*|dvtm*) ]]; then
printf "\e]2;%s\a" ${(V)argv}
fi
}
# Sets the terminal tab title.
function set-terminal-tab-title {
if [[ "$TERM" == ((x|a|ml|dt|E)term*|(u|)rxvt*) ]]; then
if [[ "$TERM" == ((x|a|ml|dt|E)term*|(u|)rxvt*|dvtm*) ]]; then
printf "\e]1;%s\a" ${(V)argv}
fi
}
# Sets the Terminal.app current working directory.
function set-terminal-app-cwd {
printf '\e]7;%s\a' "file://$HOST${PWD// /%20}"
if [[ "$TERM_PROGRAM" == 'Apple_Terminal' ]]; then
printf '\e]7;%s\a' "file://$HOST${${1:-$PWD}// /%20}"
fi
}
# Sets the tab and window titles with a given command.
@ -89,36 +94,49 @@ function set-titles-with-path {
fi
}
# Don't override precmd/preexec; append to hook array.
# Do not override precmd/preexec; append to the hook array.
autoload -Uz add-zsh-hook
if [[ "$TERM_PROGRAM" == 'Apple_Terminal' ]]; then
# Sets the Terminal.app current working directory.
# Set up the Apple Terminal.
if [[ "$TERM_PROGRAM" == 'Apple_Terminal' ]] \
&& (( ${${OSTYPE#darwin}[1,2]} >= 11 )) \
&& ! is-terminal-inside-multiplexer
then
# Sets the Terminal.app current working directory before the prompt is
# displayed.
add-zsh-hook precmd set-terminal-app-cwd
# Unsets the Terminal.app current working directory when a terminal
# multiplexer or remote connection is started since it can no longer be
# updated, and it becomes confusing when the directory displayed in the title
# bar is no longer synchronized with real current working directory.
function unset-terminal-app-cwd {
if [[ "${2[(w)1]:t}" == (screen|tmux|dvtm|ssh|mosh) ]]; then
set-terminal-app-cwd ' '
fi
}
add-zsh-hook preexec unset-terminal-app-cwd
# Do not set the tab and window titles in Terminal.app since it sets the tab
# title to the currently running process by default and the current working
# directory is set separately.
# Do set the tab and window titles inside terminal multiplexers.
if [[ "$TERM" != screen* ]]; then
# directory is set separately, but do set the tab and window titles inside
# terminal multiplexers inside Terminal.app.
return
fi
fi
# Sets the tab and window titles before the prompt is displayed.
function set-titles-precmd {
if zstyle -t ':prezto:module:terminal' auto-title; then
set-titles-with-path
fi
}
add-zsh-hook precmd set-titles-precmd
# Set up non-Apple terminals.
if ( ( ! is-terminal-inside-multiplexer || [[ -n "$DVTM" ]] ) \
&& zstyle -t ':prezto:module:terminal:auto-title' emulator ) \
|| ( is-terminal-inside-multiplexer \
&& zstyle -t ':prezto:module:terminal:auto-title' multiplexer )
then
# Sets the tab and window titles before the prompt is displayed.
add-zsh-hook precmd set-titles-with-path
# Sets the tab and window titles before command execution.
function set-titles-preexec {
if zstyle -t ':prezto:module:terminal' auto-title; then
# Sets the tab and window titles before command execution.
function set-titles-with-command-preexec {
set-titles-with-command "$2"
fi
}
add-zsh-hook preexec set-titles-preexec
}
add-zsh-hook preexec set-titles-with-command-preexec
fi

View file

@ -122,14 +122,16 @@ zstyle ':prezto:module:prompt' theme 'sorin'
# 'builtin' 'bg=blue' \
# 'command' 'bg=blue' \
# 'function' 'bg=blue'
#
#
# Terminal
#
# Auto set the tab and window titles.
zstyle ':prezto:module:terminal' auto-title 'yes'
# Auto set the terminal emulator tab and window titles.
zstyle ':prezto:module:terminal:auto-title' emulator 'yes'
# Auto set the terminal multiplexer window title.
zstyle ':prezto:module:terminal:auto-title' multiplexer 'no'
#
# Tmux