Class: Chamomile::Layout::List

Inherits:
Base
  • Object
show all
Defined in:
lib/chamomile/layout/list.rb

Instance Method Summary collapse

Constructor Details

#initialize(items, cursor: 0, height: nil, selected_color: "#7d56f4", dim_color: "#888888") ⇒ List

Returns a new instance of List.



6
7
8
9
10
11
12
13
# File 'lib/chamomile/layout/list.rb', line 6

def initialize(items, cursor: 0, height: nil,
               selected_color: "#7d56f4", dim_color: "#888888")
  @items          = Array(items)
  @cursor         = cursor
  @height         = height
  @selected_color = selected_color
  @dim_color      = dim_color
end

Instance Method Details

#render(width:, height:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/chamomile/layout/list.rb', line 15

def render(width:, height:)
  display_height = @height || height
  visible_items  = @items.first(display_height)

  lines = visible_items.each_with_index.map do |item, i|
    text = item.to_s.ljust(width)
    if i == @cursor
      Chamomile::Style.new.foreground(@selected_color).reverse.render(text)
    else
      Chamomile::Style.new.foreground(@dim_color).render(text)
    end
  end

  lines.join("\n")
end