Class: Clack::Prompts::GroupMultiselect

Inherits:
Core::Prompt show all
Includes:
Core::SelectionManager
Defined in:
lib/clack/prompts/group_multiselect.rb

Overview

Multiple-selection prompt with options organized into named groups.

Navigate with arrow keys or j/k. Toggle selection with Space. Groups can optionally be toggled as a whole when selectable_groups is enabled.

Examples:

Basic usage

features = Clack.group_multiselect(
  message: "Select features",
  options: [
    { label: "Frontend", options: %w[hotwire stimulus] },
    { label: "Backend", options: %w[sidekiq solid_queue] }
  ]
)

With selectable groups and spacing

features = Clack.group_multiselect(
  message: "Select features",
  options: [
    { label: "Frontend", options: [
      { value: "hotwire", label: "Hotwire" },
      { value: "stimulus", label: "Stimulus" }
    ]},
    { label: "Background", options: [
      { value: "sidekiq", label: "Sidekiq" },
      { value: "solid_queue", label: "Solid Queue" }
    ]}
  ],
  selectable_groups: true,
  group_spacing: 1
)

Constant Summary

Constants included from Core::SelectionManager

Core::SelectionManager::REQUIRED_ERROR

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::SelectionManager

#selected_labels, #toggle_value, #update_selection_value, #validate_selection

Methods inherited from Core::Prompt

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

Constructor Details

#initialize(message:, options:, initial_values: [], required: true, selectable_groups: false, group_spacing: 0, cursor_at: nil, **opts) ⇒ GroupMultiselect

Returns a new instance of GroupMultiselect.

Parameters:

  • message (String)

    the prompt message

  • options (Array<Hash>)

    groups, each with :label and :options (Array<Hash, String>)

  • initial_values (Array) (defaults to: [])

    values to pre-select

  • required (Boolean) (defaults to: true)

    require at least one selection (default: true)

  • selectable_groups (Boolean) (defaults to: false)

    allow toggling all options in a group at once (default: false)

  • group_spacing (Integer) (defaults to: 0)

    number of blank lines between groups (default: 0)

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

    value to position cursor at initially

  • opts (Hash)

    additional options passed to Core::Prompt



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/clack/prompts/group_multiselect.rb', line 47

def initialize(
  message:,
  options:,
  initial_values: [],
  required: true,
  selectable_groups: false,
  group_spacing: 0,
  cursor_at: nil,
  **opts
)
  if opts.key?(:initial_value)
    raise ArgumentError, "GroupMultiselect uses initial_values: (plural), not initial_value:"
  end
  super(message:, **opts)
  @groups = normalize_groups(options)
  @flat_items = build_flat_items
  valid_values = Set.new(@flat_items.select { |item| item.is_a?(Core::GroupOption) }.map(&:value))
  @selected = Set.new(initial_values) & valid_values
  @required = required
  @selectable_groups = selectable_groups
  @group_spacing = group_spacing
  @option_index = find_initial_cursor(cursor_at)
  update_value
end