Sha256: 1288ea9fa90d7855ae2cfda6834352c0870f596dffb4a492bd9448822708d699

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

module TablePal
  class PlainText

    attr_reader :table

    def initialize(table:)
      @table = table

      table.rows.each do |row|
        print_row(row)
      end
    end

    private

    def print_row(row)
      if row.heading
        print_heading_row(row)
      elsif row.subheading
        print_subheading_row(row)
      elsif row.section_end
        print_section_end_row(row)
      else
        print_regular_row(row)
      end
    end

    def print_heading_row(row)
      print_cells(row)
      puts
      underline
      empty
    end

    def print_subheading_row(row)
      print_cells(row)
      puts
      empty
    end

    def print_section_end_row(row)
      print_cells(row)
      puts
      empty
    end

    def print_regular_row(row)
      print_cells(row)
      puts
    end

    def print_cells(row)
      row.cells_including_empty.each do |cell|
        print_cell(cell)
      end
    end

    def print_cell(cell)
      print cell.column.left_border
      print cell.column.left_padding
      print cell.formatted(justified: true, coloured: false)
      print cell.column.right_padding
      print cell.column.right_border
    end

    def underline
      table.columns.map do |column|
        char = '-'
        print column.left_border
        print column.left_padding_char(char)
        print char * column.width
        print column.right_padding_char(char)
        print column.right_border
      end

      puts
    end

    def empty
      table.columns.map do |column|
        char = ' '
        print column.left_border
        print column.left_padding_char(char)
        print char * column.width
        print column.right_padding_char(char)
        print column.right_border
      end

      puts
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
table_pal-0.3.3 lib/plain_text_not_coloured.rb
table_pal-0.3.2 lib/plain_text_not_coloured.rb