Class: TTY::Shell::Response
- Inherits:
-
Object
- Object
- TTY::Shell::Response
- Defined in:
- lib/tty/shell/response.rb
Overview
A class representing a shell response
Constant Summary collapse
- VALID_TYPES =
[ :boolean, :string, :symbol, :integer, :float, :date, :datetime ]
Instance Method Summary collapse
-
#initialize(question, shell = Shell.new) ⇒ Response
constructor
Initialize a Response.
-
#read(type = nil) ⇒ undefined
Read input from STDIN either character or line.
-
#read_bool(error = nil) ⇒ Object
Read boolean.
-
#read_char ⇒ Object
Read answer’s first character.
-
#read_choice(type = nil) ⇒ Object
Read answer from predifined choicse.
-
#read_date ⇒ Object
Read date.
-
#read_datetime ⇒ Object
Read datetime.
-
#read_email ⇒ String
Read string answer and validate against email regex.
-
#read_file(error = nil) ⇒ Object
Read file contents.
-
#read_float(error = nil) ⇒ Object
Read float value.
- #read_input ⇒ Object private
-
#read_int(error = nil) ⇒ Object
Read integer value.
-
#read_keypress ⇒ Object
Read a single keypress.
-
#read_multiple ⇒ Object
Read answer provided on multiple lines.
-
#read_password ⇒ Object
Read password.
-
#read_range ⇒ Object
Read range expression.
-
#read_regex(error = nil) ⇒ Object
Read regular expression.
-
#read_string(error = nil) ⇒ Object
Read answer and cast to String type.
-
#read_symbol(error = nil) ⇒ Object
Read ansewr and cast to Symbol type.
-
#read_text ⇒ Object
Read multiple line answer and cast to String type.
Constructor Details
Instance Method Details
#read(type = nil) ⇒ undefined
Read input from STDIN either character or line
46 47 48 |
# File 'lib/tty/shell/response.rb', line 46 def read(type = nil) question.evaluate_response read_input end |
#read_bool(error = nil) ⇒ Object
Read boolean
161 162 163 164 |
# File 'lib/tty/shell/response.rb', line 161 def read_bool(error = nil) response = @converter.convert(read_input).to(:boolean, strict: true) question.evaluate_response(response) end |
#read_char ⇒ Object
Read answer’s first character
84 85 86 87 |
# File 'lib/tty/shell/response.rb', line 84 def read_char question.char(true) question.evaluate_response String(read_input).chars.to_a[0] end |
#read_choice(type = nil) ⇒ Object
Read answer from predifined choicse
106 107 108 109 |
# File 'lib/tty/shell/response.rb', line 106 def read_choice(type = nil) question.argument(:required) unless question.default? question.evaluate_response read_input end |
#read_date ⇒ Object
Read date
145 146 147 148 |
# File 'lib/tty/shell/response.rb', line 145 def read_date response = @converter.convert(read_input).to(:date) question.evaluate_response(response) end |
#read_datetime ⇒ Object
Read datetime
153 154 155 156 |
# File 'lib/tty/shell/response.rb', line 153 def read_datetime response = @converter.convert(read_input).to(:datetime) question.evaluate_response(response) end |
#read_email ⇒ String
Read string answer and validate against email regex
178 179 180 181 182 |
# File 'lib/tty/shell/response.rb', line 178 def read_email question.validate(/^[a-z0-9._%+-]+@([a-z0-9-]+\.)+[a-z]{2,6}$/i) question.prompt(question.statement) if question.error with_exception { read_string } end |
#read_file(error = nil) ⇒ Object
Read file contents
169 170 171 |
# File 'lib/tty/shell/response.rb', line 169 def read_file(error = nil) question.evaluate_response File.open(File.join(directory, read_input)) end |
#read_float(error = nil) ⇒ Object
Read float value
122 123 124 125 |
# File 'lib/tty/shell/response.rb', line 122 def read_float(error = nil) response = @converter.convert(read_input).to(:float) question.evaluate_response(response) end |
#read_input ⇒ 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.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/tty/shell/response.rb', line 51 def read_input reader = Reader.new(shell) if question.mask? && question.echo? reader.getc(question.mask) else TTY.terminal.echo(question.echo) do TTY.terminal.raw(question.raw) do if question.raw? reader.readpartial(10) elsif question.character? reader.getc(question.mask) else reader.gets end end end end end |
#read_int(error = nil) ⇒ Object
Read integer value
114 115 116 117 |
# File 'lib/tty/shell/response.rb', line 114 def read_int(error = nil) response = @converter.convert(read_input).to(:integer) question.evaluate_response(response) end |
#read_keypress ⇒ Object
Read a single keypress
209 210 211 212 213 214 215 |
# File 'lib/tty/shell/response.rb', line 209 def read_keypress question.echo false question.raw true question.evaluate_response(read_input).tap do |key| raise Interrupt if key == "\x03" # Ctrl-C end end |
#read_multiple ⇒ Object
Read answer provided on multiple lines
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/tty/shell/response.rb', line 187 def read_multiple response = '' loop do value = question.evaluate_response read_input break if !value || value == '' next if value !~ /\S/ response << value end response end |
#read_password ⇒ Object
Read password
201 202 203 204 |
# File 'lib/tty/shell/response.rb', line 201 def read_password question.echo false question.evaluate_response read_input end |
#read_range ⇒ Object
Read range expression
137 138 139 140 |
# File 'lib/tty/shell/response.rb', line 137 def read_range response = @converter.convert(read_input).to(:range, strict: true) question.evaluate_response(response) end |
#read_regex(error = nil) ⇒ Object
Read regular expression
130 131 132 |
# File 'lib/tty/shell/response.rb', line 130 def read_regex(error = nil) question.evaluate_response Kernel.send(:Regex, read_input) end |
#read_string(error = nil) ⇒ Object
Read answer and cast to String type
77 78 79 |
# File 'lib/tty/shell/response.rb', line 77 def read_string(error = nil) question.evaluate_response(String(read_input).strip) end |
#read_symbol(error = nil) ⇒ Object
Read ansewr and cast to Symbol type
99 100 101 |
# File 'lib/tty/shell/response.rb', line 99 def read_symbol(error = nil) question.evaluate_response(read_input.to_sym) end |
#read_text ⇒ Object
Read multiple line answer and cast to String type
92 93 94 |
# File 'lib/tty/shell/response.rb', line 92 def read_text question.evaluate_response String(read_input) end |