Class: Clack::Prompts::Select

Inherits:
Core::Prompt show all
Includes:
Core::OptionsHelper
Defined in:
lib/clack/prompts/select.rb

Overview

Single-selection prompt from a list of options.

Navigate with arrow keys or j/k (vim-style). Press Enter to confirm. Supports disabled options, hints, and scrolling for long lists.

Options can be:

  • Strings: ‘[“a”, “b”, “c”]` (value and label are the same)

  • Hashes: ‘[“a”, label: “Option A”, hint: “details”, disabled: false]`

Examples:

Basic usage

choice = Clack.select(
  message: "Pick a color",
  options: %w[red green blue]
)

With rich options

db = Clack.select(
  message: "Choose database",
  options: [
    { value: "pg", label: "PostgreSQL", hint: "recommended" },
    { value: "mysql", label: "MySQL" },
    { value: "sqlite", label: "SQLite", disabled: true }
  ],
  initial_value: "pg",
  max_items: 5
)

Constant Summary

Constants inherited from Core::Prompt

Core::Prompt::MIN_TERMINAL_WIDTH

Instance Attribute Summary

Attributes inherited from Core::Prompt

#error_message, #state, #value, #warning_message

Instance Method Summary collapse

Methods included from Core::OptionsHelper

#find_initial_cursor, #find_next_enabled, #first_enabled_index, #move_selection, #navigable_items, normalize_option, #normalize_options, #update_scroll, #visible_options

Methods inherited from Core::Prompt

flush_resize, register, #request_redraw, #run, setup_signal_handler, unregister

Constructor Details

#initialize(message:, options:, initial_value: nil, max_items: nil, **opts) ⇒ Select

Returns a new instance of Select.

Parameters:

  • message (String)

    the prompt message

  • options (Array<Hash, String>)

    list of options (see class docs)

  • initial_value (Object, nil) (defaults to: nil)

    value of initially selected option

  • max_items (Integer, nil) (defaults to: nil)

    max visible items (enables scrolling)

  • opts (Hash)

    additional options passed to Core::Prompt



40
41
42
43
44
45
46
47
# File 'lib/clack/prompts/select.rb', line 40

def initialize(message:, options:, initial_value: nil, max_items: nil, **opts)
  super(message:, **opts)
  @options = normalize_options(options)
  @max_items = max_items
  @scroll_offset = 0
  @option_index = find_initial_cursor(initial_value)
  update_value
end