Class: TTY::Shell
- Inherits:
-
Object
- Object
- TTY::Shell
- Defined in:
- lib/tty/shell.rb,
lib/tty/shell/reader.rb,
lib/tty/shell/question.rb,
lib/tty/shell/response.rb,
lib/tty/shell/statement.rb,
lib/tty/shell/suggestion.rb,
lib/tty/shell/question/modifier.rb,
lib/tty/shell/question/validation.rb,
lib/tty/shell/response_delegation.rb
Overview
A class responsible for shell prompt interactions.
Defined Under Namespace
Modules: ResponseDelegation Classes: Question, Reader, Response, Statement, Suggestion
Instance Attribute Summary collapse
- #input ⇒ Object readonly private
- #output ⇒ Object readonly private
-
#prefix ⇒ Object
readonly
private
Shell prompt prefix.
Instance Method Summary collapse
-
#ask(statement, *args) {|question| ... } ⇒ TTY::Question
Ask a question.
-
#confirm(*args) ⇒ Array
Print statement(s) out in red green.
-
#error(*args) ⇒ Array
Print statement(s) out in red color.
-
#initialize(input = stdin, output = stdout, options = {}) ⇒ Shell
constructor
Initialize a Shell.
-
#no?(statement, *args, &block) ⇒ Boolean
A shortcut method to ask the user negative question and return true for ‘no’ reply.
-
#print_table(*args, &block) ⇒ undefined
Print a table to shell.
-
#say(message = '', options = {}) ⇒ String
Print statement out.
-
#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 shell.
-
#warn(*args) ⇒ Array
Print statement(s) out in yellow color.
-
#yes?(statement, *args, &block) ⇒ Boolean
A shortcut method to ask the user positive question and return true for ‘yes’ reply.
Constructor Details
#initialize(input = stdin, output = stdout, options = {}) ⇒ Shell
Initialize a Shell
20 21 22 23 24 |
# File 'lib/tty/shell.rb', line 20 def initialize(input = stdin, output = stdout, = {}) @input = input @output = output @prefix = .fetch(:prefix) { '' } end |
Instance Attribute Details
#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.
7 8 9 |
# File 'lib/tty/shell.rb', line 7 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.
10 11 12 |
# File 'lib/tty/shell.rb', line 10 def output @output end |
#prefix ⇒ 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.
Shell prompt prefix
15 16 17 |
# File 'lib/tty/shell.rb', line 15 def prefix @prefix end |
Instance Method Details
#ask(statement, *args) {|question| ... } ⇒ TTY::Question
Ask a question.
43 44 45 46 47 48 49 |
# File 'lib/tty/shell.rb', line 43 def ask(statement, *args, &block) = Utils.(args) question = Question.new self, question.instance_eval(&block) if block_given? question.prompt(statement) end |
#confirm(*args) ⇒ Array
Print statement(s) out in red green.
101 102 103 104 |
# File 'lib/tty/shell.rb', line 101 def confirm(*args) = Utils.(args) args.each { || say , .merge(color: :green) } end |
#error(*args) ⇒ Array
Print statement(s) out in red color.
133 134 135 136 |
# File 'lib/tty/shell.rb', line 133 def error(*args) = Utils.(args) args.each { || say , .merge(color: :red) } end |
#no?(statement, *args, &block) ⇒ Boolean
A shortcut method to ask the user negative question and return true for ‘no’ reply.
67 68 69 |
# File 'lib/tty/shell.rb', line 67 def no?(statement, *args, &block) !yes?(statement, *args, &block) end |
#print_table(*args, &block) ⇒ undefined
Print a table to shell.
174 175 176 177 178 179 |
# File 'lib/tty/shell.rb', line 174 def print_table(*args, &block) = Utils.(args) renderer = .fetch(:renderer) { :basic } table = TTY::Table.new(*args, &block) say table.render(renderer, ) 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.
82 83 84 85 86 87 88 |
# File 'lib/tty/shell.rb', line 82 def say( = '', = {}) = .to_str return unless .length > 0 statement = Statement.new(self, ) statement.declare 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
207 208 209 |
# File 'lib/tty/shell.rb', line 207 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
193 194 195 |
# File 'lib/tty/shell.rb', line 193 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
200 201 202 |
# File 'lib/tty/shell.rb', line 200 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
160 161 162 163 |
# File 'lib/tty/shell.rb', line 160 def suggest(, possibilities, = {}) suggestion = Suggestion.new() say(suggestion.suggest(, possibilities)) end |
#tty? ⇒ Boolean
Check if outputing to shell
186 187 188 |
# File 'lib/tty/shell.rb', line 186 def tty? stdout.tty? end |
#warn(*args) ⇒ Array
Print statement(s) out in yellow color.
117 118 119 120 |
# File 'lib/tty/shell.rb', line 117 def warn(*args) = Utils.(args) args.each { || say , .merge(color: :yellow) } end |
#yes?(statement, *args, &block) ⇒ Boolean
A shortcut method to ask the user positive question and return true for ‘yes’ reply.
57 58 59 |
# File 'lib/tty/shell.rb', line 57 def yes?(statement, *args, &block) ask(statement, *args, &block).read_bool end |