Class: SwarmCLI::V3::Dropdown

Inherits:
Object
  • Object
show all
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.

Examples:

Basic usage

dropdown = Dropdown.new(items: ["option1", "option2", "option3"])
dropdown.select_next          # Move down
dropdown.selected_item        # => "option2"
dropdown.render               # => ["1. option1", "2. \e[7moption2\e[0m", ...]

Limited visibility

dropdown = Dropdown.new(items: (1..100).to_a, max_visible: 5)
dropdown.render.size          # => 5 (only shows first 5 items)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items:, max_visible: 5) ⇒ Dropdown

Create a new dropdown with the given items.

Examples:

dropdown = Dropdown.new(items: ["file1.rb", "file2.rb"], max_visible: 5)

Parameters:

  • items (Array<String>)

    the list of items to display

  • max_visible (Integer) (defaults to: 5)

    maximum number of items to show at once

Raises:

  • (ArgumentError)

    if items is empty



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

#itemsArray<String> (readonly)

Returns the list of items in the dropdown.

Returns:

  • (Array<String>)

    the list of items in the dropdown



23
24
25
# File 'lib/swarm_cli/v3/dropdown.rb', line 23

def items
  @items
end

#selected_indexInteger (readonly)

Returns the currently selected index (0-based).

Returns:

  • (Integer)

    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

Note:

This should never be true due to the constructor validation, but is provided for completeness.

Check if the dropdown has no items.

Examples:

dropdown.empty?  # => false

Returns:

  • (Boolean)

    true if items array is empty



125
126
127
# File 'lib/swarm_cli/v3/dropdown.rb', line 125

def empty?
  @items.empty?
end

#first_itemString

Get the first item in the list.

Examples:

dropdown.first_item  # => "option1"

Returns:

  • (String)

    the first item



86
87
88
# File 'lib/swarm_cli/v3/dropdown.rb', line 86

def first_item
  @items.first
end

#renderArray<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.

Examples:

dropdown = Dropdown.new(items: ["a", "b", "c"], max_visible: 5)
dropdown.select_next
dropdown.render
# => ["1. \e[2ma\e[0m", "2. \e[7mb\e[0m", "3. \e[2mc\e[0m"]

Returns:

  • (Array<String>)

    array of formatted lines



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_nextvoid

This method returns an undefined value.

Move selection to the next item (wraps to top).

Examples:

dropdown.selected_index  # => 1
dropdown.select_next
dropdown.selected_index  # => 2


65
66
67
# File 'lib/swarm_cli/v3/dropdown.rb', line 65

def select_next
  @selected_index = (@selected_index + 1) % @items.size
end

#select_previousvoid

This method returns an undefined value.

Move selection to the previous item (wraps to bottom).

Examples:

dropdown.selected_index  # => 2
dropdown.select_previous
dropdown.selected_index  # => 1


53
54
55
# File 'lib/swarm_cli/v3/dropdown.rb', line 53

def select_previous
  @selected_index = (@selected_index - 1) % @items.size
end

#selected_itemString

Get the currently selected item.

Examples:

dropdown.select_next
dropdown.selected_item  # => "option2"

Returns:

  • (String)

    the selected item



76
77
78
# File 'lib/swarm_cli/v3/dropdown.rb', line 76

def selected_item
  @items[@selected_index]
end