Class: TableFormatter
- Inherits:
-
Object
- Object
- TableFormatter
- Defined in:
- lib/table-formatter.rb
Overview
file: table-formatter.rb
Instance Attribute Summary collapse
-
#border ⇒ Object
Returns the value of attribute border.
-
#col_justify ⇒ Object
Returns the value of attribute col_justify.
-
#divider ⇒ Object
Returns the value of attribute divider.
-
#labels ⇒ Object
Returns the value of attribute labels.
-
#markdown ⇒ Object
Returns the value of attribute markdown.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #display(width = nil, widths: nil, markdown: @markdown) ⇒ Object (also: #to_s)
-
#initialize(source: nil, labels: nil, border: true, wrap: true, divider: nil, markdown: false, innermarkdown: false) ⇒ TableFormatter
constructor
A new instance of TableFormatter.
- #justify(s, ajust) ⇒ Object
Constructor Details
#initialize(source: nil, labels: nil, border: true, wrap: true, divider: nil, markdown: false, innermarkdown: false) ⇒ TableFormatter
Returns a new instance of TableFormatter.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/table-formatter.rb', line 9 def initialize(source: nil, labels: nil, border: true, wrap: true, divider: nil, markdown: false, innermarkdown: false) super() @source = source @labels = labels @border = border @wrap = wrap @divider = divider @maxwidth = 60 @markdown = markdown @innermarkdown = innermarkdown end |
Instance Attribute Details
#border ⇒ Object
Returns the value of attribute border.
7 8 9 |
# File 'lib/table-formatter.rb', line 7 def border @border end |
#col_justify ⇒ Object
Returns the value of attribute col_justify.
7 8 9 |
# File 'lib/table-formatter.rb', line 7 def col_justify @col_justify end |
#divider ⇒ Object
Returns the value of attribute divider.
7 8 9 |
# File 'lib/table-formatter.rb', line 7 def divider @divider end |
#labels ⇒ Object
Returns the value of attribute labels.
7 8 9 |
# File 'lib/table-formatter.rb', line 7 def labels @labels end |
#markdown ⇒ Object
Returns the value of attribute markdown.
7 8 9 |
# File 'lib/table-formatter.rb', line 7 def markdown @markdown end |
#source ⇒ Object
Returns the value of attribute source.
7 8 9 |
# File 'lib/table-formatter.rb', line 7 def source @source end |
Instance Method Details
#display(width = nil, widths: nil, markdown: @markdown) ⇒ Object Also known as: to_s
46 47 48 49 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 |
# File 'lib/table-formatter.rb', line 46 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
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/table-formatter.rb', line 24 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 |