Class: Railbow::Table::Renderer

Inherits:
Object
  • Object
show all
Includes:
Railbow::TextUtils
Defined in:
lib/railbow/table/renderer.rb

Constant Summary collapse

RESET =
"\e[0m"
WHITE =
"\e[97m"
GHOST_BG =

deep red/maroon background — stands out as abnormal

"\e[48;5;52m"
GHOST_FG =

warm pink foreground for contrast

"\e[38;5;217m"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Railbow::TextUtils

#display_width, #strip_ansi, #truncate_str

Constructor Details

#initialize(columns:, theme:, compact: {}, aliases: {}) ⇒ Renderer

Returns a new instance of Renderer.



18
19
20
21
22
23
24
# File 'lib/railbow/table/renderer.rb', line 18

def initialize(columns:, theme:, compact: {}, aliases: {})
  @compact = compact
  @aliases = aliases
  @reverse_col_aliases = aliases[:columns]&.invert || {}
  @columns = apply_hidden_columns(columns)
  @theme = theme
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



16
17
18
# File 'lib/railbow/table/renderer.rb', line 16

def columns
  @columns
end

#themeObject (readonly)

Returns the value of attribute theme.



16
17
18
# File 'lib/railbow/table/renderer.rb', line 16

def theme
  @theme
end

Instance Method Details

#render(rows, separators: {}, highlight_rows: Set.new, ghost_rows: Set.new, tick_rows: Set.new, tick_col: nil) ⇒ Object



26
27
28
29
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/railbow/table/renderer.rb', line 26

def render(rows, separators: {}, highlight_rows: Set.new, ghost_rows: Set.new, tick_rows: Set.new, tick_col: nil)
  return "" if columns.empty?

  # Remap rows if columns were hidden
  rows = remap_rows(rows) if @hidden_indices&.any?

  # Apply value aliases
  rows = apply_value_aliases(rows) if @aliases[:values]&.any?

  # Pre-truncate non-last columns that have truncate + max_width
  rows = rows.map { |row|
    row.each_with_index.map { |cell, i|
      col = columns[i]
      if col&.truncate && col.max_width && i < columns.size - 1
        truncate_str(cell.to_s, col.max_width)
      else
        cell
      end
    }
  }

  resolved = resolve_widths(rows)

  # In oneline mode, truncate non-sticky non-last columns at resolved width
  if @compact[:oneline]
    rows = rows.map { |row|
      row.each_with_index.map { |cell, i|
        col = columns[i]
        if col && i < columns.size - 1 && !col.sticky && !col.truncate
          truncate_str(cell.to_s, resolved[i])
        else
          cell
        end
      }
    }
  end
  lines = []
  lines << render_header(resolved) unless @compact[:noheader]
  rows.each_with_index do |row, i|
    tc = (tick_rows.include?(i) && tick_col) ? tick_col : nil
    if separators.key?(i) && theme.format_separator
      sep_row = Array.new(columns.size, "")
      sep_row[1] = theme.format_separator.call(separators[i]) if columns.size > 1
      lines << render_row(sep_row, resolved, tick_col: tc, tick_cross: true)
      tc = nil # tick already shown on separator row
    end
    formatted = render_row(row, resolved, tick_col: tc, highlight: highlight_rows.include?(i), ghost: ghost_rows.include?(i))
    lines << formatted
  end
  lines.join("\n")
end