Class: RTables::Table::UnicodeMonoTable

Inherits:
RTables::TableBuilder show all
Defined in:
lib/rtables/tables/unicodemonotable.rb

Constant Summary collapse

CORNER_POS_LTOP =
'┌'
CORNER_POS_RTOP =
'┐'
CORNER_POS_MTOP =
'┬'
CORNER_POS_LMID =
'├'
CORNER_POS_RMID =
'┤'
CORNER_POS_MMID =
'┼'
CORNER_POS_LBOT =
'└'
CORNER_POS_RBOT =
'┘'
CORNER_POS_MBOT =
'┴'

Instance Attribute Summary

Attributes inherited from RTables::TableBuilder

#columns, #rows, #table_content, #table_header

Instance Method Summary collapse

Methods inherited from RTables::TableBuilder

#add_column, #add_row, #column_exist?, #empty?, #initialize, #inspect, #raise_if_empty, #to_s

Constructor Details

This class inherits a constructor from RTables::TableBuilder

Instance Method Details

#max_content_lengthObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rtables/tables/unicodemonotable.rb', line 60

def max_content_length
  max_len = 0

  @table_content.each do |contents|
    contents.each do |content|
      len = content.length
      max_len = len if max_len < len
    end
  end

  max_len
end

#max_header_lengthObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/rtables/tables/unicodemonotable.rb', line 73

def max_header_length
  max_len = 0

  @table_header.each do |head|
    len = head.length
    max_len = len if max_len < len
  end

  max_len
end

#pad_content(content, clen) ⇒ Object



54
55
56
57
58
# File 'lib/rtables/tables/unicodemonotable.rb', line 54

def pad_content(content, clen)
  spacing = clen - content.length

  content + (' ' * spacing)
end

#pad_header(header, hlen) ⇒ Object



48
49
50
51
52
# File 'lib/rtables/tables/unicodemonotable.rb', line 48

def pad_header(header, hlen)
  spacing = hlen - header.length

  header + (' ' * spacing)
end

#renderObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rtables/tables/unicodemonotable.rb', line 16

def render
  line_horizontal = '─'
  line_vertical = '│'

  hlen = max_header_length
  clen = max_content_length

  row_sep_base = "#{line_horizontal * (hlen + 2)}%{tchar}#{line_horizontal * (clen + 2)}"
  row_sep_top = "#{CORNER_POS_LTOP}#{row_sep_base % { tchar: CORNER_POS_MTOP }}#{CORNER_POS_RTOP}"
  row_sep_mid = "#{CORNER_POS_LMID}#{row_sep_base % { tchar: CORNER_POS_MMID }}#{CORNER_POS_RMID}"
  row_sep_bot = "#{CORNER_POS_LBOT}#{row_sep_base % { tchar: CORNER_POS_MBOT }}#{CORNER_POS_RBOT}"

  line_fmt = "#{line_vertical} %{header} #{line_vertical} %{content} #{line_vertical}"
  lines = []
  i = 0

  lines << row_sep_top
  @table_content.each do |contents|
    contents.each do |content|
      lines << line_fmt % { header: pad_header(@table_header[i], hlen), content: pad_content(content, clen) }
      i += 1
    end

    i = 0
    lines << row_sep_mid
  end
  lines.pop
  lines << row_sep_bot

  lines
end