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

def initialize(yml)
  @root       = yml['root']
  @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

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



50
51
52
53
# File 'lib/termrc/builder.rb', line 50

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

#applescript_filesObject



40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# 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) ⇒ Object



84
85
86
# 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) ⇒ Object



119
120
121
# File 'lib/termrc/builder.rb', line 119

def primary_count(layout_array)
  layout_array.length
end

#run!Object



27
28
29
30
31
32
33
34
35
36
37
38
# 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?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/termrc/builder.rb', line 123

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

#writeTempFile(contents) ⇒ Object



77
78
79
80
81
82
# File 'lib/termrc/builder.rb', line 77

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