Class: Consular::ITerm
- Inherits:
-
Core
- Object
- Core
- Consular::ITerm
- Includes:
- Appscript
- Defined in:
- lib/consular/iterm.rb
Overview
Consular Core to interact with iTerm2 for Mac OS X
Class Method Summary collapse
-
.to_s ⇒ Object
Returns the name of Core.
-
.valid_system? ⇒ Boolean
Checks to see if the current system is darwin and if $TERM_PROGRAM is iTerm.app.
Instance Method Summary collapse
-
#active_tab ⇒ Object
Returns the active tab e.g the active terminal session.
-
#active_window ⇒ Object
Returns the active window/tab e.g the active terminal window.
-
#call_ui_action(menu, submenu, action) ⇒ Object
Execute the menu action via UI.
-
#execute_command(cmd, options = {}) ⇒ Object
Execute the given command in the context of the active window.
-
#execute_pane_commands(pane_commands, tab_commands) ⇒ Object
Execute the commands within a pane.
-
#execute_panes(content) ⇒ Object
Execute the tab and associated panes with the designated content.
-
#execute_subpanes(subpanes, tab_commands) ⇒ Object
Execute commands in the context of sub panes.
-
#execute_window(content, options = {}) ⇒ Object
Executes the commands for each designated window.
-
#horizontal_split ⇒ Object
Split the active tab with horizontal panes.
-
#initialize(path) ⇒ ITerm
constructor
Initializes a reference to the iTerm.app via appscript.
-
#iterm_menu ⇒ Object
Returns a reference to the iTerm menu bar.
-
#open_tab(options = nil) ⇒ Object
Opens a new tab and focuses on it.
-
#open_window(options = nil) ⇒ Object
Opens a new window and focuses on the new tab.
-
#prepend_befores(commands, befores = nil) ⇒ Array<String>
Prepends the :before commands to the current context’s commands if it exists.
-
#process! ⇒ Object
Method called by runner to execute Termfile.
-
#select_pane(direction) ⇒ Object
to select panes; iTerm’s Appscript select method does not work as expected, we have to select via menu instead.
-
#set_title(title, tab) ⇒ Object
Prepend a title setting command prior to the other commands.
-
#setup! ⇒ Object
Method called by runner to Execute Termfile setup.
-
#top_level_pane_split(panes, commands) ⇒ Object
Execute commands in the context of a top level pane.
-
#vertical_split ⇒ Object
Split the active tab with vertical panes.
Constructor Details
#initialize(path) ⇒ ITerm
Initializes a reference to the iTerm.app via appscript
39 40 41 42 |
# File 'lib/consular/iterm.rb', line 39 def initialize(path) super @terminal = app('iTerm') end |
Class Method Details
.to_s ⇒ Object
Returns the name of Core. Used in CLI core selection.
27 28 29 |
# File 'lib/consular/iterm.rb', line 27 def to_s "Consular::ITerm Mac OSX iTerm2" end |
.valid_system? ⇒ Boolean
Checks to see if the current system is darwin and if $TERM_PROGRAM is iTerm.app
20 21 22 |
# File 'lib/consular/iterm.rb', line 20 def valid_system? (RUBY_PLATFORM.downcase =~ /darwin/) && ENV['TERM_PROGRAM'] == 'iTerm.app' end |
Instance Method Details
#active_tab ⇒ Object
Returns the active tab e.g the active terminal session.
292 293 294 |
# File 'lib/consular/iterm.rb', line 292 def active_tab active_window.current_session end |
#active_window ⇒ Object
Returns the active window/tab e.g the active terminal window.
299 300 301 |
# File 'lib/consular/iterm.rb', line 299 def active_window @terminal.current_terminal end |
#call_ui_action(menu, submenu, action) ⇒ Object
Execute the menu action via UI.
325 326 327 328 329 |
# File 'lib/consular/iterm.rb', line 325 def call_ui_action(, , action) = .[].[] = .[].[] if .[action].click end |
#execute_command(cmd, options = {}) ⇒ Object
Execute the given command in the context of the active window.
284 285 286 287 |
# File 'lib/consular/iterm.rb', line 284 def execute_command(cmd, = {}) context = [:in] || active_tab context.write :text => cmd end |
#execute_pane_commands(pane_commands, tab_commands) ⇒ Object
Execute the commands within a pane
214 215 216 217 |
# File 'lib/consular/iterm.rb', line 214 def execute_pane_commands(pane_commands, tab_commands) pane_commands = (tab_commands || []) + (pane_commands || []) pane_commands.each { |cmd| execute_command cmd } end |
#execute_panes(content) ⇒ Object
Execute the tab and associated panes with the designated content
154 155 156 157 158 |
# File 'lib/consular/iterm.rb', line 154 def execute_panes(content) panes = content[:panes] commands = content[:tabs][:commands] top_level_pane_split panes, commands end |
#execute_subpanes(subpanes, tab_commands) ⇒ Object
Execute commands in the context of sub panes
197 198 199 200 201 202 203 204 |
# File 'lib/consular/iterm.rb', line 197 def execute_subpanes(subpanes, tab_commands) subpanes.keys.sort.each do |subpane_key| subpane_commands = subpanes[subpane_key][:commands] horizontal_split execute_pane_commands(subpane_commands, tab_commands) select_pane 'Right' end end |
#execute_window(content, options = {}) ⇒ Object
Executes the commands for each designated window. .run_windows will iterate through each of the tabs in sorted order to execute the tabs in the order they were set. The logic follows this:
If the content is for the 'default' window,
then use the current active window and generate the commands.
If the content is for a new window,
then generate a new window and activate the windows.
Otherwise, open a new tab and execute the commands.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/consular/iterm.rb', line 117 def execute_window(content, = {}) = content[:options] tab_contents = content[:tabs] first_run = true tab_contents.keys.sort.each do |key| tab_content = tab_contents[key] = tab_content[:options] || {} tab_name = [:name] tab = if first_run && ![:default] open_window .merge() else key == 'default' ? active_tab : open_tab() && active_tab end set_title tab_name, tab first_run = false commands = prepend_befores tab_content[:commands], content[:before] if content.key? :panes commands.each { |cmd| execute_command cmd, :in => tab } execute_panes content content.delete :panes else commands.each { |cmd| execute_command cmd, :in => tab } end end end |
#horizontal_split ⇒ Object
Split the active tab with horizontal panes
229 230 231 |
# File 'lib/consular/iterm.rb', line 229 def horizontal_split call_ui_action "Shell", nil, "Split Horizontally with Current Profile" end |
#iterm_menu ⇒ Object
Returns a reference to the iTerm menu bar.
306 307 308 309 |
# File 'lib/consular/iterm.rb', line 306 def _process = app("System Events").processes["iTerm"] _process..first end |
#open_tab(options = nil) ⇒ Object
Opens a new tab and focuses on it.
257 258 259 |
# File 'lib/consular/iterm.rb', line 257 def open_tab( = nil) active_window.launch_ :session => 'New session' end |
#open_window(options = nil) ⇒ Object
Opens a new window and focuses on the new tab.
268 269 270 271 |
# File 'lib/consular/iterm.rb', line 268 def open_window( = nil) window = @terminal.make :new => :terminal window.launch_ :session => 'New session' end |
#prepend_befores(commands, befores = nil) ⇒ Array<String>
Prepends the :before commands to the current context’s commands if it exists.
73 74 75 76 77 78 79 |
# File 'lib/consular/iterm.rb', line 73 def prepend_befores(commands, befores = nil) unless befores.nil? || befores.empty? commands.insert(0, befores).flatten! else commands end end |
#process! ⇒ Object
Method called by runner to execute Termfile.
54 55 56 57 58 59 |
# File 'lib/consular/iterm.rb', line 54 def process! windows = @termfile[:windows] default = windows.delete('default') execute_window(default, :default => true) unless default[:tabs].empty? windows.each_pair { |_, cont| execute_window(cont) } end |
#select_pane(direction) ⇒ Object
to select panes; iTerm’s Appscript select method does not work as expected, we have to select via menu instead
241 242 243 244 245 246 247 248 |
# File 'lib/consular/iterm.rb', line 241 def select_pane(direction) valid_directions = %w[Above Below Left Right] if valid_directions.include?(direction) call_ui_action("Window", "Select Split Pane", "Select Pane #{direction}") else puts "Error: #{direction} is not a valid direction to select a pane; Only Above/Below/Left/Right are valid directions" end end |
#set_title(title, tab) ⇒ Object
Prepend a title setting command prior to the other commands.
89 90 91 |
# File 'lib/consular/iterm.rb', line 89 def set_title(title, tab) tab.name.set(title) if title end |
#setup! ⇒ Object
Method called by runner to Execute Termfile setup.
47 48 49 |
# File 'lib/consular/iterm.rb', line 47 def setup! @termfile[:setup].each { |cmd| execute_command(cmd) } end |
#top_level_pane_split(panes, commands) ⇒ Object
Execute commands in the context of a top level pane
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/consular/iterm.rb', line 168 def top_level_pane_split(panes, commands) first_pane = true panes.keys.sort.each do |pane_key| pane_content = panes[pane_key] pane_commands = pane_content[:commands] vertical_split if first_pane execute_pane_commands(pane_commands,commands) if pane_content.key?(:panes) select_pane('Left') if first_pane execute_subpanes pane_content[:panes], commands pane_content.delete :panes end first_pane = false if first_pane end end |
#vertical_split ⇒ Object
Split the active tab with vertical panes
222 223 224 |
# File 'lib/consular/iterm.rb', line 222 def vertical_split call_ui_action "Shell", nil, "Split Vertically with Current Profile" end |