class Divide::TerminalBridge

Attributes

app_name[R]

Public Class Methods

apple_script(command) click to toggle source
# File lib/divide/terminal_bridge.rb, line 10
def self.apple_script(command)
  command = command.join("' -e '") if command.is_a?(Array)
  execute("osascript -e '#{command}'")
end
current_app_name() click to toggle source
# File lib/divide/terminal_bridge.rb, line 15
def self.current_app_name
  @current_app_name ||= apple_script('tell app "System Events" to get name of the first process whose frontmost is true').strip
end
execute(command) click to toggle source

Class methods

# File lib/divide/terminal_bridge.rb, line 6
def self.execute(command)
  `#{command}`
end
new(options={}) click to toggle source

Instance methods

# File lib/divide/terminal_bridge.rb, line 20
def initialize(options={})
  @options = options
  @app_name = bridge.current_app_name
end

Public Instance Methods

bridge() click to toggle source
# File lib/divide/terminal_bridge.rb, line 25
def bridge
  @bridge ||= Divide::TerminalBridge
end
exec(commands) click to toggle source
# File lib/divide/terminal_bridge.rb, line 60
def exec(commands)
  commands = Array(commands)

  scripts = commands.map { |c| do_script(c) }
  scripts_with_new_tabs = insert_between(scripts, open_new_tab_in_current_directory).flatten
  scripts_with_new_tabs.unshift(open_new_tab_in_current_directory) unless @options[:from] == Dir.pwd
  scripts_with_new_tabs.push(open_new_tab_in_current_directory) unless @options[:'no-new-tab']

  bridge.apple_script(scripts_with_new_tabs)
end
go_to_current_directory() click to toggle source
# File lib/divide/terminal_bridge.rb, line 37
def go_to_current_directory
  do_script "cd #{@options[:from]}"
end
insert_between(array, insert_between) click to toggle source
# File lib/divide/terminal_bridge.rb, line 71
def insert_between(array, insert_between)
  array.flat_map { |a| [a, insert_between] }[0...-1]
end
keystroke(key) click to toggle source
# File lib/divide/terminal_bridge.rb, line 41
def keystroke(key)
  splits = key.split('+')

  if splits.length > 2
    key = splits.last
    modifier_keys = splits - [key]
    modifier_key = "{#{modifier_keys.map { |m| "#{m} down" }.join(', ')}}"
  elsif splits.length == 2
    key = splits.last
    modifier_key = "#{splits.first} down"
  else
    key = splits[0]
    modifier_key = nil
  end

  modifier = modifier_key ? " using #{modifier_key}" : ''
  %(tell app "System Events" to tell process "#{@app_name}" to keystroke "#{key}"#{modifier})
end
open_new_tab_in_current_directory() click to toggle source
# File lib/divide/terminal_bridge.rb, line 33
def open_new_tab_in_current_directory
  [open_new_tab, go_to_current_directory]
end
tell_current_app(cmd) click to toggle source
# File lib/divide/terminal_bridge.rb, line 29
def tell_current_app(cmd)
  %(tell app "#{@app_name}" to #{cmd})
end