Class: Clack::Prompts::SelectKey

Inherits:
Core::Prompt show all
Defined in:
lib/clack/prompts/select_key.rb

Overview

Quick selection via keyboard shortcuts.

Each option has an associated key. Pressing that key immediately selects the option and submits.

Options format:

  • ‘{ value: “x”, label: “Do X”, key: “x” }` - explicit key

  • ‘{ value: “create”, label: “Create” }` - key defaults to first char

Examples:

Basic usage

action = Clack.select_key(
  message: "What to do?",
  options: [
    { value: "create", label: "Create new", key: "c" },
    { value: "open", label: "Open existing", key: "o" },
    { value: "quit", label: "Quit", key: "q" }
  ]
)

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 inherited from Core::Prompt

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

Constructor Details

#initialize(message:, options:, **opts) ⇒ SelectKey

Returns a new instance of SelectKey.

Parameters:

  • message (String)

    the prompt message

  • options (Array<Hash>)

    options with :value, :label, and optionally :key, :hint

  • opts (Hash)

    additional options passed to Core::Prompt



28
29
30
31
# File 'lib/clack/prompts/select_key.rb', line 28

def initialize(message:, options:, **opts)
  super(message:, **opts)
  @options = normalize_options(options)
end