Class: SwarmCLI::V3::Dropdown
- Inherits:
-
Object
- Object
- SwarmCLI::V3::Dropdown
- Defined in:
- lib/swarm_cli/v3/dropdown.rb
Overview
Reusable dropdown component for displaying and navigating a list of items.
The Dropdown component provides a simple, modular interface for list selection without any knowledge of file systems, autocomplete, or terminal rendering. It manages state (items, selection) and provides methods for navigation and rendering.
Instance Attribute Summary collapse
-
#items ⇒ Array<String>
readonly
The list of items in the dropdown.
-
#selected_index ⇒ Integer
readonly
The currently selected index (0-based).
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Check if the dropdown has no items.
-
#first_item ⇒ String
Get the first item in the list.
-
#initialize(items:, max_visible: 5) ⇒ Dropdown
constructor
Create a new dropdown with the given items.
-
#render ⇒ Array<String>
Render the dropdown as a numbered list with highlighting.
-
#select_next ⇒ void
Move selection to the next item (wraps to top).
-
#select_previous ⇒ void
Move selection to the previous item (wraps to bottom).
-
#selected_item ⇒ String
Get the currently selected item.
Constructor Details
#initialize(items:, max_visible: 5) ⇒ Dropdown
Create a new dropdown with the given items.
37 38 39 40 41 42 43 |
# File 'lib/swarm_cli/v3/dropdown.rb', line 37 def initialize(items:, max_visible: 5) raise ArgumentError, "items cannot be empty" if items.empty? @items = items @selected_index = 0 @max_visible = max_visible end |
Instance Attribute Details
#items ⇒ Array<String> (readonly)
Returns the list of items in the dropdown.
23 24 25 |
# File 'lib/swarm_cli/v3/dropdown.rb', line 23 def items @items end |
#selected_index ⇒ Integer (readonly)
Returns the currently selected index (0-based).
26 27 28 |
# File 'lib/swarm_cli/v3/dropdown.rb', line 26 def selected_index @selected_index end |
Instance Method Details
#empty? ⇒ Boolean
This should never be true due to the constructor validation, but is provided for completeness.
Check if the dropdown has no items.
125 126 127 |
# File 'lib/swarm_cli/v3/dropdown.rb', line 125 def empty? @items.empty? end |
#first_item ⇒ String
Get the first item in the list.
86 87 88 |
# File 'lib/swarm_cli/v3/dropdown.rb', line 86 def first_item @items.first end |
#render ⇒ Array<String>
Render the dropdown as a numbered list with highlighting.
The selected item is rendered with reverse video highlighting. Non-selected items are rendered with dim styling. Only the first max_visible items are included.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/swarm_cli/v3/dropdown.rb', line 103 def render visible_items = @items.first(@max_visible) visible_items.map.with_index do |item, idx| prefix = "#{idx + 1}. " if idx == @selected_index ANSIColors.reverse(prefix + item) else ANSIColors.dim(prefix + item) end end end |
#select_next ⇒ void
This method returns an undefined value.
Move selection to the next item (wraps to top).
65 66 67 |
# File 'lib/swarm_cli/v3/dropdown.rb', line 65 def select_next @selected_index = (@selected_index + 1) % @items.size end |
#select_previous ⇒ void
This method returns an undefined value.
Move selection to the previous item (wraps to bottom).
53 54 55 |
# File 'lib/swarm_cli/v3/dropdown.rb', line 53 def select_previous @selected_index = (@selected_index - 1) % @items.size end |
#selected_item ⇒ String
Get the currently selected item.
76 77 78 |
# File 'lib/swarm_cli/v3/dropdown.rb', line 76 def selected_item @items[@selected_index] end |