Class: TTY::Prompt::Question
- Inherits:
-
Object
- Object
- TTY::Prompt::Question
- Includes:
- Checks
- Defined in:
- lib/tty/prompt/question.rb,
lib/tty/prompt/question/checks.rb,
lib/tty/prompt/question/modifier.rb,
lib/tty/prompt/question/validation.rb
Overview
A class responsible for gathering user input
Direct Known Subclasses
Defined Under Namespace
Modules: Checks Classes: Modifier, Validation
Constant Summary collapse
- UndefinedSetting =
Class.new do def to_s "undefined" end alias inspect to_s end
Instance Attribute Summary collapse
-
#message ⇒ Object
readonly
Store question message.
-
#messages ⇒ Object
readonly
Stores all the error messages displayed to user The currently supported messages are: * :range? * :required? * :valid?.
- #modifier ⇒ Object readonly
- #validation ⇒ Object readonly
Instance Method Summary collapse
-
#call(message, &block) ⇒ self
Call the question.
-
#convert(value) ⇒ Object
Specify answer conversion.
-
#convert? ⇒ Boolean
Check if conversion is set.
-
#convert_result(value) ⇒ Object
private
Convert value to expected type.
-
#default(value = (not_set = true)) ⇒ Object
Set default value.
-
#default? ⇒ Boolean
Check if default value is set.
-
#echo(value = nil) ⇒ Object
(also: #echo?)
Turn terminal echo on or off.
-
#in(value = (not_set = true), message = nil) ⇒ Object
Set expected range of values.
-
#in? ⇒ Boolean
Check if range is set.
-
#initialize(prompt, options = {}) ⇒ Question
constructor
Initialize a Question.
-
#inspect ⇒ Object
String representation of this question.
-
#message_for(name, tokens = nil) ⇒ Array[String]
private
Retrieve message based on the key.
-
#modify(*rules) ⇒ Object
Modify string according to the rule given.
-
#process_input(question) ⇒ Object
private
Decide how to handle input from user.
-
#raw(value = nil) ⇒ Object
(also: #raw?)
Turn raw mode on or off.
-
#read_input(question) ⇒ Object
private
Process input.
-
#refresh(lines) ⇒ String
private
Determine area of the screen to clear.
-
#render ⇒ Object
private
Read answer and convert to type.
-
#render_error(errors) ⇒ String
private
Handle error condition.
-
#render_question ⇒ String
private
Render question.
-
#required(value = (not_set = true), message = nil) ⇒ Boolean
(also: #required?)
Ensure that passed argument is present or not.
- #to_s ⇒ Object
-
#validate(value = nil, message = nil, &block) ⇒ Question
Set validation rule for an argument.
- #validation? ⇒ Boolean
Constructor Details
#initialize(prompt, options = {}) ⇒ Question
Initialize a Question
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/tty/prompt/question.rb', line 37 def initialize(prompt, = {}) @prompt = prompt @prefix = .fetch(:prefix) { @prompt.prefix } @default = .fetch(:default) { UndefinedSetting } @required = .fetch(:required) { false } @echo = .fetch(:echo) { true } @in = .fetch(:in) { UndefinedSetting } @modifier = .fetch(:modifier) { [] } @validation = .fetch(:validation) { UndefinedSetting } @convert = .fetch(:convert) { UndefinedSetting } @active_color = .fetch(:active_color) { @prompt.active_color } @help_color = .fetch(:help_color) { @prompt.help_color } @error_color = .fetch(:error_color) { :red } @messages = Utils.deep_copy(.fetch(:messages) { { } }) @done = false @input = nil @evaluator = Evaluator.new(self) @evaluator << CheckRequired @evaluator << CheckDefault @evaluator << CheckRange @evaluator << CheckValidation @evaluator << CheckModifier end |
Instance Attribute Details
#message ⇒ Object (readonly)
Store question message
28 29 30 |
# File 'lib/tty/prompt/question.rb', line 28 def @message end |
#messages ⇒ Object (readonly)
Stores all the error messages displayed to user The currently supported messages are:
* :range?
* :required?
* :valid?
68 69 70 |
# File 'lib/tty/prompt/question.rb', line 68 def @messages end |
#modifier ⇒ Object (readonly)
30 31 32 |
# File 'lib/tty/prompt/question.rb', line 30 def modifier @modifier end |
#validation ⇒ Object (readonly)
32 33 34 |
# File 'lib/tty/prompt/question.rb', line 32 def validation @validation end |
Instance Method Details
#call(message, &block) ⇒ self
Call the question
97 98 99 100 101 102 |
# File 'lib/tty/prompt/question.rb', line 97 def call(, &block) return if Utils.blank?() @message = block.call(self) if block render end |
#convert(value) ⇒ Object
Specify answer conversion
212 213 214 |
# File 'lib/tty/prompt/question.rb', line 212 def convert(value) @convert = value end |
#convert? ⇒ Boolean
Check if conversion is set
221 222 223 |
# File 'lib/tty/prompt/question.rb', line 221 def convert? @convert != UndefinedSetting end |
#convert_result(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert value to expected type
201 202 203 204 205 206 207 |
# File 'lib/tty/prompt/question.rb', line 201 def convert_result(value) if convert? & !Utils.blank?(value) Converters.convert(@convert, value) else value end end |
#default(value = (not_set = true)) ⇒ Object
Set default value.
228 229 230 231 |
# File 'lib/tty/prompt/question.rb', line 228 def default(value = (not_set = true)) return @default if not_set @default = value end |
#default? ⇒ Boolean
Check if default value is set
238 239 240 |
# File 'lib/tty/prompt/question.rb', line 238 def default? @default != UndefinedSetting end |
#echo(value = nil) ⇒ Object Also known as: echo?
Turn terminal echo on or off. This is used to secure the display so that the entered characters are not echoed back to the screen.
283 284 285 286 |
# File 'lib/tty/prompt/question.rb', line 283 def echo(value = nil) return @echo if value.nil? @echo = value end |
#in(value = (not_set = true), message = nil) ⇒ Object
Set expected range of values
303 304 305 306 307 308 309 310 |
# File 'lib/tty/prompt/question.rb', line 303 def in(value = (not_set = true), = nil) [:range?] = if if in? && !@in.is_a?(Range) @in = Converters.convert(:range, @in) end return @in if not_set @in = Converters.convert(:range, value) end |
#in? ⇒ Boolean
Check if range is set
317 318 319 |
# File 'lib/tty/prompt/question.rb', line 317 def in? @in != UndefinedSetting end |
#inspect ⇒ Object
String representation of this question
328 329 330 |
# File 'lib/tty/prompt/question.rb', line 328 def inspect "#<#{self.class.name} @message=#{}, @input=#{@input}>" end |
#message_for(name, tokens = nil) ⇒ Array[String]
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieve message based on the key
81 82 83 84 85 86 87 88 |
# File 'lib/tty/prompt/question.rb', line 81 def (name, tokens = nil) template = @messages[name] if template && !template.match(/\%\{/).nil? [template % tokens] else [template || ''] end end |
#modify(*rules) ⇒ Object
Modify string according to the rule given.
275 276 277 |
# File 'lib/tty/prompt/question.rb', line 275 def modify(*rules) @modifier = rules end |
#process_input(question) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Decide how to handle input from user
146 147 148 149 150 151 152 |
# File 'lib/tty/prompt/question.rb', line 146 def process_input(question) @input = read_input(question) if Utils.blank?(@input) @input = default? ? default : nil end @evaluator.(@input) end |
#raw(value = nil) ⇒ Object Also known as: raw?
Turn raw mode on or off. This enables character-based input.
292 293 294 295 |
# File 'lib/tty/prompt/question.rb', line 292 def raw(value = nil) return @raw if value.nil? @raw = value end |
#read_input(question) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Process input
157 158 159 |
# File 'lib/tty/prompt/question.rb', line 157 def read_input(question) @prompt.read_line(question, echo: echo).chomp end |
#refresh(lines) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determine area of the screen to clear
182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/tty/prompt/question.rb', line 182 def refresh(lines) output = '' if @done if @errors.count.zero? && @echo output << @prompt.cursor.up(lines) else lines += @errors.count end else output << @prompt.cursor.up(lines) end output + @prompt.clear_lines(lines) end |
#render ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Read answer and convert to type
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/tty/prompt/question.rb', line 107 def render @errors = [] until @done question = render_question @prompt.print(question) result = process_input(question) if result.failure? @errors = result.errors @prompt.print(render_error(result.errors)) else @done = true end @prompt.print(refresh(question.lines.count)) end @prompt.print(render_question) convert_result(result.value) end |
#render_error(errors) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Handle error condition
166 167 168 169 170 171 172 |
# File 'lib/tty/prompt/question.rb', line 166 def render_error(errors) errors.reduce('') do |acc, err| newline = (@echo ? '' : "\n") acc << newline + @prompt.decorate('>>', :red) + ' ' + err acc end end |
#render_question ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Render question
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/tty/prompt/question.rb', line 130 def render_question header = "#{@prefix}#{} " if !echo? header elsif @done header += @prompt.decorate("#{@input}", @active_color) elsif default? && !Utils.blank?(@default) header += @prompt.decorate("(#{default})", @help_color) + ' ' end header << "\n" if @done header end |
#required(value = (not_set = true), message = nil) ⇒ Boolean Also known as: required?
Ensure that passed argument is present or not
247 248 249 250 251 |
# File 'lib/tty/prompt/question.rb', line 247 def required(value = (not_set = true), = nil) [:required?] = if return @required if not_set @required = value end |
#to_s ⇒ Object
322 323 324 |
# File 'lib/tty/prompt/question.rb', line 322 def to_s "#{}" end |
#validate(value = nil, message = nil, &block) ⇒ Question
Set validation rule for an argument
261 262 263 264 |
# File 'lib/tty/prompt/question.rb', line 261 def validate(value = nil, = nil, &block) [:valid?] = if @validation = (value || block) end |
#validation? ⇒ Boolean
266 267 268 |
# File 'lib/tty/prompt/question.rb', line 266 def validation? @validation != UndefinedSetting end |