class Spark::Prompt::Question(AnswerType, DefaultAnswerType)
- Spark::Prompt::Question(AnswerType, DefaultAnswerType)
- Reference
- Object
Overview
A Question is used to gather user input.
Direct Known Subclasses
Defined in:
spark/prompt/question.crConstructors
-
.new(prompt : Spark::Prompt, color : Symbol? = nil, style : Symbol? = nil, default : DefaultAnswerType? = nil, **options)
Creates a new Question.
Instance Method Summary
-
#call(message : String) : AnswerType?
Ask the question to the user.
-
#call(message : String, &)
Ask the question to the user, including a block with additional settings.
-
#color
Which color to use to colorize the Question output.
-
#default
Which default value to use for a blank/empty response to the Question.
-
#style
Which mode to use to stylize the Question output.
-
#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.
Constructor Detail
Creates a new Question.
Instance Method Detail
Ask the question to the user.
This handles adding the default text, a space before user input, and decoration.
It also gathers user input.
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.
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
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
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?"