Module: AppArchetype::CLI::Prompts

Defined in:
lib/app_archetype/cli/prompts.rb

Overview

CLI output presenters

Constant Summary collapse

VAR_PROMPT_MESSAGE =

Variable prompt question. Asked when evaluating template variables

Returns:

  • (Proc)
lambda do |variable|
  "\nEnter value for `#{variable.name}` variable\n\n"\
  "DESCRIPTION: #{variable.description}\n"\
  "TYPE: #{variable.type}\n"\
  "DEFAULT: #{variable.default}"
end

Class Method Summary collapse

Class Method Details

.ask(message, validator: String, default: nil) ⇒ Object

Prompt for requesting user input.

A default can be provided in the event the user does not provide an answer.

Validator also performs type conversion by default it is a string

Parameters:

  • message (String)
  • default (Object) (defaults to: nil)
  • validator (Object|Lambda) (defaults to: String)

Returns:

  • (Object)


56
57
58
59
60
61
# File 'lib/app_archetype/cli/prompts.rb', line 56

def ask(message, validator: String, default: nil)
  resp = prompt.ask(message, validator)
  return default if !default.nil? && resp.to_s.empty?

  resp
end

.boolean_variable_prompt(variable) ⇒ Boolean

Prompt for boolean variable. This quizzes the user as to whether they want the variable set or not. The response is returned to the caller.

Parameters:

Returns:

  • (Boolean)


110
111
112
113
114
# File 'lib/app_archetype/cli/prompts.rb', line 110

def boolean_variable_prompt(variable)
  yes?(
    VAR_PROMPT_MESSAGE.call(variable)
  )
end

.delete_template(manifest) ⇒ Boolean

Y/N prompt to ensure user is sure they wish to delete the selected template

Parameters:

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/app_archetype/cli/prompts.rb', line 70

def delete_template(manifest)
  yes?(
    "Are you sure you want to delete `#{manifest.name}`?"
  )
end

.integer_variable_prompt(variable) ⇒ Integer

Prompt for integer. This quizzes the user for their choice and then attempts to convert it to an integer.

In the event a non integer value is entered, a RuntimeError is thrown.

Parameters:

Returns:

  • (Integer)


127
128
129
130
131
132
133
# File 'lib/app_archetype/cli/prompts.rb', line 127

def integer_variable_prompt(variable)
  ask(
    VAR_PROMPT_MESSAGE.call(variable),
    default: variable.default,
    validator: Integer
  )
end

.promptHighLine

Prompt returns a TTY prompt object for asking the user questions.

Returns:

  • (HighLine)


28
29
30
# File 'lib/app_archetype/cli/prompts.rb', line 28

def prompt
  HighLine.new
end

.string_variable_prompt(variable) ⇒ String

Prompt for a string. Asks user for input and returns it.

Parameters:

Returns:



143
144
145
146
147
148
# File 'lib/app_archetype/cli/prompts.rb', line 143

def string_variable_prompt(variable)
  ask(
    VAR_PROMPT_MESSAGE.call(variable),
    default: variable.default
  )
end

.variable_prompt_for(var) ⇒ Object

Returns a variable prompt based on the type of variable required. Once prompt has been executed, the response is returned to the caller.

When the value is set in the manifest, the set value is returned without a prompt.

For boolean and integer variables, the relevant prompt function is called.

By default the string variable prompt will be used.

Parameters:

Returns:

  • (Object)


93
94
95
96
97
98
99
# File 'lib/app_archetype/cli/prompts.rb', line 93

def variable_prompt_for(var)
  return var.value if var.value?
  return boolean_variable_prompt(var) if var.type == 'boolean'
  return integer_variable_prompt(var) if var.type == 'integer'

  string_variable_prompt(var)
end

.yes?(message) ⇒ Boolean

A yes/no prompt for asking the user a yes or no question.

Returns:

  • (Boolean)


37
38
39
# File 'lib/app_archetype/cli/prompts.rb', line 37

def yes?(message)
  prompt.ask("#{message} [Y/n]", String) { |input| input.strip == 'Y' }
end