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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yml) ⇒ Builder

Returns a new instance of Builder.



16
17
18
19
20
21
# File 'lib/termrc/builder.rb', line 16

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

Instance Attribute Details

#commands(layout_array) ⇒ Object

Returns the value of attribute commands.



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

def commands
  @commands
end

#layoutObject

Returns the value of attribute layout.



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

def layout
  @layout
end

Instance Method Details

#applescript_file(layout_array, index) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/termrc/builder.rb', line 40

def applescript_file(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("[rows]",      rows(layout_array))
  t = t.gsub("[sleep]",     rest)
  t = t.gsub("[panes]",     panes(layout_array))
  t = t.gsub("[commands]",  commands(layout_array))

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

#applescript_filesObject



30
31
32
33
34
35
36
37
38
# File 'lib/termrc/builder.rb', line 30

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

#panes(layout_array) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/termrc/builder.rb', line 70

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, new_column ).join("\n")
    cmd << next_pane
    cmd << "\n"
  end

  cmd
end

#row_count(layout_array) ⇒ Object



96
97
98
# File 'lib/termrc/builder.rb', line 96

def row_count(layout_array)
  layout_array.length
end

#rows(layout_array) ⇒ Object



66
67
68
# File 'lib/termrc/builder.rb', line 66

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

#run!Object



23
24
25
26
27
28
# File 'lib/termrc/builder.rb', line 23

def run!
  applescript_files.each do |f|
    puts `/usr/bin/osascript #{f.path}`
    f.unlink
  end
end

#tabs?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/termrc/builder.rb', line 100

def tabs?
  @layout[0].is_a?(Array) and @layout[0][0].is_a? Array
end