85401d481106e4d47709dea0270d5160004d9b30

Author
Aviv A. Rosenberg <aviv.rosenberg@gmail.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Plugin for iTerm2 (#3790)

* Plugin for iTerm2 on OSX

iTerm2 is a popular terminal emulator for OSX (https://www.iterm2.com).

The plugin currently implements one function, iterm2_profile, which
allows easily changing the currently selected user settings profile,
without creating a new tab or window, just by calling the function.

For example, this is handy for switching from a dark to a light colored
profile without having to re-open anything.

In addition, it also works within tmux running inside iTerm2.

* iTerm2 Plugin: Run any iTerm2 command

Refactored the plugin so that it can run arbitrary iTerm2 commands.
Should work with any of the supported commands, see
https://iterm2.com/documentation-escape-codes.html

* iTerm2 Plugin: Add functions to change tab color

Diff

 1diff --git a/plugins/iterm2/iterm2.plugin.zsh b/plugins/iterm2/iterm2.plugin.zsh
 2new file mode 100644
 3index 0000000000000000000000000000000000000000..e4ac72ee32f5d433eb810b3dd59c7d59bb7c2ce3
 4--- /dev/null
 5+++ b/plugins/iterm2/iterm2.plugin.zsh
 6@@ -0,0 +1,68 @@
 7+#####################################################
 8+# iTerm2 plugin for oh-my-zsh                       #
 9+# Author: Aviv Rosenberg (github.com/avivrosenberg) #
10+#####################################################
11+
12+###
13+# This plugin is only relevant if the terminal is iTerm2 on OSX.
14+if [[ "$OSTYPE" == darwin* ]] && [[ -n "$ITERM_SESSION_ID" ]] ; then
15+
16+  ###
17+  # Executes an arbitrary iTerm2 command via an escape code sequce.
18+  # See https://iterm2.com/documentation-escape-codes.html for all supported commands.
19+  # Example: $ _iterm2_command "1337;StealFocus"
20+  function _iterm2_command() {
21+    local cmd="$1"
22+
23+    # Escape codes for wrapping commands for iTerm2.
24+    local iterm2_prefix="\x1B]"
25+    local iterm2_suffix="\x07"
26+
27+    # If we're in tmux, a special escape code must be prepended/appended so that
28+    # the iTerm2 escape code is passed on into iTerm2.
29+    if [[ -n $TMUX ]]; then
30+      local tmux_prefix="\x1BPtmux;\x1B"
31+      local tmux_suffix="\x1B\\"
32+    fi
33+
34+    echo -n "${tmux_prefix}${iterm2_prefix}${cmd}${iterm2_suffix}${tmux_suffix}"
35+  }
36+
37+  ###
38+  # iterm2_profile(): Function for changing the current terminal window's
39+  # profile (colors, fonts, settings, etc).
40+  # To change the current iTerm2 profile, call this function and pass in a name
41+  # of another existing iTerm2 profile (name can contain spaces).
42+  function iterm2_profile() {
43+    # Desired name of profile
44+    local profile="$1"
45+
46+    # iTerm2 command for changing profile
47+    local cmd="1337;SetProfile=$profile"
48+
49+    # send the sequence
50+    _iterm2_command "${cmd}"
51+
52+    # update shell variable
53+    ITERM_PROFILE="$profile"
54+  }
55+
56+  ###
57+  # iterm2_tab_color(): Changes the color of iTerm2's currently active tab.
58+  # Usage: iterm2_tab_color <red> <green> <blue>
59+  #        where red/green/blue are on the range 0-255.
60+  function iterm2_tab_color() {
61+    _iterm2_command "6;1;bg;red;brightness;$1"
62+    _iterm2_command "6;1;bg;green;brightness;$2"
63+    _iterm2_command "6;1;bg;blue;brightness;$3"
64+  }
65+
66+
67+  ###
68+  # iterm2_tab_color_reset(): Resets the color of iTerm2's current tab back to
69+  # default.
70+  function iterm2_tab_color_reset() {
71+    _iterm2_command "6;1;bg;*;default"
72+  }
73+
74+fi