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 - `is-autoloadable` checks if a file can be autoloaded by trying to load it
in a subshell. in a subshell.
- `is-callable` checks if a name is a command, function, or alias. - `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". - `is-true` checks a boolean variable for "true".
- `coalesce` prints the first non-empty string in the arguments array. - `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]|)) ]] [[ -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. # Prints the first non-empty string in the arguments array.
function coalesce { function coalesce {
for arg in $argv; do for arg in $argv; do

View File

@ -8,10 +8,15 @@ Settings
### Auto-Title ### 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*: directory, add the following to *zpreztorc*:
zstyle ':prezto:module:terminal' auto-title 'yes' zstyle ':prezto:module:terminal:auto-title' multiplexer 'yes'
Functions Functions
--------- ---------

View File

@ -6,6 +6,9 @@
# Sorin Ionescu <sorin.ionescu@gmail.com> # Sorin Ionescu <sorin.ionescu@gmail.com>
# #
# Load dependencies.
pmodload 'helper'
# Return if requirements are not found. # Return if requirements are not found.
if [[ "$TERM" == 'dumb' ]]; then if [[ "$TERM" == 'dumb' ]]; then
return 1 return 1
@ -20,21 +23,23 @@ function set-screen-window-title {
# Sets the terminal window title. # Sets the terminal window title.
function set-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} printf "\e]2;%s\a" ${(V)argv}
fi fi
} }
# Sets the terminal tab title. # Sets the terminal tab title.
function set-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} printf "\e]1;%s\a" ${(V)argv}
fi fi
} }
# Sets the Terminal.app current working directory. # Sets the Terminal.app current working directory.
function set-terminal-app-cwd { 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. # Sets the tab and window titles with a given command.
@ -89,36 +94,49 @@ function set-titles-with-path {
fi 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 autoload -Uz add-zsh-hook
if [[ "$TERM_PROGRAM" == 'Apple_Terminal' ]]; then # Set up the Apple Terminal.
# Sets the Terminal.app current working directory. 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 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 # 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 # title to the currently running process by default and the current working
# directory is set separately. # directory is set separately, but do set the tab and window titles inside
# terminal multiplexers inside Terminal.app.
# Do set the tab and window titles inside terminal multiplexers. return
if [[ "$TERM" != screen* ]]; then
return
fi
fi fi
# Sets the tab and window titles before the prompt is displayed. # Set up non-Apple terminals.
function set-titles-precmd { if ( ( ! is-terminal-inside-multiplexer || [[ -n "$DVTM" ]] ) \
if zstyle -t ':prezto:module:terminal' auto-title; then && zstyle -t ':prezto:module:terminal:auto-title' emulator ) \
set-titles-with-path || ( is-terminal-inside-multiplexer \
fi && zstyle -t ':prezto:module:terminal:auto-title' multiplexer )
} then
add-zsh-hook precmd set-titles-precmd # 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. # Sets the tab and window titles before command execution.
function set-titles-preexec { function set-titles-with-command-preexec {
if zstyle -t ':prezto:module:terminal' auto-title; then set-titles-with-command "$2"
set-titles-with-command "$2" }
fi add-zsh-hook preexec set-titles-with-command-preexec
} fi
add-zsh-hook preexec set-titles-preexec

View File

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