Class: Consular::OSX

Inherits:
Core
  • Object
show all
Includes:
Appscript
Defined in:
lib/consular/osx.rb

Overview

Consular Core to interact with Mac OS X Terminal.

Constant Summary collapse

ALLOWED_OPTIONS =

The acceptable options that OSX Terminal will register.

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ OSX

Initializes a reference to the Terminal.app via appscript

Parameters:

  • path (String)

    path to Termfile.



44
45
46
47
# File 'lib/consular/osx.rb', line 44

def initialize(path)
  super
  @terminal = app('Terminal')
end

Class Method Details

.to_sObject

Returns name of Core. used in CLI core selection



32
33
34
# File 'lib/consular/osx.rb', line 32

def to_s
  "Consular::OSX Mac OSX Terminal"
end

.valid_system?Boolean

Checks to see if the current system is on darwin and if $TERM_PROGRAM is set to Apple_Terminal.

Returns:

  • (Boolean)


25
26
27
# File 'lib/consular/osx.rb', line 25

def valid_system?
  (RUBY_PLATFORM.downcase =~ /darwin/) && ENV['TERM_PROGRAM'] == 'Apple_Terminal'
end

Instance Method Details

#active_tabObject

Returns the last instantiated tab from active window



225
226
227
228
229
# File 'lib/consular/osx.rb', line 225

def active_tab
  window = active_window
  tabs   = window.tabs if window
  tabs.last.get        if tabs
end

#active_windowObject

Returns the currently active window by checking to see if it is the :frontmost window.



235
236
237
238
239
240
# File 'lib/consular/osx.rb', line 235

def active_window
  windows = @terminal.windows.get
  windows.detect do |window|
    window.properties_.get[:frontmost] rescue false
  end
end

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

Execute the given command in the context of the active window.

Examples:

@osx.execute_command 'ps aux', :in => @tab_object

Parameters:

  • cmd (String)

    The command to execute.

  • options (Hash) (defaults to: {})

    Additional options to pass into appscript for the context.



160
161
162
# File 'lib/consular/osx.rb', line 160

def execute_command(cmd, options = {})
  active_window.do_script cmd, options
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.

Examples:

@core.execute_window contents, :default => true
@core.execute_window contents, :default => true

Parameters:

  • content (Hash)

    The hash contents of the window from the Termfile.

  • options (Hash) (defaults to: {})

    Addional options to pass. You can use:

    :default - Whether this is being run as the default window.
    


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/consular/osx.rb', line 90

def execute_window(content, options = {})
  window_options = content[:options]
  _contents      = content[:tabs]
  _first_run     = true

  _contents.keys.sort.each do |key|
    _content = _contents[key]
    _options = content[:options]
    _name    = options[:name]

    _tab =
    if _first_run && !options[:default]
      open_window options.merge(window_options)
    else
      key == 'default' ? active_window : open_tab(_options)
    end

    _first_run = false
    commands = prepend_befores _content[:commands], content[:before]
    commands = set_title _name, commands
    commands.each { |cmd| execute_command cmd, :in => _tab }
  end

end

#open_tab(options = nil) ⇒ Object

Opens a new tab and return the last instantiated tab(itself).

Parameters:

  • options (Hash) (defaults to: nil)

    Options to further customize the window. You can use:

    :settings -
    :selected -
    

Returns:

  • Returns a refernce to the last instantiated tab of the window.



177
178
179
# File 'lib/consular/osx.rb', line 177

def open_tab(options = nil)
  open_terminal_with 't', options
end

#open_window(options = nil) ⇒ Object

Opens a new window and returns its last instantiated tab.(The first ‘tab’ in the window).

Parameters:

  • options (Hash) (defaults to: nil)

    Options to further customize the window. You can use:

    :bound        - Set the bounds of the windows
    :visible      - Set whether or not the current window is visible
    :miniaturized - Set whether or not the window is minimized
    

Returns:

  • Returns a refernce to the last instantiated tab of the window.



195
196
197
# File 'lib/consular/osx.rb', line 195

def open_window(options = nil)
  open_terminal_with 'n', options
end

#prepend_befores(commands, befores = nil) ⇒ Array<String>

Prepends the :before commands to the current context’s commands if it exists.

Parameters:

  • commands (Array<String>)

    The current tab commands

  • befores (Array<String>) (defaults to: nil)

    The current window’s :befores

Returns:

  • (Array<String>)

    The current context commands with the :before commands prepended



140
141
142
143
144
145
146
# File 'lib/consular/osx.rb', line 140

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.



59
60
61
62
63
64
# File 'lib/consular/osx.rb', line 59

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

#set_options(options = {}) ⇒ Object

Sets the options for the windows/tabs. Will filter options based on what options can be set for either windows or tabs.

Parameters:

  • options (Hash) (defaults to: {})

    Options to set for the window/tab



207
208
209
# File 'lib/consular/osx.rb', line 207

def set_options(options = {})
  # raise NotImplementedError
end

#set_title(title, commands) ⇒ Object

Prepend a title setting command prior to the other commands.

Parameters:

  • title (String)

    The title to set for the context of the commands.

  • commands (Array<String>)

    The context of commands to preprend to.



123
124
125
126
# File 'lib/consular/osx.rb', line 123

def set_title(title, commands)
  cmd = "PS1=\"$PS1\\[\\e]2;#{title}\\a\\]\""
  title ? commands.insert(0, cmd) : commands
end

#setup!Object

Method called by runner to Execute Termfile setup.



52
53
54
# File 'lib/consular/osx.rb', line 52

def setup!
  @termfile[:setup].each { |cmd| execute_command(cmd, :in => active_window) }
end

#terminal_processObject

Returns the current 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.



218
219
220
# File 'lib/consular/osx.rb', line 218

def terminal_process
  app('System Events').application_processes['Terminal.app']
end