Class: Consular::OSX
- Inherits:
-
Core
- Object
- Core
- Consular::OSX
- 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
-
.to_s ⇒ Object
Returns name of Core.
-
.valid_system? ⇒ Boolean
Checks to see if the current system is on darwin and if $TERM_PROGRAM is set to Apple_Terminal.
Instance Method Summary collapse
-
#active_tab ⇒ Object
Returns the last instantiated tab from active window.
-
#active_window ⇒ Object
Returns the currently active window by checking to see if it is the :frontmost window.
-
#execute_command(cmd, options = {}) ⇒ Object
Execute the given command in the context of the active window.
-
#execute_window(content, options = {}) ⇒ Object
Executes the commands for each designated window.
-
#initialize(path) ⇒ OSX
constructor
Initializes a reference to the Terminal.app via appscript.
-
#open_tab(options = nil) ⇒ Object
Opens a new tab and return the last instantiated tab(itself).
-
#open_window(options = nil) ⇒ Object
Opens a new window and returns its last instantiated tab.(The first ‘tab’ in the window).
-
#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.
-
#set_options(options = {}) ⇒ Object
Sets the options for the windows/tabs.
-
#set_title(title, commands) ⇒ Object
Prepend a title setting command prior to the other commands.
-
#setup! ⇒ Object
Method called by runner to Execute Termfile setup.
-
#terminal_process ⇒ Object
Returns the current terminal process.
Constructor Details
#initialize(path) ⇒ OSX
Initializes a reference to the Terminal.app via appscript
44 45 46 47 |
# File 'lib/consular/osx.rb', line 44 def initialize(path) super @terminal = app('Terminal') end |
Class Method Details
.to_s ⇒ Object
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.
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_tab ⇒ Object
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_window ⇒ Object
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.
160 161 162 |
# File 'lib/consular/osx.rb', line 160 def execute_command(cmd, = {}) active_window.do_script cmd, 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.
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, = {}) = content[:options] _contents = content[:tabs] _first_run = true _contents.keys.sort.each do |key| _content = _contents[key] = content[:options] _name = [:name] _tab = if _first_run && ![:default] open_window .merge() else key == 'default' ? active_window : open_tab() 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).
177 178 179 |
# File 'lib/consular/osx.rb', line 177 def open_tab( = nil) open_terminal_with 't', end |
#open_window(options = nil) ⇒ Object
Opens a new window and returns its last instantiated tab.(The first ‘tab’ in the window).
195 196 197 |
# File 'lib/consular/osx.rb', line 195 def open_window( = nil) open_terminal_with 'n', end |
#prepend_befores(commands, befores = nil) ⇒ Array<String>
Prepends the :before commands to the current context’s commands if it exists.
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.
207 208 209 |
# File 'lib/consular/osx.rb', line 207 def ( = {}) # raise NotImplementedError end |
#set_title(title, commands) ⇒ Object
Prepend a title setting command prior to the other commands.
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_process ⇒ Object
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 |