class Spark::Prompt

Overview

Spark::Prompt allows you to interact with users to gather input or display messages

Defined in:

spark/prompt.cr
spark/prompt/confirmation_question.cr
spark/prompt/question.cr
spark/prompt/statement.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(input : IO::FileDescriptor = STDIN, output : IO::FileDescriptor = STDOUT) #

Initialize a new Spark::Prompt


[View source]

Instance Method Detail

def ask(message : String, **options) : String #

Ask the user a question.

Example:

prompt = Spark::Prompt.new
prompt.ask("What is your name?") # => "What is your name?"

[View source]
def ask(message : String, **options, &block : Spark::Prompt::Question(String, String) -> _) : String #

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?"

[View source]
def decorate(string : String, color : Symbol? = nil, style : Symbol? = nil) #

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"

[View source]
def newline #

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"

[View source]
def no?(message : String, **options) : Bool #

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

[View source]
def say(message : String = "", **options) #

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"

[View source]
def yes?(message : String, **options) : Bool #

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

[View source]