Module: Tabelinha
- Defined in:
- lib/table.rb
Class Method Summary collapse
-
.table(rows, options = {}) ⇒ Object
Generates a formatted table with customizable options.
Class Method Details
.table(rows, options = {}) ⇒ Object
Generates a formatted table with customizable options.
This method generates a formatted table with the specified rows and options. It supports various formatting elements to provide flexibility in table appearance.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/table.rb', line 45 def table(rows, = {}) # sets options = { space_linebroken: true, padding: 1, max_width: Float::INFINITY, corners: { top_right: '┌', top_left: '┐', bottom_right: '└', bottom_left: '┘' }, straight: { vertical: '│', horizontal: '─' }, junctions: { top: '┬', middle: '┼', bottom: '┴' } }.merge() padding_amount = [:padding] padding = ' ' * padding_amount # split \n and normalize rows newline_treated_rows = [] rows.each do |row| max_size = 0 split_row = row.map do |cell| split_cells = cell.split("\n") max_size = split_cells.length if split_cells.length > max_size split_cells end.map do |split_cells| split_cells += Array.new(max_size - split_cells.length, '') end.transpose newline_treated_rows += split_row if [:space_linebroken] && split_row.length > 1 newline_treated_rows << Array.new(split_row.first.length, ' ') end end rows = newline_treated_rows # set widths for each column to be printed, fitting terminal size columns = rows.transpose total_column_width = [:max_width] - columns.length - 1 current_column_widths = columns.map { |column| column.map { |cell| cell.gsub(/\e\[([;\d]+)?m/, '').length }.max } actual_column_widths = current_column_widths if current_column_widths.sum > total_column_width even_width_per_column = total_column_width / columns.length even_width_per_column = 1 if even_width_per_column.zero? width_for_linebroken = total_column_width - actual_column_widths.select { |n| n < even_width_per_column }.sum actual_column_widths = current_column_widths.map do |width| width > even_width_per_column ? even_width_per_column : width end end # write the top of the table table = '' table << .dig(:corners, :top_right) table << actual_column_widths.map do |width| str = '' str << .dig(:straight, :horizontal) * width str << .dig(:straight, :horizontal) * padding_amount * 2 end.join(.dig(:junctions, :top)) table << .dig(:corners, :top_left) table << "\n" # write the contents of the table(breaks lines if needed) rows.each do |row| stripped_columns = row.map.with_index do |cell, column_i| width = actual_column_widths[column_i] cell.chars.each { |a| } no_ansi = cell.gsub(/\e\[([;\d]+)?m/, '') divide_cell(cell, width) end height = stripped_columns.map(&:length).max height += 1 if height > 1 && [:space_linebroken] new_columns = stripped_columns.map.with_index do |column, column_i| width = actual_column_widths[column_i] column.fill(' ' * width, column.length..(height - 1)) column end table << new_columns.transpose.map do |row| str = '' str << .dig(:straight, :vertical) + padding str << row.join(padding + .dig(:straight, :vertical) + padding) str << padding + .dig(:straight, :vertical) end.join("\n") table << "\n" end # writes the bottom of the table table << .dig(:corners, :bottom_right) table << actual_column_widths.map do |width| str = '' str << .dig(:straight, :horizontal) * width str << .dig(:straight, :horizontal) * padding_amount * 2 end.join(.dig(:junctions, :bottom)) table << .dig(:corners, :bottom_left) table << "\n" end |