class Prompt

Class for managing Command Line printing and user input

Public Class Methods

ask(string, valid_responses: nil) click to toggle source

Public - Asks the user a question in pink and waits for a response

@param string - String (question) to print

@return - Response from the user

# File lib/helpers/prompt.rb, line 12
def self.ask(string, valid_responses: nil)
  puts colorize(:pink, string)
  response = gets.chomp
  if valid_responses
    while !valid_responses.include?(response)
      response = ask("#{response} is not valid. #{string}")
    end
  end
  response
end
clear() click to toggle source

Public - Clears the command line

# File lib/helpers/prompt.rb, line 24
def self.clear
  system "clear"
end
result(string) click to toggle source

Public - Accepts a string and prints the string in blue

@param string - String to print

@return - Prints out a String in the color blue surrounded by empty lines

# File lib/helpers/prompt.rb, line 38
def self.result(string)
  puts "", colorize(:blue, string), ""
end
say(string) click to toggle source

Public - Accepts a string and prints the string in red

@param string - String to print

@return - Prints out a String in the color red with an extra line after

# File lib/helpers/prompt.rb, line 47
def self.say(string)
  puts colorize(:red, string), ""
end

Private Class Methods

colorize(color, string) click to toggle source

Public - Converts a string into the specified color

@param string - String to print @param color - Color to change the string to

@return - String in the color specified

# File lib/helpers/prompt.rb, line 59
def self.colorize(color, string)
  colors = {
    red: 31,
    green: 32,
    yellow: 33,
    blue: 34,
    pink: 35,
    light_blue: 36
  }

  "\e[#{colors[color]}m#{string}\e[0m"
end