Class: Clack::Prompts::AutocompleteMultiselect

Inherits:
Core::Prompt
  • Object
show all
Includes:
Core::OptionsHelper, Core::SelectionManager, Core::TextInputHelper
Defined in:
lib/clack/prompts/autocomplete_multiselect.rb

Overview

Type-to-filter autocomplete with multiple selection.

Combines text input filtering with checkbox-style selection. Type to filter, Space to toggle, Enter to confirm.

Unlike Multiselect, the ā€˜a’ (select all) and ā€˜i’ (invert) shortcuts are not available because those characters are needed for the search field. Similarly, vim-style j/k navigation is disabled in favor of typing.

Examples:

Basic usage

colors = Clack.autocomplete_multiselect(
  message: "Pick colors",
  options: %w[red orange yellow green blue]
)

With options

tags = Clack.autocomplete_multiselect(
  message: "Select tags",
  options: all_tags,
  placeholder: "Type to filter...",
  required: true,
  initial_values: ["important"]
)

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 included from Core::TextInputHelper

#current_placeholder, #format_placeholder_with_cursor, #input_display, #placeholder_display, #value_with_cursor

Methods included from Core::OptionsHelper

#find_initial_cursor, #find_next_enabled, #first_enabled_index, #move_cursor, #move_selection, 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:, max_items: 5, placeholder: nil, required: true, initial_values: [], filter: nil, **opts) ⇒ AutocompleteMultiselect

Returns a new instance of AutocompleteMultiselect.

Parameters:

  • message (String)

    the prompt message

  • options (Array<Hash, String>)

    list of options to filter

  • max_items (Integer) (defaults to: 5)

    max visible options (default: 5)

  • placeholder (String, nil) (defaults to: nil)

    placeholder text when empty

  • required (Boolean) (defaults to: true)

    require at least one selection (default: true)

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

    values to pre-select

  • filter (Proc, nil) (defaults to: nil)

    custom filter proc receiving (option_hash, query_string) and returning true/false. When nil, the default fuzzy matching across label, value, and hint is used.

  • opts (Hash)

    additional options passed to Core::Prompt



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/clack/prompts/autocomplete_multiselect.rb', line 44

def initialize(message:, options:, max_items: 5, placeholder: nil, required: true, initial_values: [], filter: nil, **opts)
  if opts.key?(:initial_value)
    raise ArgumentError, "AutocompleteMultiselect uses initial_values: (plural), not initial_value:"
  end
  super(message:, **opts)
  @all_options = normalize_options(options)
  @max_items = max_items
  @placeholder = placeholder
  @required = required
  @filter = filter
  @search_text = ""
  @cursor = 0
  @option_index = 0
  @scroll_offset = 0
  valid_values = Set.new(@all_options.map { |o| o.value })
  @selected = Set.new(initial_values) & valid_values
  update_filtered
end