class Toys::Utils::Terminal

def confirm(prompt = "Proceed? ", *styles, default: nil)

Returns:
  • (Boolean) -

Parameters:
  • default (Boolean, nil) -- Default value, or `nil` for no default.
  • styles (Symbol, String, Array...) -- Styles to apply to the
  • prompt (String) -- Prompt string. Defaults to `"Proceed?"`.
def confirm(prompt = "Proceed? ", *styles, default: nil)
  default_val, trailing_text =
    case default
    when true
      ["y", "(Y/n)"]
    when false
      ["n", "(y/N)"]
    else
      [nil, "(y/n)"]
    end
  resp = ask(prompt, *styles, default: default_val, trailing_text: trailing_text)
  return true if resp =~ /^y/i
  return false if resp =~ /^n/i
  if resp.nil? && default.nil?
    raise TerminalError, "Cannot confirm because the input stream is at eof."
  end
  if !resp.strip.empty? || default.nil?
    if input.nil?
      raise TerminalError, "Cannot confirm because there is no input stream."
    end
    confirm('Please answer "y" or "n"', default: default)
  else
    default
  end
end