Class: Terminitor::ItermCore

Inherits:
AbstractCore show all
Includes:
Appscript
Defined in:
lib/terminitor/cores/iterm_core.rb

Overview

Mac OS X Core for Terminitor This Core manages all the interaction with Appscript and the Terminal

Constant Summary collapse

ALLOWED_OPTIONS =
{
  :window => [:bounds, :visible, :miniaturized],
  :tab => [:settings, :selected]
}

Instance Attribute Summary

Attributes inherited from AbstractCore

#termfile, #terminal, #windows, #working_dir

Instance Method Summary collapse

Methods inherited from AbstractCore

#load_termfile, #process!, #run_in_window, #setup!

Constructor Details

#initialize(path) ⇒ ItermCore

Initialize @terminal with Terminal.app, Load the Windows, store the Termfile Terminitor::MacCore.new(‘/path’)



14
15
16
17
18
19
# File 'lib/terminitor/cores/iterm_core.rb', line 14

def initialize(path)
  super
  @terminal = app('iTerm')
  @windows  = @terminal.terminals
  @delayed_options = []
end

Instance Method Details

#active_windowObject

returns the active windows



61
62
63
# File 'lib/terminitor/cores/iterm_core.rb', line 61

def active_window
  current_terminal.current_session.get
end

#current_terminalObject

Returns the current terminal



66
67
68
# File 'lib/terminitor/cores/iterm_core.rb', line 66

def current_terminal
  @terminal.current_terminal
end

#execute_command(cmd, options = {}) ⇒ Object

executes the given command via appscript execute_command ‘cd /path/to’, :in => #<tab>



23
24
25
26
27
28
29
# File 'lib/terminitor/cores/iterm_core.rb', line 23

def execute_command(cmd, options = {})      
  if options[:in]
    options[:in].write(:text => "#{cmd}")
  else
    active_window.write(:text => "#{cmd}")
  end
end

#open_tab(options = nil) ⇒ Object

Opens a new tab and returns itself. TODO : handle options (?)



33
34
35
36
37
# File 'lib/terminitor/cores/iterm_core.rb', line 33

def open_tab(options = nil)
  session = current_terminal.sessions.end.make( :new => :session )
  session.exec(:command => ENV['SHELL'])
  session
end

#open_window(options = nil) ⇒ Object

Opens A New Window, applies settings to the first tab and returns the tab object. TODO : handle options (?)



41
42
43
44
45
46
# File 'lib/terminitor/cores/iterm_core.rb', line 41

def open_window(options = nil)
  window  = terminal.make( :new => :terminal )
  session = window.sessions.end.make( :new => :session )
  session.exec(:command => ENV['SHELL'])
  session
end

#return_last_tabObject

Returns the last instantiated tab from active window



56
57
58
# File 'lib/terminitor/cores/iterm_core.rb', line 56

def return_last_tab
  current_terminal.sessions.last.get rescue false
end

#set_delayed_optionsObject

Apply delayed options and remove them from the queue



102
103
104
105
106
107
# File 'lib/terminitor/cores/iterm_core.rb', line 102

def set_delayed_options
  @delayed_options.length.times do 
    option = @delayed_options.shift
    option[:object].instance_eval(option[:option]).set(option[:value])
  end
end

#set_options(object, options = {}) ⇒ Object

Sets options of the given object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/terminitor/cores/iterm_core.rb', line 71

def set_options(object, options = {})
  options.each_pair do |option, value| 
    case option
    when :settings   # works for windows and tabs, for example :settings => "Grass"
      begin
        object.current_settings.set(@terminal.settings_sets[value])
      rescue Appscript::CommandError => e
        puts "Error: invalid settings set '#{value}'"
      end
    when :bounds # works only for windows, for example :bounds => [10,20,300,200]
      # the only working sequence to restore window size and position! 
      object.bounds.set(value)
      object.frame.set(value)
      object.position.set(value)
    when :selected # works for tabs, for example tab :active => true
      delayed_option(option, value, object)
    when :miniaturized # works for windows only
      delayed_option(option, value, object)
    when :name
      # ignore it.
    else # trying to apply any other option
      begin
        object.instance_eval(option.to_s).set(value)
      rescue
        puts "Error setting #{option} = #{value} on #{object.inspect}"
      end
    end
  end
end

#terminal_processObject

Returns the Terminal Process We need this method to workaround appscript so that we can instantiate new tabs and windows. otherwise it would have looked something like window.make(:new => :tab) but that doesn’t work.



51
52
53
# File 'lib/terminitor/cores/iterm_core.rb', line 51

def terminal_process
  app("System Events").application_processes["iTerm.app"]
end