Class: Potty::Widgets::ColoredFieldsItem

Inherits:
ActionItem show all
Defined in:
lib/potty/widgets/colored_fields_item.rb

Overview

Generic multi-color list item Takes an array of segments, each with text and color, and renders them inline

Usage:

item = ColoredFieldsItem.new(
  fields: [
    { text: "[M]", color: :success },
    { text: " " },
    { text: "[0]", color: :dim },
    { text: " /path/to/backup", color: :normal }
  ],
  value: some_object
) { |item| handle_activation(item) }

Instance Attribute Summary collapse

Attributes inherited from ListItem

#color, #text, #value

Instance Method Summary collapse

Methods inherited from ActionItem

#activate

Methods inherited from ListItem

#activate, #disabled?, #display_text, #handle_key

Constructor Details

#initialize(fields:, value: nil, &action) ⇒ ColoredFieldsItem

Returns a new instance of ColoredFieldsItem.



24
25
26
27
# File 'lib/potty/widgets/colored_fields_item.rb', line 24

def initialize(fields:, value: nil, &action)
  @fields = fields
  super("", value: value, &action)
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



22
23
24
# File 'lib/potty/widgets/colored_fields_item.rb', line 22

def fields
  @fields
end

Instance Method Details

#render_custom(window, theme, max_width) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/potty/widgets/colored_fields_item.rb', line 29

def render_custom(window, theme, max_width)
  remaining = max_width
  @fields.each do |field|
    break if remaining <= 0

    text = field[:text] || ""
    color = field[:color]
    bold = field[:bold] || false

    text = text[0, remaining]

    if color
      attr = bold ? theme.attr(color, bold: true) : theme[color]
      window.attron(attr) do
        window.addstr(text)
      end
    else
      window.addstr(text)
    end

    remaining -= text.length
  end

  true
end