Module: MarkdownTableFormatter

Defined in:
lib/format_table.rb

Class Method Summary collapse

Class Method Details

.calculate_column_alignment_and_widths(rows, column_count) ⇒ Object



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

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



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

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



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

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



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

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

.format_cell(cell, align, width) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/format_table.rb', line 66

def format_cell(cell, align, width)
  case align
  when :center
    cell.center(width)
  when :right
    cell.rjust(width)
  else
    cell.ljust(width)
  end
end

.format_row_line(row, alignment_indicators, column_widths, decorate, text_sym: :text, style_sym: :color) ⇒ Object



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

def format_row_line(
  row, alignment_indicators, column_widths, decorate,
  text_sym: :text, style_sym: :color
)
  return '' 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]),
             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
  ).decorate
end

.format_rows(rows, alignment_indicators, column_widths, decorate) ⇒ Object



109
110
111
112
113
# File 'lib/format_table.rb', line 109

def format_rows(rows, alignment_indicators, column_widths, decorate)
  rows.map do |row|
    format_row_line(row, alignment_indicators, column_widths, decorate)
  end
end

.format_table(lines:, column_count:, decorate: nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/format_table.rb', line 115

def format_table(lines:, column_count:, decorate: nil)
  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)

  alignment_indicators, column_widths =
    calculate_column_alignment_and_widths(rows, column_count)

  format_rows(rows, alignment_indicators, column_widths, decorate)
end

.insert_every_other(array, obj) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/format_table.rb', line 131

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) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/format_table.rb', line 140

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

  ret = []
  lines.each do |line|
    line += '|' unless line.end_with?('|')
    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)
    ret << OpenStruct.new(cells: cells, role: role, counter: counter)
  end
  ret
end

.reset_counter_if_needed(role, counter) ⇒ Object



157
158
159
# File 'lib/format_table.rb', line 157

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

.role_for_raw_row(current_role, line) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/format_table.rb', line 161

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) ⇒ Object



178
179
180
181
# File 'lib/format_table.rb', line 178

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