class Spark::Prompt
- Spark::Prompt
- Reference
- Object
Overview
Spark::Prompt allows you to interact with users to gather input or display messages
Defined in:
spark/prompt.crspark/prompt/confirmation_question.cr
spark/prompt/question.cr
spark/prompt/statement.cr
Constructors
-
.new(input : IO::FileDescriptor = STDIN, output : IO::FileDescriptor = STDOUT)
Initialize a new Spark::Prompt
Instance Method Summary
-
#ask(message : String, **options) : String
Ask the user a question.
-
#ask(message : String, **options, &block : Spark::Prompt::Question(String, String) -> _) : String
Ask the user a question with optional validation.
-
#decorate(string : String, color : Symbol | Nil = nil, style : Symbol | Nil = nil)
Color and stylize a given string.
-
#newline
Output an empty line to the user's prompt.
-
#no?(message : String, **options) : Bool
Ask the user a yes/no question, where the default is "No".
-
#say(message : String = "", **options)
Output some text to a user, with an optional color and style.
-
#yes?(message : String, **options) : Bool
Ask the user a yes/no question, where the default is "Yes".
Constructor Detail
Initialize a new Spark::Prompt
Instance Method Detail
Ask the user a question.
Example:
prompt = Spark::Prompt.new
prompt.ask("What is your name?") # => "What is your name?"
Ask the user a question with optional validation.
Example with validation:
prompt = Spark::Prompt.new
prompt.ask("What is your name?") do |question|
question.validate(/LuckyCasts/, error_message: "Name must be 'LuckyCasts'")
end
# => "What is your name?"
Color and stylize a given string.
Example:
prompt = Spark::Prompt.new
prompt.decorate("This is an example", color: :green, style: :bold) # => "\e[32;1mHello, there!\e[0m\n"
Output an empty line to the user's prompt.
Example:
prompt = Spark::Prompt.new
prompt.say("This is an example") # => "This is an example\n"
prompt.newline # => "\n"
Ask the user a yes/no question, where the default is "No".
Example:
prompt = Spark::Prompt.new
if prompt.no? "Are you feeling happy today?"
prompt.say "I'm sorry to hear that."
else
prompt.say "Then it's going to be a great day!"
end
Output some text to a user, with an optional color and style.
Plain example:
prompt = Spark::Prompt.new
prompt.say("This is an example") # => "This is an example\n"
Without a newline:
prompt = Spark::Prompt.new
prompt.say("This is an example", newline: false) # => "This is an example"
With color and style (see Spark::Prompt#decorate):
prompt = Spark::Prompt.new
prompt.say("This is an example", color: :green, style: :bold) # => "\e[32;1mHello, there!\e[0m\n"
Ask the user a yes/no question, where the default is "Yes".
Example:
prompt = Spark::Prompt.new
if prompt.yes? "Did you tell me the truth?"
prompt.say "Great! Thank you, #{user_name}.", color: :green
else
prompt.say "Shame on you!"
end