class Termrc::Builder

Constants

SLEEP_INTERVAL
TEMPLATE
TEMPLATE_FILE

Attributes

commands[RW]
layout[RW]

Public Class Methods

new(yml) click to toggle source
# File lib/termrc/builder.rb, line 16
def initialize(yml)
  @root       = yml['root'] || File.dirname( File.expand_path('./Termfile') )
  @commands   = yml['commands']
  @windows    = yml['windows'] || []

  #support traditional format not using multiple windows
  if yml['layout']
    @windows << {'layout' => yml['layout'], 'layout_type' => yml['layout_type'] }
  end
end

Public Instance Methods

applescript_file(layout_array, index) click to toggle source
# File lib/termrc/builder.rb, line 50
def applescript_file(layout_array, index)
  t = generateContents(layout_array, index)
  writeTempFile(t)
end
applescript_files() click to toggle source
# File lib/termrc/builder.rb, line 40
def applescript_files
  if tabs?
    return @layout.each_with_index.map{ |layout_array, index|
      applescript_file(layout_array, index)
    }
  else
    return [ applescript_file(@layout, 0) ]
  end
end
generateContents(layout_array, index) click to toggle source
# File lib/termrc/builder.rb, line 55
def generateContents(layout_array, index)
  t = TEMPLATE

  if index > 0
    # All other tabs.
    t = t.gsub("[window_or_tab]",     new_tab)
    t = t.gsub("[session]",           current_session)
    t = t.gsub("[terminate_unused]",  terminate_session('last'))
  else
    # First tab.
    t = t.gsub("[window_or_tab]",     new_window)
    t = t.gsub("[session]",           new_session)
    t = t.gsub("[terminate_unused]",  terminate_session)
  end

  t = t.gsub("[primary]",   primary(layout_array))
  t = t.gsub("[sleep]",     rest)
  t = t.gsub("[panes]",     panes(layout_array))
  t = t.gsub("[commands]",  commands(layout_array))
  t
end
panes(layout_array) click to toggle source
# File lib/termrc/builder.rb, line 88
def panes(layout_array)
  cmd =   next_pane     # back to the top
  cmd =   next_pane     # back to the top

  layout_array.each do |cmds|
    cmd << Array.new( cmds.length - 1, @columns ? new_row : new_column ).join("\n")
    cmd << next_pane
    cmd << "\n"
  end

  cmd
end
primary(layout_array) click to toggle source
# File lib/termrc/builder.rb, line 84
def primary(layout_array)
  Array.new( primary_count(layout_array), @columns ? new_column : new_row ).join("\n")
end
primary_count(layout_array) click to toggle source
# File lib/termrc/builder.rb, line 119
def primary_count(layout_array)
  layout_array.length
end
run!() click to toggle source
# File lib/termrc/builder.rb, line 27
def run!
  @windows.each do |current_window|
    @cmd_index  = 1
    @layout = current_window['layout']
    @columns = current_window['layout_type'] == 'column' || false

    applescript_files.each do |f|
      puts `/usr/bin/osascript #{f.path}`
      f.unlink
    end
  end
end
tabs?() click to toggle source
# File lib/termrc/builder.rb, line 123
def tabs?
  @layout[0].is_a?(Array) and @layout[0][0].is_a? Array
end
writeTempFile(contents) click to toggle source
# File lib/termrc/builder.rb, line 77
def writeTempFile(contents)
  file = Tempfile.new('termrc.osascript')
  file.write(contents)
  file.close
  file
end

Protected Instance Methods

current_session() click to toggle source
# File lib/termrc/builder.rb, line 160
def current_session
  "set mysession to (current session)"
end
execute_command(item, command, name) click to toggle source
# File lib/termrc/builder.rb, line 168
    def execute_command(item, command, name)
      command = command.gsub('"', '\\"')
      command = command.gsub("'", "\\'")
      command = "cd #{@root} && " + command if @root
      <<-EOH
        tell item #{item} of sessions to set name to "#{name}" \n
        tell item #{item} of sessions to write text \"#{command}\" \n
      EOH
    end
keystroke(key, using="") click to toggle source
# File lib/termrc/builder.rb, line 178
def keystroke(key, using="")
  cmd =   "tell application \"System Events\" to keystroke \"#{key}\" "
  cmd <<  "using {#{using}} \n" if  using
  cmd <<  "\n"                if !using
  cmd
end
new_column() click to toggle source
# File lib/termrc/builder.rb, line 137
def new_column
  keystroke("d", "command down")
end
new_row() click to toggle source
# File lib/termrc/builder.rb, line 133
def new_row
  keystroke("D", "command down")
end
new_session() click to toggle source
# File lib/termrc/builder.rb, line 156
def new_session
  "set mysession to (make new session at the end of sessions)"
end
new_tab() click to toggle source
# File lib/termrc/builder.rb, line 145
def new_tab
  [
    keystroke("t", "command down"),
    "set myterm to (current terminal)",
  ].join("\n")
end
new_window() click to toggle source
# File lib/termrc/builder.rb, line 152
def new_window
  [ "set myterm to (make new terminal)" ].join("\n")
end
next_pane() click to toggle source
# File lib/termrc/builder.rb, line 141
def next_pane
  keystroke("]", "command down")
end
rest(i=SLEEP_INTERVAL) click to toggle source
# File lib/termrc/builder.rb, line 129
def rest(i=SLEEP_INTERVAL)
  "do shell script \"sleep #{i}\" \n"
end
terminate_session(which='first') click to toggle source
# File lib/termrc/builder.rb, line 164
def terminate_session(which='first')
  "terminate the #{which} session"
end