Class: Clack::Prompts::Confirm

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

Overview

Yes/No confirmation prompt.

Displays a toggle between two options. Navigate with arrow keys, j/k, or press y/n to select directly.

Examples:

Basic usage

proceed = Clack.confirm(message: "Continue?")

With custom labels

deploy = Clack.confirm(
  message: "Deploy to production?",
  active: "Yes, ship it!",
  inactive: "No, abort",
  initial_value: false
)

Stacked layout for long labels

Clack.confirm(
  message: "Overwrite ~/.zshrc?",
  active: "Yes, back it up and replace it",
  inactive: "No, keep my existing file",
  vertical: true
)

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:, active: "Yes", inactive: "No", initial_value: true, vertical: false, **opts) ⇒ Confirm

Returns a new instance of Confirm.

Parameters:

  • message (String)

    the prompt message

  • active (String) (defaults to: "Yes")

    label for the "yes" option (default: "Yes")

  • inactive (String) (defaults to: "No")

    label for the "no" option (default: "No")

  • initial_value (Object) (defaults to: true)

    initial selection (default: true); coerced to a Boolean, so nil and false select "no" and anything else selects "yes"

  • vertical (Boolean) (defaults to: false)

    render the two options on separate lines (default: false)

  • opts (Hash)

    additional options passed to Core::Prompt



37
38
39
40
41
42
43
44
45
46
# File 'lib/clack/prompts/confirm.rb', line 37

def initialize(message:, active: "Yes", inactive: "No", initial_value: true, vertical: false, **opts)
  super(message:, **opts)
  @active_label = active
  @inactive_label = inactive
  @vertical = vertical
  # Coerce like upstream (`!!opts.initialValue`) so the prompt always
  # returns a real Boolean, even when the caller passes nil from ENV or
  # an options hash.
  @value = initial_value ? true : false
end