From 3d55d66d8bfa3772c61da2dc79de6b35bd2bdf25 Mon Sep 17 00:00:00 2001 From: Robby Russell Date: Thu, 3 Dec 2026 05:13:53 -0800 Subject: [PATCH] perf(termsupport): don't re-encode the cwd URL on every prompt (#14070) Co-authored-by: Carlo Sala --- lib/termsupport.zsh | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index c2623887a..4153a1ef3 100644 --- a/lib/termsupport.zsh +++ b/lib/termsupport.zsh @@ -154,16 +154,23 @@ esac # the host name to disambiguate local vs. remote paths. function omz_termsupport_cwd { setopt localoptions unset - # Percent-encode the host and path names. - local URL_HOST URL_PATH - URL_HOST="$(omz_urlencode -P $HOST)" || return 1 - URL_PATH="$(omz_urlencode -P $PWD)" || return 1 + # Percent-encode the host and path names. Encoding forks a subshell each, + # so keep the result and only redo it when its inputs change. + local cache_key="$HOST:$PWD:${langinfo[CODESET]}:${KONSOLE_PROFILE_NAME:+1}:${KONSOLE_DBUS_SESSION:+1}" + if [[ "$_omz_termsupport_cwd_key" != "$cache_key" ]]; then + local URL_HOST URL_PATH + URL_HOST="$(omz_urlencode -P "$HOST")" || return 1 + URL_PATH="$(omz_urlencode -P "$PWD")" || return 1 - # Konsole errors if the HOST is provided - [[ -z "$KONSOLE_PROFILE_NAME" && -z "$KONSOLE_DBUS_SESSION" ]] || URL_HOST="" + # Konsole errors if the HOST is provided + [[ -z "$KONSOLE_PROFILE_NAME" && -z "$KONSOLE_DBUS_SESSION" ]] || URL_HOST="" + + typeset -g _omz_termsupport_cwd_key="$cache_key" + typeset -g _omz_termsupport_cwd_url="file://${URL_HOST}${URL_PATH}" + fi # common control sequence (OSC 7) to set current host and path - printf "\e]7;file://%s%s\e\\" "${URL_HOST}" "${URL_PATH}" + printf "\e]7;%s\e\\" "$_omz_termsupport_cwd_url" } # Use a precmd hook instead of a chpwd hook to avoid contaminating output