class Spark::Prompt::Question(AnswerType, DefaultAnswerType)

Overview

A Question is used to gather user input.

Direct Known Subclasses

Defined in:

spark/prompt/question.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(prompt : Spark::Prompt, color : Symbol? = nil, style : Symbol? = nil, default : DefaultAnswerType? = nil, **options) #

Creates a new Question.


[View source]

Instance Method Detail

def call(message : String) : AnswerType? #

Ask the question to the user.

This handles adding the default text, a space before user input, and decoration.

It also gathers user input.


[View source]
def call(message : String, &) #

Ask the question to the user, including a block with additional settings.

We optionally allow the ability to re-collect invalid input.

If @retry_on_validation_failure is true (set via #validate), we collect input until it is valid. If @retry_on_validation_failure is false (set via #validate), we collect input once and return nil if it is invalid.


[View source]
def color #

Which color to use to colorize the Question output.

See https://crystal-lang.org/api/latest/Colorize.html

Example:

:default
:black
:red
:green
:yellow
:blue

[View source]
def default #

Which default value to use for a blank/empty response to the Question.


[View source]
def style #

Which mode to use to stylize the Question output.

See https://crystal-lang.org/api/latest/Colorize.html

Example:

:bold
:bright
:dim
:underline
:blink
:reverse
:hidden

[View source]
def validate(validation : Regex, error_message : String? = nil, *, retry_on_failure : Bool = false) #

Provide validation for a Spark::Prompt::Question, optionally overriding the default error message.

Example with the default message:

Question.new(Spark::Prompt.new).call("What is your name") do |question|
  question.validate(/LuckyCasts/)
end
# => "What is your name?"

Example with a custom message:

Question.new(Spark::Prompt.new).call("What is your name") do |question|
  question.validate(/LuckyCasts/, "Your name must be 'LuckyCasts'")
end
# => "What is your name?"

Example that retries on validation failure:

Question.new(Spark::Prompt.new).call("What is your name") do |question|
  question.validate(/LuckyCasts/, "Your name must be 'LuckyCasts'. Please enter a valid value.", retry_on_failure: true)
end
# => "What is your name?"

[View source]