Class: Rich::Columns

Inherits:
Object
  • Object
show all
Defined in:
lib/rich/layout.rb

Overview

Display content side by side in columns

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = expand
end

Instance Attribute Details

#column_countInteger? (readonly)

Returns Column count.

Returns:

  • (Integer, nil)

    Column count



14
15
16
# File 'lib/rich/layout.rb', line 14

def column_count
  @column_count
end

#equalBoolean (readonly)

Returns Equal width columns.

Returns:

  • (Boolean)

    Equal width columns



20
21
22
# File 'lib/rich/layout.rb', line 20

def equal
  @equal
end

#expandBoolean (readonly)

Returns Expand to fill width.

Returns:

  • (Boolean)

    Expand to fill width



23
24
25
# File 'lib/rich/layout.rb', line 23

def expand
  @expand
end

#itemsArray (readonly)

Returns Items to display.

Returns:

  • (Array)

    Items to display



11
12
13
# File 'lib/rich/layout.rb', line 11

def items
  @items
end

#paddingInteger (readonly)

Returns Padding between columns.

Returns:

  • (Integer)

    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

Parameters:

  • item (Object)

    Item to add

Returns:

  • (self)


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

Parameters:

  • max_width (Integer) (defaults to: 80)

    Maximum width

  • color_system (Symbol) (defaults to: ColorSystem::TRUECOLOR)

    Color system

Returns:

  • (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

Parameters:

  • max_width (Integer) (defaults to: 80)

    Maximum width

Returns:



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