bd9c216fe04a1542913f524cad1719797ce39ba2

Author
Zoltán Reegn <zoltan.reegn@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(iterm2): add shell integration script (#11509)

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/iterm2/README.md b/plugins/iterm2/README.md
  2index 50cdebf5ef1ea343c067a2b2141a356ffa91e3c5..3d11622dfd88a924f4271f467a525e1b64d52f26 100644
  3--- a/plugins/iterm2/README.md
  4+++ b/plugins/iterm2/README.md
  5@@ -2,11 +2,20 @@
  6 
  7 This plugin adds a few functions that are useful when using [iTerm2](https://www.iterm2.com/).
  8 
  9+
 10 To use it, add _iterm2_ to the plugins array of your zshrc file:
 11 ```
 12 plugins=(... iterm2)
 13 ```
 14 
 15+Optionally, the plugin also applies the [Shell Integration Script for iTerm2](https://iterm2.com/documentation-shell-integration.html).
 16+You can enable the integration with zstyle. It's important to add this line 
 17+before the line sourcing oh-my-zsh:
 18+
 19+```
 20+zstyle :omz:plugins:iterm2 shell-integration yes
 21+```
 22+
 23 ## Plugin commands
 24 
 25 * `_iterm2_command <iterm2-command>`
 26@@ -24,6 +33,9 @@ plugins=(... iterm2)
 27 * `iterm2_tab_color_reset`
 28   resets the color of iTerm2's current tab back to default.
 29 
 30+
 31+For shell integration features see the [official documentation](https://iterm2.com/documentation-shell-integration.html).
 32+
 33 ## Contributors
 34 
 35 - [Aviv Rosenberg](https://github.com/avivrosenberg)
 36diff --git a/plugins/iterm2/iterm2.plugin.zsh b/plugins/iterm2/iterm2.plugin.zsh
 37index 9d8e40bf6b8c29fbe0e83203b728415c2281e7ff..d00232a30a27883ced6d245271220b3a5247f94b 100644
 38--- a/plugins/iterm2/iterm2.plugin.zsh
 39+++ b/plugins/iterm2/iterm2.plugin.zsh
 40@@ -7,6 +7,17 @@
 41 # This plugin is only relevant if the terminal is iTerm2 on OSX.
 42 if [[ "$OSTYPE" == darwin* ]] && [[ -n "$ITERM_SESSION_ID" ]] ; then
 43 
 44+  # maybe make it the default in the future and allow opting out?
 45+  if zstyle -t ':omz:plugins:iterm2' shell-integration; then 
 46+    # Handle $0 according to the standard:
 47+    # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
 48+    0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
 49+    0="${${(M)0:#/*}:-$PWD/$0}"
 50+
 51+    # See official docs: https://iterm2.com/documentation-shell-integration.html
 52+    source "${0:A:h}/iterm2_shell_integration.zsh"
 53+  fi
 54+
 55   ###
 56   # Executes an arbitrary iTerm2 command via an escape code sequence.
 57   # See https://iterm2.com/documentation-escape-codes.html for all supported commands.
 58diff --git a/plugins/iterm2/iterm2_shell_integration.zsh b/plugins/iterm2/iterm2_shell_integration.zsh
 59new file mode 100644
 60index 0000000000000000000000000000000000000000..7871dddedb280ec42fca89540fa4d05f2bc9f4e4
 61--- /dev/null
 62+++ b/plugins/iterm2/iterm2_shell_integration.zsh
 63@@ -0,0 +1,178 @@
 64+# This program is free software; you can redistribute it and/or
 65+# modify it under the terms of the GNU General Public License
 66+# as published by the Free Software Foundation; either version 2
 67+# of the License, or (at your option) any later version.
 68+# 
 69+# This program is distributed in the hope that it will be useful,
 70+# but WITHOUT ANY WARRANTY; without even the implied warranty of
 71+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 72+# GNU General Public License for more details.
 73+# 
 74+# You should have received a copy of the GNU General Public License
 75+# along with this program; if not, write to the Free Software
 76+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 77+
 78+if [[ -o interactive ]]; then
 79+  if [ "${ITERM_ENABLE_SHELL_INTEGRATION_WITH_TMUX-}""$TERM" != "tmux-256color" -a "${ITERM_ENABLE_SHELL_INTEGRATION_WITH_TMUX-}""$TERM" != "screen" -a "${ITERM_SHELL_INTEGRATION_INSTALLED-}" = "" -a "$TERM" != linux -a "$TERM" != dumb ]; then
 80+    ITERM_SHELL_INTEGRATION_INSTALLED=Yes
 81+    ITERM2_SHOULD_DECORATE_PROMPT="1"
 82+    # Indicates start of command output. Runs just before command executes.
 83+    iterm2_before_cmd_executes() {
 84+      if [ "$TERM_PROGRAM" = "iTerm.app" ]; then
 85+        printf "\033]133;C;\r\007"
 86+      else
 87+        printf "\033]133;C;\007"
 88+      fi
 89+    }
 90+
 91+    iterm2_set_user_var() {
 92+      printf "\033]1337;SetUserVar=%s=%s\007" "$1" $(printf "%s" "$2" | base64 | tr -d '\n')
 93+    }
 94+
 95+    # Users can write their own version of this method. It should call
 96+    # iterm2_set_user_var but not produce any other output.
 97+    # e.g., iterm2_set_user_var currentDirectory $PWD
 98+    # Accessible in iTerm2 (in a badge now, elsewhere in the future) as
 99+    # \(user.currentDirectory).
100+    whence -v iterm2_print_user_vars > /dev/null 2>&1
101+    if [ $? -ne 0 ]; then
102+      iterm2_print_user_vars() {
103+          true
104+      }
105+    fi
106+
107+    iterm2_print_state_data() {
108+      local _iterm2_hostname="${iterm2_hostname-}"
109+      if [ -z "${iterm2_hostname:-}" ]; then
110+        _iterm2_hostname=$(hostname -f 2>/dev/null)
111+      fi
112+      printf "\033]1337;RemoteHost=%s@%s\007" "$USER" "${_iterm2_hostname-}"
113+      printf "\033]1337;CurrentDir=%s\007" "$PWD"
114+      iterm2_print_user_vars
115+    }
116+
117+    # Report return code of command; runs after command finishes but before prompt
118+    iterm2_after_cmd_executes() {
119+      printf "\033]133;D;%s\007" "$STATUS"
120+      iterm2_print_state_data
121+    }
122+
123+    # Mark start of prompt
124+    iterm2_prompt_mark() {
125+      printf "\033]133;A\007"
126+    }
127+
128+    # Mark end of prompt
129+    iterm2_prompt_end() {
130+      printf "\033]133;B\007"
131+    }
132+
133+    # There are three possible paths in life.
134+    #
135+    # 1) A command is entered at the prompt and you press return.
136+    #    The following steps happen:
137+    #    * iterm2_preexec is invoked
138+    #      * PS1 is set to ITERM2_PRECMD_PS1
139+    #      * ITERM2_SHOULD_DECORATE_PROMPT is set to 1
140+    #    * The command executes (possibly reading or modifying PS1)
141+    #    * iterm2_precmd is invoked
142+    #      * ITERM2_PRECMD_PS1 is set to PS1 (as modified by command execution)
143+    #      * PS1 gets our escape sequences added to it
144+    #    * zsh displays your prompt
145+    #    * You start entering a command
146+    #
147+    # 2) You press ^C while entering a command at the prompt.
148+    #    The following steps happen:
149+    #    * (iterm2_preexec is NOT invoked)
150+    #    * iterm2_precmd is invoked
151+    #      * iterm2_before_cmd_executes is called since we detected that iterm2_preexec was not run
152+    #      * (ITERM2_PRECMD_PS1 and PS1 are not messed with, since PS1 already has our escape
153+    #        sequences and ITERM2_PRECMD_PS1 already has PS1's original value)
154+    #    * zsh displays your prompt
155+    #    * You start entering a command
156+    #
157+    # 3) A new shell is born.
158+    #    * PS1 has some initial value, either zsh's default or a value set before this script is sourced.
159+    #    * iterm2_precmd is invoked
160+    #      * ITERM2_SHOULD_DECORATE_PROMPT is initialized to 1
161+    #      * ITERM2_PRECMD_PS1 is set to the initial value of PS1
162+    #      * PS1 gets our escape sequences added to it
163diff --git a/plugins/iterm2/update b/plugins/iterm2/update
164new file mode 100755
165index 0000000000000000000000000000000000000000..da8dae6902c33798b37045ff844550e1a9f8c613
166--- /dev/null
167+++ b/plugins/iterm2/update
168@@ -0,0 +1,4 @@
169+#!/bin/sh
170+
171+curl -s -L https://iterm2.com/shell_integration/zsh \
172+  -o iterm2_shell_integration.zsh