Class: CommandKit::Printing::Tables::TableFormatter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/command_kit/printing/tables/table_formatter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 0.4.0

Instance Method Summary collapse

Constructor Details

#initialize(table, style) ⇒ TableFormatter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialies the table formatter.

Parameters:

  • table (TableBuilder)

    The table data to print.

  • style (Style)

    The table's style configuration.

Since:

  • 0.4.0



20
21
22
23
24
25
26
27
28
29
# File 'lib/command_kit/printing/tables/table_formatter.rb', line 20

def initialize(table,style)
  @table = table
  @style = style

  @padding = ' ' * @style.padding
  @extra_padding = @style.padding * 2

  @last_column = @table.max_columns - 1
  @last_row    = @table.max_rows - 1
end

Instance Method Details

#format {|line| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Formats the table.

Yields:

  • (line)

    The given block will be passed each formatted line of the table.

Yield Parameters:

  • A (String)

    formatted line of the table.

Since:

  • 0.4.0



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/command_kit/printing/tables/table_formatter.rb', line 40

def format(&block)
  yield format_top_border if @style.border

  separator_row = if @table.header? || @style.separate_rows?
                    format_separator_row
                  end

  @table.max_rows.times do |row_index|
    row = @table[row_index]

    if @table.header? && row_index == 0
      format_header_row(row,&block)

      yield separator_row
    else
      format_row(row,&block)

      if @style.separate_rows? && row_index < @last_row
        yield separator_row
      end
    end
  end

  yield format_bottom_border if @style.border
end