Class: RubyRich::Columns

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

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total_width: 80, gutter_width: 2) ⇒ Columns

Returns a new instance of Columns.



39
40
41
42
43
# File 'lib/ruby_rich/columns.rb', line 39

def initialize(total_width: 80, gutter_width: 2)
  @columns = []
  @total_width = total_width
  @gutter_width = gutter_width
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



37
38
39
# File 'lib/ruby_rich/columns.rb', line 37

def columns
  @columns
end

#gutter_widthObject (readonly)

Returns the value of attribute gutter_width.



37
38
39
# File 'lib/ruby_rich/columns.rb', line 37

def gutter_width
  @gutter_width
end

#total_widthObject (readonly)

Returns the value of attribute total_width.



37
38
39
# File 'lib/ruby_rich/columns.rb', line 37

def total_width
  @total_width
end

Instance Method Details

#add_column(width: nil, align: :left, padding: 1, title: nil) ⇒ Object

添加列



46
47
48
49
50
51
52
53
54
# File 'lib/ruby_rich/columns.rb', line 46

def add_column(width: nil, align: :left, padding: 1, title: nil)
  column = Column.new(width: width, align: align, padding: padding, title: title)
  @columns << column
  
  # 如果没有指定宽度,自动计算平均宽度
  calculate_column_widths if width.nil?
  
  column
end

#clear_allObject

清空所有列的内容



63
64
65
66
# File 'lib/ruby_rich/columns.rb', line 63

def clear_all
  @columns.each(&:clear)
  self
end

#equal_widthsObject

设置等宽列



129
130
131
132
# File 'lib/ruby_rich/columns.rb', line 129

def equal_widths
  calculate_column_widths
  self
end

#remove_column(index) ⇒ Object

删除列



57
58
59
60
# File 'lib/ruby_rich/columns.rb', line 57

def remove_column(index)
  @columns.delete_at(index) if index >= 0 && index < @columns.length
  calculate_column_widths
end

#render(show_headers: true, show_borders: false, equal_height: true) ⇒ Object

渲染列布局



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
# File 'lib/ruby_rich/columns.rb', line 69

def render(show_headers: true, show_borders: false, equal_height: true)
  return "" if @columns.empty?

  calculate_column_widths
  lines = []

  # 渲染标题行
  if show_headers && @columns.any? { |col| col.title }
    header_line = render_header_line(show_borders)
    lines << header_line unless header_line.empty?
    
    if show_borders
      separator_line = render_separator_line
      lines << separator_line
    end
  end

  # 准备内容行
  max_height = equal_height ? @columns.map(&:height).max : 0
  
  # 填充较短的列以达到相同高度
  if equal_height && max_height > 0
    @columns.each do |column|
      while column.height < max_height
        column.add("")
      end
    end
  end

  # 渲染内容行
  content_height = @columns.map(&:height).max || 0
  content_height.times do |row_index|
    line = render_content_line(row_index, show_borders)
    lines << line
  end

  # 渲染底部边框
  if show_borders
    bottom_line = render_separator_line
    lines << bottom_line
  end

  lines.join("\n")
end

#set_ratios(*ratios) ⇒ Object

按比例设置列宽



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ruby_rich/columns.rb', line 115

def set_ratios(*ratios)
  return if ratios.length != @columns.length

  total_ratio = ratios.sum.to_f
  available_width = @total_width - (@gutter_width * (@columns.length - 1))
  
  @columns.each_with_index do |column, index|
    column.width = (available_width * ratios[index] / total_ratio).to_i
  end
  
  self
end