iterm2_shell_integration.zsh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # This program is free software; you can redistribute it and/or
  2. # modify it under the terms of the GNU General Public License
  3. # as published by the Free Software Foundation; either version 2
  4. # of the License, or (at your option) any later version.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program; if not, write to the Free Software
  13. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. if [[ -o interactive ]]; then
  15. 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
  16. ITERM_SHELL_INTEGRATION_INSTALLED=Yes
  17. ITERM2_SHOULD_DECORATE_PROMPT="1"
  18. # Indicates start of command output. Runs just before command executes.
  19. iterm2_before_cmd_executes() {
  20. if [ "$TERM_PROGRAM" = "iTerm.app" ]; then
  21. printf "\033]133;C;\r\007"
  22. else
  23. printf "\033]133;C;\007"
  24. fi
  25. }
  26. iterm2_set_user_var() {
  27. printf "\033]1337;SetUserVar=%s=%s\007" "$1" $(printf "%s" "$2" | base64 | tr -d '\n')
  28. }
  29. # Users can write their own version of this method. It should call
  30. # iterm2_set_user_var but not produce any other output.
  31. # e.g., iterm2_set_user_var currentDirectory $PWD
  32. # Accessible in iTerm2 (in a badge now, elsewhere in the future) as
  33. # \(user.currentDirectory).
  34. whence -v iterm2_print_user_vars > /dev/null 2>&1
  35. if [ $? -ne 0 ]; then
  36. iterm2_print_user_vars() {
  37. true
  38. }
  39. fi
  40. iterm2_print_state_data() {
  41. local _iterm2_hostname="${iterm2_hostname-}"
  42. if [ -z "${iterm2_hostname:-}" ]; then
  43. _iterm2_hostname=$(hostname -f 2>/dev/null)
  44. fi
  45. printf "\033]1337;RemoteHost=%s@%s\007" "$USER" "${_iterm2_hostname-}"
  46. printf "\033]1337;CurrentDir=%s\007" "$PWD"
  47. iterm2_print_user_vars
  48. }
  49. # Report return code of command; runs after command finishes but before prompt
  50. iterm2_after_cmd_executes() {
  51. printf "\033]133;D;%s\007" "$STATUS"
  52. iterm2_print_state_data
  53. }
  54. # Mark start of prompt
  55. iterm2_prompt_mark() {
  56. printf "\033]133;A\007"
  57. }
  58. # Mark end of prompt
  59. iterm2_prompt_end() {
  60. printf "\033]133;B\007"
  61. }
  62. # There are three possible paths in life.
  63. #
  64. # 1) A command is entered at the prompt and you press return.
  65. # The following steps happen:
  66. # * iterm2_preexec is invoked
  67. # * PS1 is set to ITERM2_PRECMD_PS1
  68. # * ITERM2_SHOULD_DECORATE_PROMPT is set to 1
  69. # * The command executes (possibly reading or modifying PS1)
  70. # * iterm2_precmd is invoked
  71. # * ITERM2_PRECMD_PS1 is set to PS1 (as modified by command execution)
  72. # * PS1 gets our escape sequences added to it
  73. # * zsh displays your prompt
  74. # * You start entering a command
  75. #
  76. # 2) You press ^C while entering a command at the prompt.
  77. # The following steps happen:
  78. # * (iterm2_preexec is NOT invoked)
  79. # * iterm2_precmd is invoked
  80. # * iterm2_before_cmd_executes is called since we detected that iterm2_preexec was not run
  81. # * (ITERM2_PRECMD_PS1 and PS1 are not messed with, since PS1 already has our escape
  82. # sequences and ITERM2_PRECMD_PS1 already has PS1's original value)
  83. # * zsh displays your prompt
  84. # * You start entering a command
  85. #
  86. # 3) A new shell is born.
  87. # * PS1 has some initial value, either zsh's default or a value set before this script is sourced.
  88. # * iterm2_precmd is invoked
  89. # * ITERM2_SHOULD_DECORATE_PROMPT is initialized to 1
  90. # * ITERM2_PRECMD_PS1 is set to the initial value of PS1
  91. # * PS1 gets our escape sequences added to it
  92. # * Your prompt is shown and you may begin entering a command.
  93. #
  94. # Invariants:
  95. # * ITERM2_SHOULD_DECORATE_PROMPT is 1 during and just after command execution, and "" while the prompt is
  96. # shown and until you enter a command and press return.
  97. # * PS1 does not have our escape sequences during command execution
  98. # * After the command executes but before a new one begins, PS1 has escape sequences and
  99. # ITERM2_PRECMD_PS1 has PS1's original value.
  100. iterm2_decorate_prompt() {
  101. # This should be a raw PS1 without iTerm2's stuff. It could be changed during command
  102. # execution.
  103. ITERM2_PRECMD_PS1="$PS1"
  104. ITERM2_SHOULD_DECORATE_PROMPT=""
  105. # Add our escape sequences just before the prompt is shown.
  106. # Use ITERM2_SQUELCH_MARK for people who can't mdoify PS1 directly, like powerlevel9k users.
  107. # This is gross but I had a heck of a time writing a correct if statetment for zsh 5.0.2.
  108. local PREFIX=""
  109. if [[ $PS1 == *"$(iterm2_prompt_mark)"* ]]; then
  110. PREFIX=""
  111. elif [[ "${ITERM2_SQUELCH_MARK-}" != "" ]]; then
  112. PREFIX=""
  113. else
  114. PREFIX="%{$(iterm2_prompt_mark)%}"
  115. fi
  116. PS1="$PREFIX$PS1%{$(iterm2_prompt_end)%}"
  117. ITERM2_DECORATED_PS1="$PS1"
  118. }
  119. iterm2_precmd() {
  120. local STATUS="$?"
  121. if [ -z "${ITERM2_SHOULD_DECORATE_PROMPT-}" ]; then
  122. # You pressed ^C while entering a command (iterm2_preexec did not run)
  123. iterm2_before_cmd_executes
  124. if [ "$PS1" != "${ITERM2_DECORATED_PS1-}" ]; then
  125. # PS1 changed, perhaps in another precmd. See issue 9938.
  126. ITERM2_SHOULD_DECORATE_PROMPT="1"
  127. fi
  128. fi
  129. iterm2_after_cmd_executes "$STATUS"
  130. if [ -n "$ITERM2_SHOULD_DECORATE_PROMPT" ]; then
  131. iterm2_decorate_prompt
  132. fi
  133. }
  134. # This is not run if you press ^C while entering a command.
  135. iterm2_preexec() {
  136. # Set PS1 back to its raw value prior to executing the command.
  137. PS1="$ITERM2_PRECMD_PS1"
  138. ITERM2_SHOULD_DECORATE_PROMPT="1"
  139. iterm2_before_cmd_executes
  140. }
  141. # If hostname -f is slow on your system set iterm2_hostname prior to
  142. # sourcing this script. We know it is fast on macOS so we don't cache
  143. # it. That lets us handle the hostname changing like when you attach
  144. # to a VPN.
  145. if [ -z "${iterm2_hostname-}" ]; then
  146. if [ "$(uname)" != "Darwin" ]; then
  147. iterm2_hostname=`hostname -f 2>/dev/null`
  148. # Some flavors of BSD (i.e. NetBSD and OpenBSD) don't have the -f option.
  149. if [ $? -ne 0 ]; then
  150. iterm2_hostname=`hostname`
  151. fi
  152. fi
  153. fi
  154. [[ -z ${precmd_functions-} ]] && precmd_functions=()
  155. precmd_functions=($precmd_functions iterm2_precmd)
  156. [[ -z ${preexec_functions-} ]] && preexec_functions=()
  157. preexec_functions=($preexec_functions iterm2_preexec)
  158. iterm2_print_state_data
  159. printf "\033]1337;ShellIntegrationVersion=14;shell=zsh\007"
  160. fi
  161. fi