Class: Rich::Columns
- Inherits:
-
Object
- Object
- Rich::Columns
- Defined in:
- lib/rich/layout.rb
Overview
Display content side by side in columns
Instance Attribute Summary collapse
-
#column_count ⇒ Integer?
readonly
Column count.
-
#equal ⇒ Boolean
readonly
Equal width columns.
-
#expand ⇒ Boolean
readonly
Expand to fill width.
-
#items ⇒ Array
readonly
Items to display.
-
#padding ⇒ Integer
readonly
Padding between columns.
Instance Method Summary collapse
-
#add(item) ⇒ self
Add an item.
-
#initialize(items = [], column_count: nil, padding: 1, equal: false, expand: true) ⇒ Columns
constructor
A new instance of Columns.
-
#render(max_width: 80, color_system: ColorSystem::TRUECOLOR) ⇒ String
Render to string.
-
#to_segments(max_width: 80) ⇒ Array<Segment>
Render to segments.
Constructor Details
#initialize(items = [], column_count: nil, padding: 1, equal: false, expand: true) ⇒ Columns
Returns a new instance of Columns.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rich/layout.rb', line 25 def initialize( items = [], column_count: nil, padding: 1, equal: false, expand: true ) @items = items.to_a @column_count = column_count @padding = padding @equal = equal @expand = end |
Instance Attribute Details
#column_count ⇒ Integer? (readonly)
Returns Column count.
14 15 16 |
# File 'lib/rich/layout.rb', line 14 def column_count @column_count end |
#equal ⇒ Boolean (readonly)
Returns Equal width columns.
20 21 22 |
# File 'lib/rich/layout.rb', line 20 def equal @equal end |
#expand ⇒ Boolean (readonly)
Returns Expand to fill width.
23 24 25 |
# File 'lib/rich/layout.rb', line 23 def @expand end |
#items ⇒ Array (readonly)
Returns Items to display.
11 12 13 |
# File 'lib/rich/layout.rb', line 11 def items @items end |
#padding ⇒ Integer (readonly)
Returns Padding between columns.
17 18 19 |
# File 'lib/rich/layout.rb', line 17 def padding @padding end |
Instance Method Details
#add(item) ⇒ self
Add an item
42 43 44 45 |
# File 'lib/rich/layout.rb', line 42 def add(item) @items << item self end |
#render(max_width: 80, color_system: ColorSystem::TRUECOLOR) ⇒ String
Render to string
94 95 96 |
# File 'lib/rich/layout.rb', line 94 def render(max_width: 80, color_system: ColorSystem::TRUECOLOR) Segment.render(to_segments(max_width: max_width), color_system: color_system) end |
#to_segments(max_width: 80) ⇒ Array<Segment>
Render to segments
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 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rich/layout.rb', line 50 def to_segments(max_width: 80) return [] if @items.empty? segments = [] # Calculate column count if not specified num_columns = @column_count || calculate_column_count(max_width) num_columns = [num_columns, @items.length].min # Calculate column widths col_widths = calculate_widths(max_width, num_columns) # Render items in rows @items.each_slice(num_columns) do |row_items| row_items.each_with_index do |item, col_index| content = item.to_s width = col_widths[col_index] # Render content content_width = Cells.cell_len(content) if content_width > width content = truncate(content, width) content_width = Cells.cell_len(content) end segments << Segment.new(content) segments << Segment.new(" " * (width - content_width)) # Padding between columns (not after last) if col_index < row_items.length - 1 segments << Segment.new(" " * @padding) end end segments << Segment.new("\n") end segments end |