Class: TypedPrint::Table
- Inherits:
-
Object
- Object
- TypedPrint::Table
- Defined in:
- lib/typed_print/table.rb
Instance Method Summary collapse
-
#initialize(data, alignments = {}, only_columns = nil, custom_headers = {}, color: false, colors: nil) ⇒ Table
constructor
A new instance of Table.
- #render(format = :plain) ⇒ Object
- #render_markdown ⇒ Object
- #render_plain ⇒ Object
Constructor Details
#initialize(data, alignments = {}, only_columns = nil, custom_headers = {}, color: false, colors: nil) ⇒ Table
Returns a new instance of Table.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/typed_print/table.rb', line 11 def initialize(data, alignments = {}, only_columns = nil, custom_headers = {}, color: false, colors: nil) @data = data @alignments = alignments @only_columns = only_columns&.map(&:to_sym) @custom_headers = custom_headers.transform_keys(&:to_sym) @color_auto = color @colors = colors&.transform_keys(&:to_sym) @headers = determine_headers end |
Instance Method Details
#render(format = :plain) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/typed_print/table.rb', line 22 def render(format = :plain) if format == :markdown render_markdown else render_plain end end |
#render_markdown ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/typed_print/table.rb', line 62 def render_markdown return "" if @data.empty? rows_plain = @data.map { |row| @headers.map { |h| format_value(row[h]) } } header_strings_plain = @headers.map { |h| header_label(h) } column_widths = header_strings_plain.map(&:length) rows_plain.each do |row| row.each_with_index do |cell, i| column_widths[i] = [column_widths[i], cell.length].max end end if color_mode? header_color = @color_auto ? :cyan : nil header_strings_display = header_strings_plain.map { |h| colorize(h, header_color) } rows_display = @data.map { |row| @headers.map { |h| colorize_cell(h, row[h]) } } else header_strings_display = header_strings_plain rows_display = rows_plain end output = [] output << "| " + header_strings_display.each_with_index.map { |h, i| pad_right(h, header_strings_plain[i], column_widths[i]) }.join(" | ") + " |" output << "|" + column_widths.map { |w| "-" * (w + 2) }.join("|") + "|" rows_plain.each_with_index do |plain_row, ri| output << "| " + rows_display[ri].each_with_index.map { |cell, i| pad_right(cell, plain_row[i], column_widths[i]) }.join(" | ") + " |" end output.join("\n") end |
#render_plain ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/typed_print/table.rb', line 30 def render_plain return "" if @data.empty? rows_plain = @data.map { |row| @headers.map { |h| format_value(row[h]) } } header_strings_plain = @headers.map { |h| header_label(h) } column_widths = header_strings_plain.map(&:length) rows_plain.each do |row| row.each_with_index do |cell, i| column_widths[i] = [column_widths[i], cell.length].max end end if color_mode? header_color = @color_auto ? :cyan : nil header_strings_display = header_strings_plain.map { |h| colorize(h, header_color) } rows_display = @data.map { |row| @headers.map { |h| colorize_cell(h, row[h]) } } else header_strings_display = header_strings_plain rows_display = rows_plain end output = [] output << format_row(header_strings_display, column_widths, :center, header_strings_plain) output << separator(column_widths) rows_plain.each_with_index do |plain_row, ri| output << format_row(rows_display[ri], column_widths, nil, plain_row) end output.join("\n") end |