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(options = {}) ⇒ 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(options = {}) ⇒ TerminalBridge
Instance methods
20 21 22 23 |
# File 'lib/divide/terminal_bridge.rb', line 20 def initialize(={}) @options = @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
25 26 27 |
# File 'lib/divide/terminal_bridge.rb', line 25 def bridge @bridge ||= Divide::TerminalBridge end |
#exec(commands) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# 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 ⇒ Object
37 38 39 |
# File 'lib/divide/terminal_bridge.rb', line 37 def go_to_current_directory do_script "cd #{@options[:from]}" end |
#insert_between(array, insert_between) ⇒ Object
71 72 73 |
# 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) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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 ⇒ Object
33 34 35 |
# 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) ⇒ Object
29 30 31 |
# File 'lib/divide/terminal_bridge.rb', line 29 def tell_current_app(cmd) %(tell app "#{@app_name}" to #{cmd}) end |