summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2016-12-15 16:45:01 +0100
committerAlan Pearce2016-12-15 16:45:01 +0100
commit10c8548d14e2349ff9bdcf0aa2f26b2e8eadd012 (patch)
treeeb69a3a8724b1c1a44c9ccd9a0be0eb216200008
parent4d67c3a9251ed35fa8a00a589ad688644dd9c6b8 (diff)
downloaddotfiles-10c8548d14e2349ff9bdcf0aa2f26b2e8eadd012.tar.lz
dotfiles-10c8548d14e2349ff9bdcf0aa2f26b2e8eadd012.tar.zst
dotfiles-10c8548d14e2349ff9bdcf0aa2f26b2e8eadd012.zip
zsh: Update macOS Terminal icon with cwd
-rw-r--r--tag-zsh/config/zsh/zshrc40
1 files changed, 40 insertions, 0 deletions
diff --git a/tag-zsh/config/zsh/zshrc b/tag-zsh/config/zsh/zshrc
index d66b591..7b3d332 100644
--- a/tag-zsh/config/zsh/zshrc
+++ b/tag-zsh/config/zsh/zshrc
@@ -177,6 +177,46 @@ else
   setopt prompt_cr
   PROMPT='%F{blue}%~%f${prompt_pure_username}
 %(?.%F{magenta}.%F{red})>%f '
+
+  ## From https://github.com/robbyrussell/oh-my-zsh/blob/71deb74552d54630d99ae1db3647ebed7b3bc735/lib/termsupport.zsh
+
+  # Keep Apple Terminal.app's current working directory updated
+  # Based on this answer: http://superuser.com/a/315029
+  # With extra fixes to handle multibyte chars and non-UTF-8 locales
+
+  if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] && [[ -z "$INSIDE_EMACS" ]]; then
+    # Emits the control sequence to notify Terminal.app of the cwd
+    # Identifies the directory using a file: URI scheme, including
+    # the host name to disambiguate local vs. remote paths.
+    function update_terminalapp_cwd() {
+      emulate -L zsh
+
+      # Percent-encode the pathname.
+      # Percent-encode the pathname.
+      local URL_PATH=''
+      {
+        # Use LC_CTYPE=C to process text byte-by-byte.
+        local i ch hexch LC_CTYPE=C
+        for ((i = 1; i <= ${#PWD}; ++i)); do
+          ch="$PWD[i]"
+          if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
+            URL_PATH+="$ch"
+          else
+            hexch=$(printf "%02X" "'$ch")
+            URL_PATH+="%$hexch"
+          fi
+        done
+      }
+      [[ $? != 0 ]] && return 1
+
+      printf '\e]7;%s\a' "file://$HOST$URL_PATH"
+    }
+
+    # Use a precmd hook instead of a chpwd hook to avoid contaminating output
+    precmd_functions+=(update_terminalapp_cwd)
+    # Run once to get initial cwd set
+    update_terminalapp_cwd
+  fi
 fi
 
 if zplug check clvv/fasd