Class: Divide::TerminalBridge

Inherits:
Object
  • Object
show all
Defined in:
lib/divide/terminal_bridge.rb

Direct Known Subclasses

ITerm, Terminal

Defined Under Namespace

Classes: ITerm, Terminal

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTerminalBridge

Instance methods



20
21
22
# File 'lib/divide/terminal_bridge.rb', line 20

def initialize
  @app_name = bridge.current_app_name
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



3
4
5
# File 'lib/divide/terminal_bridge.rb', line 3

def app_name
  @app_name
end

Class Method Details

.apple_script(command) ⇒ Object



10
11
12
13
# 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_nameObject



15
16
17
# 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) ⇒ Object

Class methods



6
7
8
# File 'lib/divide/terminal_bridge.rb', line 6

def self.execute(command)
  `#{command}`
end

Instance Method Details

#bridgeObject



24
25
26
# File 'lib/divide/terminal_bridge.rb', line 24

def bridge
  @bridge ||= Divide::TerminalBridge
end

#exec(commands) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/divide/terminal_bridge.rb', line 55

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

  bridge.apple_script(scripts_with_new_tabs)
end

#go_to_current_directoryObject



36
37
38
# File 'lib/divide/terminal_bridge.rb', line 36

def go_to_current_directory
  do_script "cd #{Dir.pwd}"
end

#insert_between(array, insert_between) ⇒ Object



64
65
66
# File 'lib/divide/terminal_bridge.rb', line 64

def insert_between(array, insert_between)
  array.flat_map { |a| [a, insert_between] }[0...-1]
end

#keystroke(key) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/divide/terminal_bridge.rb', line 40

def keystroke(key)
  splits = key.split('+')

  if splits.length > 1
    modifier_key = splits[0]
    key = splits[1]
  else
    modifier_key = nil
    key = splits[0]
  end

  modifier = modifier_key ? " using #{modifier_key} down" : ''
  %(tell app "System Events" to tell process "#{@app_name}" to keystroke "#{key}"#{modifier})
end

#open_new_tab_in_current_directoryObject



32
33
34
# File 'lib/divide/terminal_bridge.rb', line 32

def open_new_tab_in_current_directory
  [open_new_tab, go_to_current_directory]
end

#tell_current_app(cmd) ⇒ Object



28
29
30
# File 'lib/divide/terminal_bridge.rb', line 28

def tell_current_app(cmd)
  %(tell app "#{@app_name}" to #{cmd})
end