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
22
# File 'lib/termrc/builder.rb', line 16

def initialize(yml)
  @root       = yml['root']
  @commands   = yml['commands']
  @layout     = yml['layout']
  @columns    = yml['layout_type'] == 'column' || false
  @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



41
42
43
44
# File 'lib/termrc/builder.rb', line 41

def applescript_file(layout_array, index)
  t = generateContents(layout_array, index)
  writeTempFile(t)
end

#applescript_filesObject



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

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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/termrc/builder.rb', line 46

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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/termrc/builder.rb', line 79

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) ⇒ Object



75
76
77
# File 'lib/termrc/builder.rb', line 75

def primary(layout_array)
  Array.new( primary_count(layout_array), @columns ? new_column : new_row ).join("\n")
end

#primary_count(layout_array) ⇒ Object



110
111
112
# File 'lib/termrc/builder.rb', line 110

def primary_count(layout_array)
  layout_array.length
end

#run!Object



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

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

#tabs?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/termrc/builder.rb', line 114

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

#writeTempFile(contents) ⇒ Object



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

def writeTempFile(contents)
  file = Tempfile.new('termrc.osascript')
  file.write(contents)
  file.close
  file
end