lib/termup/base.rb in termup-2.0.3 vs lib/termup/base.rb in termup-3.0.0

- old
+ new

@@ -1,90 +1,34 @@ -require 'yaml' - module Termup class Base - def initialize(project) - @handler = Termup::Handler.new - - config = YAML.load(File.read("#{TERMUP_DIR}/#{project}.yml")) - @tabs = config['tabs'] - - # 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 - - @options = config['options'] || {} - @iterm_options = @options['iterm'] - - # Split panes for iTerm 2 - split_panes if @handler.iterm? and @iterm_options - - # 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 - - # 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 - # 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 + def initialize(project, process) + @config = YAML.load_file(Termup::Dir.join("#{project}.yml")) + @options = @config['options'] || {} + @tabs = @config['tabs'] || {} + @process = process + @lines = [] end - def split_panes - width, height = @iterm_options['width'], @iterm_options['height'] - return unless width and height - - (width - 1).times do - layout :split_vertically - end - layout :goto_next_pane # Back home - width.times do - (height - 1).times do - layout :split_horizontally - end - layout :goto_next_pane # Move to next, or back home - end + def start + script = <<-JS + var app = Application(#{@process.pid}); + var se = Application('System Events'); + app.activate(); + #{@lines.join(';')} + JS + ExecJS.exec script end - 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 + protected - def layout(command) - @handler.layout(command) + def hit(key, *using) + # activate + using = using.map{|i| i.to_s.gsub(/_/,' ') } + case key + when Integer + @lines << "se.keyCode(#{key}, { using: #{using} })" + when String + @lines << "se.keystroke('#{key}', { using: #{using} })" + end end end end