Class: TableFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/table-formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source: nil, labels: nil, border: true, wrap: true, divider: nil, markdown: false, innermarkdown: false, debug: false) ⇒ TableFormatter

Returns a new instance of TableFormatter.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/table-formatter.rb', line 13

def initialize(source: nil, labels: nil, border: true, wrap: true, 
           divider: nil, markdown: false, innermarkdown: false, debug: false)

  super()
  @source = source.map {|x| x.map(&:to_s)} if source
  @labels = labels
  @border = border
  @wrap = wrap
  @divider = divider
  @maxwidth = 60
  @markdown = markdown
  @innermarkdown = innermarkdown
  @debug = debug
  
end

Instance Attribute Details

#borderObject

Returns the value of attribute border.



11
12
13
# File 'lib/table-formatter.rb', line 11

def border
  @border
end

#col_justifyObject

Returns the value of attribute col_justify.



11
12
13
# File 'lib/table-formatter.rb', line 11

def col_justify
  @col_justify
end

#dividerObject

Returns the value of attribute divider.



11
12
13
# File 'lib/table-formatter.rb', line 11

def divider
  @divider
end

#labelsObject

Returns the value of attribute labels.



11
12
13
# File 'lib/table-formatter.rb', line 11

def labels
  @labels
end

#markdownObject

Returns the value of attribute markdown.



11
12
13
# File 'lib/table-formatter.rb', line 11

def markdown
  @markdown
end

#sourceObject

Returns the value of attribute source.



11
12
13
# File 'lib/table-formatter.rb', line 11

def source
  @source
end

Instance Method Details

#display(width = nil, widths: nil, markdown: @markdown) ⇒ Object Also known as: to_s



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/table-formatter.rb', line 51

def display(width=nil, widths: nil, markdown: @markdown)
  
  @align_cols = []
  @labels ||= [''] * @source.first.length
  
  if @labels then

    labels = []

    @labels.each do |raw_label|
      col_just, label = just(raw_label)
      @align_cols << col_just
      labels << label
    end

  end

  @col_justify = @align_cols.map do |col|
    {ljust: :l, rjust: :r, center: :c}[col]
  end

  return display_markdown(@source, labels) if markdown    

  #width ||= @maxwidth
  @width = width
  @maxwidth = width if width
  a = @source.map {|x| x.map{|y| y.length > 0 ? y : ' ' }}.to_a

  column_widths = widths ? widths : fetch_column_widths(a)

  column_widths[-1] -= column_widths.inject(&:+) - width if width

  records = format_rows(a, column_widths)

  div =  (border == true ? '-' : '') * records[0].length + "\n"
  label_buffer = ''
  
  label_buffer = format_cols(labels, column_widths) + "\n" + div if labels

  div + label_buffer + records.join("\n") + "\n" + div

end

#justify(s, ajust) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/table-formatter.rb', line 29

def justify(s, ajust)
  
  a = s.split('|')
  
  a2 = ajust.map.with_index do |x,i|
    
    s = a[i+1]

    case x
    when :r
      '-' * (s.length - 1) + ':'
    when :l
      ':' + '-' * (s.length - 1)
    when
      ':' + '-' * (s.length - 2) + ':'
    end
  end

  "|%s|\n" % a2.join('|')    

end