lib/termup/base.rb in termup-1.3.1 vs lib/termup/base.rb in termup-2.0.0
- old
+ new
@@ -1,79 +1,90 @@
-require 'appscript'
require 'yaml'
module Termup
class Base
- ITERM1 = /^0\.10/
- ITERM2 = /^1.0/
+ def initialize(project)
+ @handler = Termup::Handler.new
- include Appscript
+ config = YAML.load(File.read("#{TERMUP_DIR}/#{project}.yml"))
+ @tabs = config['tabs']
- def initialize(project)
- @apps = app('System Events').application_processes
- @frontmost = @apps.get.select{|i| i.frontmost.get }.map{|i| i.name.get }.first
- return unless ['Terminal', 'iTerm'].include?(@frontmost)
- @terminal = app(@frontmost)
+ # Config file compatibility checking
+ if @tabs.is_a?(Array) and @tabs.first.is_a?(Hash)
+ abort 'YAML syntax for config has been changed. See https://github.com/kenn/termup for details.'
+ end
- @project = YAML.load(File.read("#{TERMUP_DIR}/#{project}.yml"))
+ @options = config['options'] || {}
+ @iterm_options = @options['iterm']
# Split panes for iTerm 2
- split_panes if iterm2? and @project['options']['iterm']
+ split_panes if @handler.iterm? and @iterm_options
- @project['tabs'].each do |hash|
- tabname = hash.keys.first
- cmds = hash.values.first
- cmds = [cmds].flatten
- tab = new_tab
- cmds.each do |cmd|
- if terminal?
- @terminal.do_script(cmd, :in => tab)
- else
- @terminal.current_terminal.current_session.write(:text => "#{cmd}")
- end
+ # Setting up tabs / panes
+ @tabs.each_with_index do |(tabname, values), index|
+ # Set tab title
+ @handler.set_property(:name, tabname)
+
+ # Run commands
+ (advanced_iterm? ? values['commands'] : values).each do |command|
+ @handler.run command
end
- end
- end
- def new_tab
- if @got_first_tab_already
- if iterm2?
- @apps[@frontmost].keystroke(']', :using => :command_down)
+ # Layout
+ if advanced_iterm?
+ values['properties'].each do |key, value|
+ @handler.set_property(key, value)
+ end if values['properties']
+
+ values['layout'].each do |command|
+ layout command
+ end if values['layout']
else
- @apps[@frontmost].keystroke('t', :using => :command_down)
+ # Move to next
+ if @iterm_options
+ layout :goto_next_pane
+ else
+ if index < @tabs.size - 1
+ layout :new_tab
+ sleep 0.01 # Allow some time to complete opening a new tab
+ else
+ layout :goto_next_tab # Back home
+ end
+ end
end
end
- @got_first_tab_already = true
- sleep 0.01 # Allow some time to complete opening a new tab
- @terminal.windows[1].tabs.last.get if terminal?
end
def split_panes
- # Virtical splits
- (@project['options']['iterm']['width'] - 1).times do |i|
- @apps[@frontmost].keystroke('d', :using => :command_down)
+ width, height = @iterm_options['width'], @iterm_options['height']
+ return unless width and height
+
+ (width - 1).times do
+ layout :split_vertically
end
- # Back to home
- @apps[@frontmost].keystroke(']', :using => :command_down)
- # Horizontal splits
- @project['options']['iterm']['width'].times do |i|
- (@project['options']['iterm']['height'] - 1).times do |i|
- @apps[@frontmost].keystroke('d', :using => [ :command_down, :shift_down ])
+ layout :goto_next_pane # Back home
+ width.times do
+ (height - 1).times do
+ layout :split_horizontally
end
- # Move to the right
- @apps[@frontmost].keystroke(']', :using => :command_down)
+ layout :goto_next_pane # Move to next, or back home
end
end
- def terminal?
- @frontmost == 'Terminal'
+ def advanced_iterm?
+ unless defined?(@advanced_iterm)
+ @advanced_iterm = case @tabs.values.first
+ when Hash then true
+ when Array then false
+ else
+ abort 'invalid YAML format'
+ end
+ abort 'advanced config only supported for iTerm' if @advanced_iterm and !@handler.iterm?
+ end
+ @advanced_iterm
end
- def iterm1?
- @frontmost == 'iTerm' and @terminal.version.get =~ ITERM1
- end
-
- def iterm2?
- @frontmost == 'iTerm' and @terminal.version.get =~ ITERM2
+ def layout(command)
+ @handler.layout(command)
end
end
end