Class: TTY::Prompt
- Inherits:
-
Object
- Object
- TTY::Prompt
- Extended by:
- Forwardable
- Defined in:
- lib/tty/prompt.rb,
lib/tty/prompt/list.rb,
lib/tty/prompt/choice.rb,
lib/tty/prompt/reader.rb,
lib/tty/prompt/result.rb,
lib/tty/prompt/slider.rb,
lib/tty/prompt/choices.rb,
lib/tty/prompt/symbols.rb,
lib/tty/prompt/timeout.rb,
lib/tty/prompt/version.rb,
lib/tty/prompt/distance.rb,
lib/tty/prompt/expander.rb,
lib/tty/prompt/keypress.rb,
lib/tty/prompt/question.rb,
lib/tty/prompt/enum_list.rb,
lib/tty/prompt/evaluator.rb,
lib/tty/prompt/multiline.rb,
lib/tty/prompt/paginator.rb,
lib/tty/prompt/statement.rb,
lib/tty/prompt/converters.rb,
lib/tty/prompt/multi_list.rb,
lib/tty/prompt/suggestion.rb,
lib/tty/prompt/reader/line.rb,
lib/tty/prompt/reader/mode.rb,
lib/tty/prompt/reader/codes.rb,
lib/tty/prompt/converter_dsl.rb,
lib/tty/prompt/mask_question.rb,
lib/tty/prompt/enum_paginator.rb,
lib/tty/prompt/reader/console.rb,
lib/tty/prompt/reader/history.rb,
lib/tty/prompt/reader/win_api.rb,
lib/tty/prompt/question/checks.rb,
lib/tty/prompt/confirm_question.rb,
lib/tty/prompt/reader/key_event.rb,
lib/tty/prompt/answers_collector.rb,
lib/tty/prompt/question/modifier.rb,
lib/tty/prompt/converter_registry.rb,
lib/tty/prompt/reader/win_console.rb,
lib/tty/prompt/question/validation.rb
Overview
A class responsible for terminal prompt interactions.
Direct Known Subclasses
Defined Under Namespace
Modules: ConverterDSL, Converters, Symbols Classes: AnswersCollector, Choice, Choices, ConfirmQuestion, ConverterRegistry, Distance, EnumList, EnumPaginator, Evaluator, Expander, Keypress, List, MaskQuestion, MultiList, Multiline, Paginator, Question, Reader, Result, Slider, Statement, Suggestion, Timeout
Constant Summary collapse
- ConfigurationError =
Raised when wrong parameter is used to configure prompt
Class.new(StandardError)
- ConversionError =
Raised when type conversion cannot be performed
Class.new(StandardError)
- ValidationCoercion =
Raised when the passed in validation argument is of wrong type
Class.new(TypeError)
- ArgumentRequired =
Raised when the required argument is not supplied
Class.new(ArgumentError)
- ArgumentValidation =
Raised when the argument validation fails
Class.new(ArgumentError)
- InvalidArgument =
Raised when the argument is not expected
Class.new(ArgumentError)
- VERSION =
"0.13.0"
Instance Attribute Summary collapse
-
#active_color ⇒ Object
readonly
private
Theme colors.
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#enabled_color ⇒ Object
readonly
private
Theme colors.
-
#error_color ⇒ Object
readonly
private
Theme colors.
-
#help_color ⇒ Object
readonly
private
Theme colors.
- #input ⇒ Object readonly private
- #output ⇒ Object readonly private
-
#prefix ⇒ String
readonly
private
Prompt prefix.
-
#reader ⇒ Object
readonly
Returns the value of attribute reader.
Class Method Summary collapse
Instance Method Summary collapse
-
#ask(message, *args) {|question| ... } ⇒ TTY::Prompt::Question
Ask a question.
-
#collect(options = {}, &block) ⇒ Hash
Gathers more than one aswer.
-
#enum_select(question, *args, &block) ⇒ String
Ask a question with indexed list.
-
#error(*args) ⇒ Array
Print statement(s) out in red color.
-
#expand(message, *args, &block) ⇒ Object
Expand available options.
-
#initialize(*args) ⇒ Prompt
constructor
Initialize a Prompt.
-
#inspect ⇒ String
Inspect class name and public attributes.
-
#invoke_question(object, message, *args, &block) ⇒ String
Invoke a question type of prompt.
-
#invoke_select(object, question, *args, &block) ⇒ String
Invoke a list type of prompt.
-
#keypress(message, *args, &block) ⇒ Object
Ask a question with a keypress answer.
-
#mask(message, *args, &block) ⇒ TTY::Prompt::MaskQuestion
Ask masked question.
-
#multi_select(question, *args, &block) ⇒ String
Ask a question with multiple attributes activated.
-
#multiline(message, *args, &block) ⇒ Array[String]
Ask a question with a multiline answer.
-
#no?(message, *args, &block) ⇒ Boolean
A shortcut method to ask the user negative question and return true for ‘no’ reply.
-
#ok(*args) ⇒ Array
Print statement(s) out in red green.
-
#say(message = '', options = {}) ⇒ String
Print statement out.
-
#select(question, *args, &block) ⇒ Object
Ask a question with a list of options.
-
#slider(question, *args, &block) ⇒ String
Ask a question with a range slider.
-
#stderr ⇒ Object
private
Return standard error.
-
#stdin ⇒ Object
private
Return standard in.
-
#stdout ⇒ Object
private
Return standard out.
-
#suggest(message, possibilities, options = {}) ⇒ String
Takes the string provided by the user and compare it with other possible matches to suggest an unambigous string.
-
#tty? ⇒ Boolean
Check if outputing to terminal.
-
#warn(*args) ⇒ Array
Print statement(s) out in yellow color.
-
#yes?(message, *args, &block) ⇒ Boolean
A shortcut method to ask the user positive question and return true for ‘yes’ reply, false for ‘no’.
Constructor Details
#initialize(*args) ⇒ Prompt
Initialize a Prompt
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/tty/prompt.rb', line 115 def initialize(*args) = Utils.(args) @input = .fetch(:input) { $stdin } @output = .fetch(:output) { $stdout } @env = .fetch(:env) { ENV } @prefix = .fetch(:prefix) { '' } @enabled_color = [:enable_color] @active_color = .fetch(:active_color) { :green } @help_color = .fetch(:help_color) { :bright_black } @error_color = .fetch(:error_color) { :red } @interrupt = .fetch(:interrupt) { :error } @track_history = .fetch(:track_history) { true } @cursor = TTY::Cursor @pastel = Pastel.new(@enabled_color.nil? ? {} : { enabled: @enabled_color }) @reader = Reader.new(@input, @output, interrupt: @interrupt, track_history: @track_history, env: @env) end |
Instance Attribute Details
#active_color ⇒ Object (readonly)
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.
Theme colors
70 71 72 |
# File 'lib/tty/prompt.rb', line 70 def active_color @active_color end |
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
55 56 57 |
# File 'lib/tty/prompt.rb', line 55 def cursor @cursor end |
#enabled_color ⇒ Object (readonly)
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.
Theme colors
70 71 72 |
# File 'lib/tty/prompt.rb', line 70 def enabled_color @enabled_color end |
#error_color ⇒ Object (readonly)
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.
Theme colors
70 71 72 |
# File 'lib/tty/prompt.rb', line 70 def error_color @error_color end |
#help_color ⇒ Object (readonly)
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.
Theme colors
70 71 72 |
# File 'lib/tty/prompt.rb', line 70 def help_color @help_color end |
#input ⇒ Object (readonly)
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.
48 49 50 |
# File 'lib/tty/prompt.rb', line 48 def input @input end |
#output ⇒ Object (readonly)
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 |
# File 'lib/tty/prompt.rb', line 51 def output @output end |
#prefix ⇒ String (readonly)
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.
Prompt prefix
65 66 67 |
# File 'lib/tty/prompt.rb', line 65 def prefix @prefix end |
#reader ⇒ Object (readonly)
Returns the value of attribute reader.
53 54 55 |
# File 'lib/tty/prompt.rb', line 53 def reader @reader end |
Class Method Details
.messages ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/tty/prompt.rb', line 82 def self. { range?: 'Value %{value} must be within the range %{in}', valid?: 'Your answer is invalid (must match %{valid})', required?: 'Value must be provided' } end |
Instance Method Details
#ask(message, *args) {|question| ... } ⇒ TTY::Prompt::Question
Ask a question.
167 168 169 |
# File 'lib/tty/prompt.rb', line 167 def ask(, *args, &block) invoke_question(Question, , *args, &block) end |
#collect(options = {}, &block) ⇒ Hash
Gathers more than one aswer
483 484 485 486 |
# File 'lib/tty/prompt.rb', line 483 def collect( = {}, &block) collector = AnswersCollector.new(self, ) collector.call(&block) end |
#enum_select(question, *args, &block) ⇒ String
Ask a question with indexed list
293 294 295 |
# File 'lib/tty/prompt.rb', line 293 def enum_select(question, *args, &block) invoke_select(EnumList, question, *args, &block) end |
#error(*args) ⇒ Array
Print statement(s) out in red color.
440 441 442 443 |
# File 'lib/tty/prompt.rb', line 440 def error(*args) = Utils.(args) args.each { || say , .merge(color: :red) } end |
#expand(message, *args, &block) ⇒ Object
Expand available options
356 357 358 |
# File 'lib/tty/prompt.rb', line 356 def (, *args, &block) invoke_select(Expander, , *args, &block) end |
#inspect ⇒ String
Inspect class name and public attributes
522 523 524 525 526 527 528 529 530 531 532 533 |
# File 'lib/tty/prompt.rb', line 522 def inspect attributes = { input: input, output: output, prefix: prefix, active_color: active_color, error_color: error_color, enabled_color: enabled_color, help_color: help_color } "#<#{self.class}: #{attributes.each { |name, val| "@#{name}=#{val}" }}" end |
#invoke_question(object, message, *args, &block) ⇒ String
Invoke a question type of prompt
143 144 145 146 147 148 |
# File 'lib/tty/prompt.rb', line 143 def invoke_question(object, , *args, &block) = Utils.(args) [:messages] = self.class. question = object.new(self, ) question.(, &block) end |
#invoke_select(object, question, *args, &block) ⇒ String
Invoke a list type of prompt
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/tty/prompt.rb', line 202 def invoke_select(object, question, *args, &block) = Utils.(args) choices = if block [] elsif args.empty? possible = .dup = {} possible elsif args.size == 1 && args[0].is_a?(Hash) Utils.(args) else args.flatten end list = object.new(self, ) list.(question, choices, &block) end |
#keypress(message, *args, &block) ⇒ Object
Ask a question with a keypress answer
176 177 178 |
# File 'lib/tty/prompt.rb', line 176 def keypress(, *args, &block) invoke_question(Keypress, , *args, &block) end |
#mask(message, *args, &block) ⇒ TTY::Prompt::MaskQuestion
Ask masked question
229 230 231 |
# File 'lib/tty/prompt.rb', line 229 def mask(, *args, &block) invoke_question(MaskQuestion, , *args, &block) end |
#multi_select(question, *args, &block) ⇒ String
Ask a question with multiple attributes activated
273 274 275 |
# File 'lib/tty/prompt.rb', line 273 def multi_select(question, *args, &block) invoke_select(MultiList, question, *args, &block) end |
#multiline(message, *args, &block) ⇒ Array[String]
Ask a question with a multiline answer
188 189 190 |
# File 'lib/tty/prompt.rb', line 188 def multiline(, *args, &block) invoke_question(Multiline, , *args, &block) end |
#no?(message, *args, &block) ⇒ Boolean
A shortcut method to ask the user negative question and return true for ‘no’ reply.
328 329 330 331 332 333 334 335 |
# File 'lib/tty/prompt.rb', line 328 def no?(, *args, &block) defaults = { default: false } = Utils.(args) .merge!(defaults.reject { |k, _| .key?(k) }) question = ConfirmQuestion.new(self, ) !question.call(, &block) end |
#ok(*args) ⇒ Array
Print statement(s) out in red green.
408 409 410 411 |
# File 'lib/tty/prompt.rb', line 408 def ok(*args) = Utils.(args) args.each { || say , .merge(color: :green) } end |
#say(message = '', options = {}) ⇒ String
Print statement out. If the supplied message ends with a space or tab character, a new line will not be appended.
389 390 391 392 393 394 395 |
# File 'lib/tty/prompt.rb', line 389 def say( = '', = {}) = .to_s return if .empty? statement = Statement.new(self, ) statement.call() end |
#select(question, *args, &block) ⇒ Object
Ask a question with a list of options
253 254 255 |
# File 'lib/tty/prompt.rb', line 253 def select(question, *args, &block) invoke_select(List, question, *args, &block) end |
#slider(question, *args, &block) ⇒ String
Ask a question with a range slider
372 373 374 375 376 |
# File 'lib/tty/prompt.rb', line 372 def (question, *args, &block) = Utils.(args) = Slider.new(self, ) .call(question, &block) end |
#stderr ⇒ 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.
Return standard error
514 515 516 |
# File 'lib/tty/prompt.rb', line 514 def stderr $stderr end |
#stdin ⇒ 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.
Return standard in
500 501 502 |
# File 'lib/tty/prompt.rb', line 500 def stdin $stdin end |
#stdout ⇒ 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.
Return standard out
507 508 509 |
# File 'lib/tty/prompt.rb', line 507 def stdout $stdout end |
#suggest(message, possibilities, options = {}) ⇒ String
Takes the string provided by the user and compare it with other possible matches to suggest an unambigous string
467 468 469 470 |
# File 'lib/tty/prompt.rb', line 467 def suggest(, possibilities, = {}) suggestion = Suggestion.new() say(suggestion.suggest(, possibilities)) end |
#tty? ⇒ Boolean
Check if outputing to terminal
493 494 495 |
# File 'lib/tty/prompt.rb', line 493 def tty? stdout.tty? end |
#warn(*args) ⇒ Array
Print statement(s) out in yellow color.
424 425 426 427 |
# File 'lib/tty/prompt.rb', line 424 def warn(*args) = Utils.(args) args.each { || say , .merge(color: :yellow) } end |
#yes?(message, *args, &block) ⇒ Boolean
A shortcut method to ask the user positive question and return true for ‘yes’ reply, false for ‘no’.
308 309 310 311 312 313 314 315 |
# File 'lib/tty/prompt.rb', line 308 def yes?(, *args, &block) defaults = { default: true } = Utils.(args) .merge!(defaults.reject { |k, _| .key?(k) }) question = ConfirmQuestion.new(self, ) question.call(, &block) end |