Class: Termrc::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/termrc/builder.rb

Constant Summary collapse

SLEEP_INTERVAL =
1
TEMPLATE_FILE =
File.join( File.expand_path('../..', __FILE__),  'template', 'run.osascript' )
TEMPLATE =
File.read( TEMPLATE_FILE )

Instance Method Summary collapse

Constructor Details

#initialize(yml) ⇒ Builder

Returns a new instance of Builder.



13
14
15
16
17
# File 'lib/termrc/builder.rb', line 13

def initialize(yml)
  @root       = yml['root']
  @commands   = yml['commands']
  @layout     = yml['layout']
end

Instance Method Details

#applescript_fileObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/termrc/builder.rb', line 25

def applescript_file
  t = TEMPLATE
  t = t.gsub("[rows]",      rows)
  t = t.gsub("[sleep]",     sleep)
  t = t.gsub("[panes]",     panes)
  t = t.gsub("[commands]",  commands)
  t

  file = Tempfile.new('termrc.osascript')
  file.write(t)
  file.close
  file
end

#commandsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/termrc/builder.rb', line 56

def commands
  cmd = ""

  index = 1
  @layout.each do |commands|
    commands.each do |name|
      cmd << execute_command(index, @commands[name] )
      index += 1
    end
  end  

  cmd
end

#panesObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/termrc/builder.rb', line 43

def panes
  cmd =   next_pane     # back to the top
  cmd =   next_pane     # back to the top
  
  @layout.each do |cmds|
    cmd << Array.new( cmds.length - 1, new_column ).join("\n")
    cmd << next_pane
    cmd << "\n"
  end

  cmd
end

#row_countObject



70
71
72
# File 'lib/termrc/builder.rb', line 70

def row_count
  @row_count ||= @layout.length
end

#rowsObject



39
40
41
# File 'lib/termrc/builder.rb', line 39

def rows
  Array.new( row_count, new_row ).join("\n")
end

#run!Object



19
20
21
22
23
# File 'lib/termrc/builder.rb', line 19

def run!
  f = applescript_file
  puts `/usr/bin/osascript #{f.path}`
  f.unlink
end