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.



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

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.



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

def border
  @border
end

#col_justifyObject

Returns the value of attribute col_justify.



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

def col_justify
  @col_justify
end

#dividerObject

Returns the value of attribute divider.



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

def divider
  @divider
end

#labelsObject

Returns the value of attribute labels.



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

def labels
  @labels
end

#markdownObject

Returns the value of attribute markdown.



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

def markdown
  @markdown
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

Instance Method Details

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



50
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
# File 'lib/table-formatter.rb', line 50

def display(width=nil, widths: nil, markdown: @markdown)
  
  @align_cols = []
  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



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

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