Module: Tabelinha

Defined in:
lib/table.rb

Class Method Summary collapse

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.

Examples:

rows = [
  ['Header 1', 'Header 2', 'Header 3'],
  ['Value 1', 'Value 2', 'Value 3'],
  ['Another Value 1', 'Another Value 2', 'Another Value 3']
]

options = {
  space_linebroken: true,
  padding: 1,
  max_width: 80,
  corners: { top_right: '', top_left: '', bottom_right: '', bottom_left: '' },
  straight: { vertical: '', horizontal: '' },
  junctions: { top: '', middle: '', bottom: '' }
}

table(rows, options)

Parameters:

  • rows (Array<Array<String>>)

    An array of rows, where each row is an array of cells to be displayed in the table.

  • options (Hash) (defaults to: {})

    An optional hash of configuration options to customize the appearance of the table.

Options Hash (options):

  • :space_linebroken (Boolean) — default: true

    If true, adds extra space between rows with line breaks.

  • :padding (Integer) — default: 1

    The number of spaces padding each cell.

  • :max_width (Float) — default: Float::INFINITY

    The maximum width of the table.

  • :corners (Hash)

    Hash containing characters for table corners. @option options [String] :top_right (‘┌’) Character for the top-right corner. @option options [String] :top_left (‘┐’) Character for the top-left corner. @option options [String] :bottom_right (‘└’) Character for the bottom-right corner. @option options [String] :bottom_left (‘┘’) Character for the bottom-left corner.

  • :straight (Hash)

    Hash containing characters for straight lines. @option options [String] :vertical (‘│’) Character for vertical lines. @option options [String] :horizontal (‘─’) Character for horizontal lines.

  • :junctions (Hash)

    Hash containing characters for junctions. @option options [String] :top (‘┬’) Character for the top junction. @option options [String] :middle (‘┼’) Character for the middle junction. @option options [String] :bottom (‘┴’) Character for the bottom junction.



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, options = {})
  # sets options

  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(options)

  padding_amount = options[: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 options[: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 = options[: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 << options.dig(:corners, :top_right)
  table << actual_column_widths.map do |width|
    str = ''
    str << options.dig(:straight, :horizontal) * width
    str << options.dig(:straight, :horizontal) * padding_amount * 2
  end.join(options.dig(:junctions, :top))

  table << options.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 && options[: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 << options.dig(:straight, :vertical) + padding
      str << row.join(padding + options.dig(:straight, :vertical) + padding)
      str << padding + options.dig(:straight, :vertical)
    end.join("\n")
    table << "\n"
  end

  # writes the bottom of the table

  table << options.dig(:corners, :bottom_right)
  table << actual_column_widths.map do |width|
    str = ''
    str << options.dig(:straight, :horizontal) * width
    str << options.dig(:straight, :horizontal) * padding_amount * 2
  end.join(options.dig(:junctions, :bottom))
  table << options.dig(:corners, :bottom_left)
  table << "\n"
end