Class: Clack::Prompts::Autocomplete

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

Overview

Type-to-filter autocomplete prompt.

Combines text input with a filtered option list. Type to filter, use arrow keys to navigate matches, Enter to select.

By default, filtering uses fuzzy matching across value, label, and hint fields, sorted by relevance score. Supply a custom filter proc to override this behavior.

Examples:

Basic usage

color = Clack.autocomplete(
  message: "Pick a color",
  options: %w[red orange yellow green blue indigo violet]
)

With placeholder

city = Clack.autocomplete(
  message: "Select city",
  options: cities,
  placeholder: "Type to search...",
  max_items: 10
)

Custom filter

Clack.autocomplete(
  message: "Select command",
  options: commands,
  filter: ->(opt, query) { opt.label.start_with?(query) }
)

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::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, filter: nil, **opts) ⇒ Autocomplete

Returns a new instance of Autocomplete.

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

  • 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



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/clack/prompts/autocomplete.rb', line 47

def initialize(message:, options:, max_items: 5, placeholder: nil, filter: nil, **opts)
  super(message:, **opts)
  @all_options = normalize_options(options)
  @max_items = max_items
  @placeholder = placeholder
  @filter = filter
  @search_text = ""
  @cursor = 0
  @option_index = 0
  @scroll_offset = 0
  update_filtered
end