Class: Termup::Handler
- Inherits:
-
Object
- Object
- Termup::Handler
- Defined in:
- lib/termup/handler.rb
Instance Method Summary collapse
- #activate ⇒ Object
- #app(*args) ⇒ Object
- #app_name ⇒ Object
- #app_process ⇒ Object
- #hit(key, *using) ⇒ Object
- #iterm? ⇒ Boolean
- #layout(command) ⇒ Object
- #run(command) ⇒ Object
- #set_property(key, value) ⇒ Object
- #term_like_pids ⇒ Object
- #term_pid ⇒ Object
- #terminal? ⇒ Boolean
Instance Method Details
#activate ⇒ Object
25 26 27 |
# File 'lib/termup/handler.rb', line 25 def activate app_process.activate end |
#app(*args) ⇒ Object
5 6 7 |
# File 'lib/termup/handler.rb', line 5 def app(*args) Appscript.app(*args) end |
#app_name ⇒ Object
47 48 49 |
# File 'lib/termup/handler.rb', line 47 def app_name @app_name ||= app_process.name.get end |
#app_process ⇒ Object
51 52 53 |
# File 'lib/termup/handler.rb', line 51 def app_process @app_process ||= app.by_pid(term_pid) end |
#hit(key, *using) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/termup/handler.rb', line 29 def hit(key, *using) activate case key when Integer app('System Events').processes[app_name].key_code key, using && { :using => using } when String app('System Events').processes[app_name].keystroke key, using && { :using => using } end end |
#iterm? ⇒ Boolean
43 44 45 |
# File 'lib/termup/handler.rb', line 43 def iterm? app_name == 'iTerm' end |
#layout(command) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/termup/handler.rb', line 81 def layout(command) if iterm? case command.to_sym when :new_tab then hit 't', :command_down when :close_tab then hit 'w', :command_down when :goto_previous_tab then hit '[', :command_down, :shift_down when :goto_next_tab then hit ']', :command_down, :shift_down when :goto_previous_pane then hit '[', :command_down when :goto_next_pane then hit ']', :command_down when :split_vertically then hit 'd', :command_down when :split_horizontally then hit 'd', :command_down, :shift_down when :go_left then hit 123, :command_down, :option_down when :go_right then hit 124, :command_down, :option_down when :go_down then hit 125, :command_down, :option_down when :go_up then hit 126, :command_down, :option_down else abort "Unknown iTerm2.app command: #{command}" end else case command.to_sym when :new_tab then hit 't', :command_down when :close_tab then hit 'w', :command_down when :goto_previous_tab then hit '[', :command_down, :shift_down when :goto_next_tab then hit ']', :command_down, :shift_down else abort "Unknown Terminal.app command: #{command}" end end end |
#run(command) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/termup/handler.rb', line 9 def run(command) if terminal? app_process.do_script(command, :in => app_process.windows[1].tabs.last.get) else app_process.current_terminal.current_session.write(:text => command) end end |
#set_property(key, value) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/termup/handler.rb', line 17 def set_property(key, value) if iterm? app_process.current_terminal.current_session.send(key).set(value) else # No-op for terminal for now end end |
#term_like_pids ⇒ Object
77 78 79 |
# File 'lib/termup/handler.rb', line 77 def term_like_pids @term_like_pids ||= `ps x | grep Term`.split("\n").reject{|i| i =~ /grep/ }.map{|i| i.match(/^\d+/).to_s.to_i } end |
#term_pid ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/termup/handler.rb', line 55 def term_pid return @term_pid if @term_pid pid = Process.ppid # Go up the process tree to find term-like process 100.times do ppid = `ps -p #{pid} -o ppid=`.strip.to_i abort 'terminal pid not found!' if ppid == 1 if term_like_pids.include?(ppid) @term_pid = ppid break end pid = ppid end @term_pid end |
#terminal? ⇒ Boolean
39 40 41 |
# File 'lib/termup/handler.rb', line 39 def terminal? app_name == 'Terminal' end |