Class: RubyRich::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_rich/table.rb

Constant Summary collapse

BORDER_STYLES =

边框样式定义

{
  none: {
    top: '', bottom: '', left: '', right: '', 
    horizontal: '-', vertical: '|',
    top_left: '', top_right: '', bottom_left: '', bottom_right: '',
    cross: '', top_cross: '', bottom_cross: '', left_cross: '', right_cross: ''
  },
  simple: {
    top: '-', bottom: '-', left: '|', right: '|',
    horizontal: '-', vertical: '|',
    top_left: '+', top_right: '+', bottom_left: '+', bottom_right: '+',
    cross: '+', top_cross: '+', bottom_cross: '+', left_cross: '+', right_cross: '+'
  },
  full: {
    top: '', bottom: '', left: '', right: '',
    horizontal: '', vertical: '',
    top_left: '', top_right: '', bottom_left: '', bottom_right: '',
    cross: '', top_cross: '', bottom_cross: '', left_cross: '', right_cross: ''
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers: [], align: :left, row_height: 1, border_style: :none) ⇒ Table

Returns a new instance of Table.



30
31
32
33
34
35
36
# File 'lib/ruby_rich/table.rb', line 30

def initialize(headers: [], align: :left, row_height: 1, border_style: :none)
  @headers = headers.map { |h| format_cell(h) }
  @rows = []
  @align = align
  @row_height = row_height
  @border_style = border_style
end

Instance Attribute Details

#alignObject

Returns the value of attribute align.



5
6
7
# File 'lib/ruby_rich/table.rb', line 5

def align
  @align
end

#border_styleObject

Returns the value of attribute border_style.



5
6
7
# File 'lib/ruby_rich/table.rb', line 5

def border_style
  @border_style
end

#headersObject

Returns the value of attribute headers.



6
7
8
# File 'lib/ruby_rich/table.rb', line 6

def headers
  @headers
end

#row_heightObject

Returns the value of attribute row_height.



5
6
7
# File 'lib/ruby_rich/table.rb', line 5

def row_height
  @row_height
end

#rowsObject

Returns the value of attribute rows.



5
6
7
# File 'lib/ruby_rich/table.rb', line 5

def rows
  @rows
end

Instance Method Details

#add_row(row) ⇒ Object



38
39
40
# File 'lib/ruby_rich/table.rb', line 38

def add_row(row)
  @rows << row.map { |cell| format_cell(cell) }
end

#renderObject



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/ruby_rich/table.rb', line 46

def render
  return render_empty_table if @headers.empty? && @rows.empty?
  
  column_widths = calculate_column_widths
  lines = []
  border_chars = BORDER_STYLES[@border_style] || BORDER_STYLES[:none]

  # Render top border
  if @border_style != :none && !@headers.empty?
    lines << render_horizontal_border(column_widths, :top)
  end

  # Render headers
  unless @headers.empty?
    lines.concat(render_styled_row(@headers, column_widths, bold: true))
    # Header separator line
    lines << render_horizontal_border(column_widths, :middle)
  end

  # Render rows
  @rows.each_with_index do |row, index|
    lines.concat(render_styled_multiline_row(row, column_widths))
  end

  # Render bottom border
  if @border_style != :none && (!@headers.empty? || !@rows.empty?)
    lines << render_horizontal_border(column_widths, :bottom)
  end

  lines.join("\n")
end