Module: MarkdownTableFormatter

Defined in:
lib/format_table.rb

Class Method Summary collapse

Class Method Details

.calculate_column_alignment_and_widths(rows, column_count) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/format_table.rb', line 11

def calculate_column_alignment_and_widths(rows, column_count)
  alignment_indicators = Array.new(column_count, :left)
  column_widths = Array.new(column_count, 0)

  rows.each do |row|
    next if row.cells.nil?

    row.cells.each_with_index do |cell, i|
      column_widths[i] = [column_widths[i], cell.length].max

      if row.role == :separator_line
        alignment_indicators[i] = determine_column_alignment(cell)
      end
    end
  end

  # 2024-08-24 remove last column if it is 0-width
  if column_widths.last&.zero?
    column_widths.pop
    alignment_indicators.pop
  end

  [alignment_indicators, column_widths]
end

.decorate_line(line, role, counter, decorate) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/format_table.rb', line 36

def decorate_line(line, role, counter, decorate)
  return line unless decorate

  style = decoration_style(line, role, counter, decorate)
  return line unless style

  AnsiString.new(line).send(style)
end

.decoration_style(role, counter, decorate) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/format_table.rb', line 45

def decoration_style(role, counter, decorate)
  return nil unless decorate

  return nil unless (style = decorate[role])

  if style.is_a?(Array)
    style[counter % style.count]
  else
    style
  end
end

.determine_column_alignment(cell) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/format_table.rb', line 57

def determine_column_alignment(cell)
  if cell =~ /^-+:$/
    :right
  elsif cell =~ /^:-+:$/
    :center
  else
    :left
  end
end

.format_cell(cell, align, width, truncate: true) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/format_table.rb', line 67

def format_cell(cell, align, width, truncate: true)
  plain_string = cell.gsub(/\033\[[\d;]+m|\033\[0m/, '')
  exceeded = plain_string.length > width
  truncated = false
  ret = TrackedString.new(
    case
    when truncate && exceeded
      truncated = true
      plain_string[0, width] ### s/trim decorated text
    when exceeded
      cell
    when align == :center
      cell.center(width)
    when align == :right
      cell.rjust(width)
    else
      cell.ljust(width)
    end
  )
  ret.exceeded = exceeded
  ret.truncated = truncated
  ret
end

.format_row_line__hs(row, alignment_indicators, column_widths, decorate, style_sym: :color, text_sym: :text, truncate: true) ⇒ Object



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
# File 'lib/format_table.rb', line 91

def format_row_line__hs(
  row, alignment_indicators, column_widths, decorate,
  style_sym: :color,
  text_sym: :text,
  truncate: true
)
  return HierarchyString.new if row.cells.nil?

  border_style = decorate && decorate[:border]
  HierarchyString.new(
    [{ text_sym => '| ', style_sym => border_style },
     *insert_every_other(
       row.cells.map.with_index do |cell, i|
         next unless alignment_indicators[i] && column_widths[i]

         if row.role == :separator_line
           { text_sym => '-' * column_widths[i],
             style_sym => decorate && decorate[row.role] }
         else
           {
             text_sym => format_cell(
               cell, alignment_indicators[i], column_widths[i],
               truncate: truncate
             ),
             style_sym => decoration_style(row.role, row.counter, decorate)
           }
         end
       end.compact,
       { text_sym => ' | ', style_sym => border_style }
     ),
     { text_sym => ' |', style_sym => border_style }].compact,
    style_sym: style_sym,
    text_sym: text_sym
  )
end

.format_rows__hs(rows, alignment_indicators, column_widths, decorate, truncate: true) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/format_table.rb', line 127

def format_rows__hs(
  rows, alignment_indicators, column_widths, decorate,
  truncate: true
)
  rows.map do |row|
    format_row_line__hs(
      row, alignment_indicators, column_widths, decorate,
      truncate: truncate
    )
  end
end

.format_table(**kwargs) ⇒ Object



139
140
141
# File 'lib/format_table.rb', line 139

def format_table(**kwargs)
  format_table__hs(**kwargs).map(&:decorate)
end

.format_table__hs(column_count:, decorate: nil, lines:, max_table_width: nil, table:, truncate: true) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/format_table.rb', line 143

def format_table__hs(
  column_count:,
  decorate: nil,
  lines:,
  max_table_width: nil,
  table:,
  truncate: true
)
  unless column_count.positive?
    return lines.map do |line|
      HierarchyString.new([{ text: line }])
      # HierarchyString.new([{ text: line, color: decorate }]) #???
    end
  end

  rows = raw_lines_into_row_role_cells(
    lines, column_count, delimiter: table[:delimiter]
  )

  alignment_indicators, column_widths =
    calculate_column_alignment_and_widths(rows, column_count)

  unless max_table_width.nil?
    # each column has a frame width of 3 characters
    # border and space before and after each and 1 final border
    borders_width = (column_count * 3) + 1

    full_column_table_width = column_widths.sum + borders_width

    if full_column_table_width > max_table_width
      text_width_sum = full_column_table_width - borders_width
      available_text_width = max_table_width - borders_width
      ratio = available_text_width.to_f / text_width_sum

      # distribute the width across the columns
      column_widths.each_with_index do |width, i|
        column_widths[i] = (width * ratio).to_i
      end

      # the last column fills the remaining width
      column_widths[column_widths.count - 1] =
        available_text_width - column_widths.sum + column_widths.last
    end
  end

  format_rows__hs(
    rows, alignment_indicators, column_widths, decorate,
    truncate: truncate
  )
end

.insert_every_other(array, obj) ⇒ Object



194
195
196
197
198
199
200
201
# File 'lib/format_table.rb', line 194

def insert_every_other(array, obj)
  result = []
  array.each_with_index do |element, index|
    result << element
    result << obj if index < array.size - 1
  end
  result
end

.raw_lines_into_row_role_cells(lines, column_count, delimiter:) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/format_table.rb', line 203

def raw_lines_into_row_role_cells(
  lines, column_count, delimiter:
)
  role = :header_row
  counter = -1

  ret = []
  lines.each do |line|
    line += delimiter unless line.end_with?(delimiter)

    counter += 1

    role = role_for_raw_row(role, line)
    counter = reset_counter_if_needed(role, counter)
    cells = split_decorated_row_into_cells(line, column_count,
                                           delimiter: delimiter)
    ret << OpenStruct.new(cells: cells, role: role, counter: counter)
  end
  ret
end

.reset_counter_if_needed(role, counter) ⇒ Object



224
225
226
# File 'lib/format_table.rb', line 224

def reset_counter_if_needed(role, counter)
  i[header_row row].include?(role) ? counter : 0
end

.role_for_raw_row(current_role, line) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/format_table.rb', line 228

def role_for_raw_row(current_role, line)
  case current_role
  when :header_row
    if line =~ /^[ \t]*\| *[:\-][:\- |]*$/
      :separator_line
    else
      current_role
    end
  when :separator_line
    :row
  when :row
    current_role
  else
    raise "Unexpected role: #{current_role} for line #{line}"
  end
end

.split_decorated_row_into_cells(line, column_count, delimiter: '|') ⇒ Object



245
246
247
248
# File 'lib/format_table.rb', line 245

def split_decorated_row_into_cells(line, column_count, delimiter: '|')
  cells = line.split(delimiter).map(&:strip)[1..-1]
  cells&.slice(0, column_count)&.fill('', cells.length...column_count)
end