Sha256: 9eaf0a6dc12a711aca2390a5818d72e363b72a62d5d00cb107966cfcc96caf39

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

module TablePal
  class PlainTextColoured

    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: true)
      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.1 lib/plain_text_coloured.rb
table_pal-0.3.0 lib/plain_text_coloured.rb