module Crafter::CLIHelpers

Public Instance Methods

ask(message, valid: [], bold: true, force_new_line: false) click to toggle source

Asks for information from the user. @param message [String] Message to provide. @param valid: [] [Array<String>] Valid options.

An empty array will be ignored.

@return [String] User response.

# File lib/crafter/cli_helpers.rb, line 23
def ask(message, valid: [], bold: true, force_new_line: false)
  return loop_valid(message, valid) unless valid.empty?
  stringify_message!(message); arrow(colors: :blue)
  message << "\n    > "
  low_say(message, bold: bold, force_new_line: force_new_line)
  STDIN.gets
end
from_to_message(from, to, source: '') click to toggle source
# File lib/crafter/cli_helpers.rb, line 36
def from_to_message(from, to, source: '')
  "(§#{(source.dup << ' ').colorize(:default) unless source.empty?}§"\
  "§#{from.underline}§ -> §#{to.underline}§)"
end
say(*args, superify: false, bold: true, force_new_line: false) click to toggle source

Say something! Use § characters around parts of text you have colorized @param *args [String] Arguments to pass to low_say @param bold: true [Boolean] Enable or disable auto-bolding @param force_new_line: false [Boolean] Force new line?

Calls superclass method
# File lib/crafter/cli_helpers.rb, line 10
def say(*args, superify: false, bold: true, force_new_line: false)
  return super *args if superify
  arrow(colors: :blue)
  low_say(*args, bold: bold, force_new_line: force_new_line)
end
say_error(message) click to toggle source
# File lib/crafter/cli_helpers.rb, line 31
def say_error(message)
  arrow(colors: :red)
  low_say(message, colors: :red)
end

Private Instance Methods

arrow(colors: []) click to toggle source
# File lib/crafter/cli_helpers.rb, line 74
def arrow(colors: [])
  low_say('==> ', bold: false, colors: colors)
end
conditional_arg!(args_array, arg) click to toggle source

Adds argument to array if it is truthy

# File lib/crafter/cli_helpers.rb, line 53
def conditional_arg!(args_array, arg)
  args_array << arg if arg
end
generate_bold_text(text) click to toggle source
# File lib/crafter/cli_helpers.rb, line 64
def generate_bold_text(text)
  if text.scan('§').length % 2 == 1
    raise "There must be an even number of §'s in the message: #{text}"
  end

  text.split('§').map do |string|
    string.colorized? ? string : string.bold
  end.join
end
loop_valid(message, valid) click to toggle source
# File lib/crafter/cli_helpers.rb, line 43
def loop_valid(message, valid)
  valid.map!(&:to_s)
  loop do
    response = ask("#{message} #{valid.to_s.delete('"')}")
    return response if valid.include? response.downcase.strip
    say_error('Invalid choice')
  end
end
low_say(message, colors: [], bold: true, force_new_line: nil) click to toggle source
# File lib/crafter/cli_helpers.rb, line 57
def low_say(message, colors: [], bold: true, force_new_line: nil)
  stringify_message!(message)
  message = generate_bold_text(message) if bold
  conditional_arg!(args = [], force_new_line)
  say(message, colors, *args, superify: true)
end
stringify_message!(message) click to toggle source
# File lib/crafter/cli_helpers.rb, line 78
def stringify_message!(message)
  message = [message].flatten.join("\n    ")
end