module TablePal class PlainText attr_reader :table, :coloured, :output, :result def initialize(table:, coloured: false, output: true) @table = table @coloured = coloured @output = output @result = '' table.rows.each do |row| print_row(row) end end def to_s result 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) add_new_line underline empty end def print_subheading_row(row) print_cells(row) add_new_line empty end def print_section_end_row(row) print_cells(row) add_new_line empty end def print_regular_row(row) print_cells(row) add_new_line end def print_cells(row) row.cells_including_empty.each do |cell| print_cell(cell) end end def print_cell(cell) add_chars cell.column.left_border add_chars cell.column.left_padding add_chars cell.formatted(justified: true, coloured: coloured) add_chars cell.column.right_padding add_chars cell.column.right_border end def underline table.columns.map do |column| char = '-' add_chars column.left_border add_chars column.left_padding_char(char) add_chars char * column.width add_chars column.right_padding_char(char) add_chars column.right_border end add_new_line end def empty table.columns.map do |column| char = ' ' add_chars column.left_border add_chars column.left_padding_char(char) add_chars char * column.width add_chars column.right_padding_char(char) add_chars column.right_border end add_new_line end def add_chars(chars) @result.concat(chars) print chars if output end def add_new_line @result.concat("\n") puts if output end end end