mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2024-12-19 20:29:45 +01:00
feat(lib): send OSC 7 on directory change for more supported terminals (#9914)
Previously this was only emitted on macOS with Apple's Terminal.app (and compatible clones like iTerm2), but it is used by other terminal emulators as well to obtain the actual current working directory wiht symlinks intact. All non-supporting terminal emulators tested gracefully ignored this value, so emit this on these as well in case they (or some other app masquarading as them) add future support for this value. Closes #9914 Co-authored-by: Marc Cornellà <hello@mcornella.com>
This commit is contained in:
parent
33aadeced0
commit
25c2c3a468
1 changed files with 47 additions and 23 deletions
|
@ -109,28 +109,52 @@ if [[ -z "$INSIDE_EMACS" || "$INSIDE_EMACS" = vterm ]]; then
|
||||||
add-zsh-hook preexec omz_termsupport_preexec
|
add-zsh-hook preexec omz_termsupport_preexec
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Keep Apple Terminal.app's current working directory updated
|
# Keep terminal emulator's current working directory correct,
|
||||||
# Based on this answer: https://superuser.com/a/315029
|
# even if the current working directory path contains symbolic links
|
||||||
# With extra fixes to handle multibyte chars and non-UTF-8 locales
|
#
|
||||||
|
# References:
|
||||||
|
# - Apple's Terminal.app: https://superuser.com/a/315029
|
||||||
|
# - iTerm2: https://iterm2.com/documentation-escape-codes.html (iTerm2 Extension / CurrentDir+RemoteHost)
|
||||||
|
# - Konsole: https://bugs.kde.org/show_bug.cgi?id=327720#c1
|
||||||
|
# - libvte (gnome-terminal, mate-terminal, …): https://bugzilla.gnome.org/show_bug.cgi?id=675987#c14
|
||||||
|
# Apparently it had a bug before ~2012 were it would display the unknown OSC 7 code
|
||||||
|
#
|
||||||
|
# As of May 2021 mlterm, PuTTY, rxvt, screen, termux & xterm simply ignore the unknown OSC.
|
||||||
|
|
||||||
if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
|
# Don't define the function if we're inside Emacs
|
||||||
# Emits the control sequence to notify Terminal.app of the cwd
|
if [[ -n "$INSIDE_EMACS" ]]; then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Don't define the function if we're in an unsupported terminal
|
||||||
|
case "$TERM" in
|
||||||
|
# all of these either process OSC 7 correctly or ignore entirely
|
||||||
|
xterm*|putty*|rxvt*|konsole*|mlterm*|alacritty|screen*|tmux*) ;;
|
||||||
|
contour*|foot*) ;;
|
||||||
|
*)
|
||||||
|
# Terminal.app and iTerm2 process OSC 7 correctly
|
||||||
|
case "$TERM_PROGRAM" in
|
||||||
|
Apple_Terminal|iTerm.app) ;;
|
||||||
|
*) return ;;
|
||||||
|
esac ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Emits the control sequence to notify many terminal emulators
|
||||||
|
# of the cwd
|
||||||
|
#
|
||||||
# Identifies the directory using a file: URI scheme, including
|
# Identifies the directory using a file: URI scheme, including
|
||||||
# the host name to disambiguate local vs. remote paths.
|
# the host name to disambiguate local vs. remote paths.
|
||||||
function update_terminalapp_cwd() {
|
function omz_termsupport_cwd {
|
||||||
emulate -L zsh
|
|
||||||
|
|
||||||
# Percent-encode the host and path names.
|
# Percent-encode the host and path names.
|
||||||
local URL_HOST URL_PATH
|
local URL_HOST URL_PATH
|
||||||
URL_HOST="$(omz_urlencode -P $HOST)" || return 1
|
URL_HOST="$(omz_urlencode -P $HOST)" || return 1
|
||||||
URL_PATH="$(omz_urlencode -P $PWD)" || return 1
|
URL_PATH="$(omz_urlencode -P $PWD)" || return 1
|
||||||
|
|
||||||
# Undocumented Terminal.app-specific control sequence
|
# common control sequence (OSC 7) to set current host and path
|
||||||
printf '\e]7;%s\a' "file://$URL_HOST$URL_PATH"
|
printf "\e]7;%s\a" "file://${URL_HOST}${URL_PATH}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Use a precmd hook instead of a chpwd hook to avoid contaminating output
|
# Use a precmd hook instead of a chpwd hook to avoid contaminating output
|
||||||
add-zsh-hook precmd update_terminalapp_cwd
|
# i.e. when a script or function changes directory without `cd -q`, chpwd
|
||||||
# Run once to get initial cwd set
|
# will be called the output may be swallowed by the script or function.
|
||||||
update_terminalapp_cwd
|
add-zsh-hook precmd omz_termsupport_cwd
|
||||||
fi
|
|
||||||
|
|
Loading…
Reference in a new issue