Class: Divide::TerminalBridge
- Inherits:
-
Object
- Object
- Divide::TerminalBridge
- Defined in:
- lib/divide/terminal_bridge.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
Class Method Summary collapse
- .apple_script(command) ⇒ Object
- .current_app_name ⇒ Object
-
.execute(command) ⇒ Object
Class methods.
Instance Method Summary collapse
- #bridge ⇒ Object
- #exec(commands) ⇒ Object
- #go_to_current_directory ⇒ Object
-
#initialize ⇒ TerminalBridge
constructor
Instance methods.
- #insert_between(array, insert_between) ⇒ Object
- #keystroke(key) ⇒ Object
- #open_new_tab_in_current_directory ⇒ Object
- #tell_current_app(cmd) ⇒ Object
Constructor Details
#initialize ⇒ TerminalBridge
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_name ⇒ Object (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_name ⇒ Object
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
#bridge ⇒ Object
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_directory ⇒ Object
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_directory ⇒ Object
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 |