Class: TTY::Shell

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options={})
  @input  = input
  @output = output
  @prefix = options.fetch(:prefix) { '' }
end

Instance Attribute Details

#inputObject (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.



9
10
11
# File 'lib/tty/shell.rb', line 9

def input
  @input
end

#outputObject (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.



12
13
14
# File 'lib/tty/shell.rb', line 12

def output
  @output
end

#prefixObject (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.



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.

Examples:

shell = TTY::Shell.new
shell.ask("What is your name?")

Parameters:

  • statement (String)

    string question to be asked

Yields:

  • (question)

Yield Parameters:

  • question (TTY::Question)

    further configure the question

Returns:

  • (TTY::Question)


43
44
45
46
47
48
49
# File 'lib/tty/shell.rb', line 43

def ask(statement, *args, &block)
  options = Utils.extract_options!(args)

  question = Question.new self, options
  question.instance_eval(&block) if block_given?
  question.prompt(statement)
end

#confirm(*args) ⇒ Array

Print statement(s) out in red green.

Examples:

shell.confirm "Are you sure?"
shell.confirm "All is fine!", "This is fine too."

Parameters:

  • messages (Array)

Returns:

  • (Array)

    messages



101
102
103
104
# File 'lib/tty/shell.rb', line 101

def confirm(*args)
  options = Utils.extract_options!(args)
  args.each { |message| say message, options.merge(:color => :green) }
end

#error(*args) ⇒ Array

Print statement(s) out in red color.

Examples:

shell.error "Shutting down all systems!"
shell.error "Nothing is fine!", "All is broken!"

Parameters:

  • messages (Array)

Returns:

  • (Array)

    messages



133
134
135
136
# File 'lib/tty/shell.rb', line 133

def error(*args)
  options = Utils.extract_options!(args)
  args.each { |message| say message, options.merge(:color => :red) }
end

#no?(statement, *args, &block) ⇒ Boolean

A shortcut method to ask the user negative question and return true for ‘no’ reply.

Returns:

  • (Boolean)


67
68
69
# File 'lib/tty/shell.rb', line 67

def no?(statement, *args, &block)
  !yes?(statement, *args, &block)
end

Print a table to shell.

Examples:

of a table with rows rendered with ascii border

rows = [[1], [2], [3]]
TTY.shell.print_table rows, renderer: :ascii

Returns:

  • (undefined)


174
175
176
177
178
179
# File 'lib/tty/shell.rb', line 174

def print_table(*args, &block)
  options  = Utils.extract_options!(args)
  renderer = options.fetch(:renderer) { :basic }
  table    = TTY::Table.new(*args, &block)
  say table.render(renderer, options)
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.

Examples:

say("Simple things.")

Parameters:

  • message (String) (defaults to: "")

Returns:

  • (String)


82
83
84
85
86
87
88
# File 'lib/tty/shell.rb', line 82

def say(message="", options={})
  message = message.to_str
  return unless message.length > 0

  statement = Statement.new(self, options)
  statement.declare message
end

#stderrObject



198
199
200
# File 'lib/tty/shell.rb', line 198

def stderr
  $stderr
end

#stdinObject



190
191
192
# File 'lib/tty/shell.rb', line 190

def stdin
  $stdin
end

#stdoutObject



194
195
196
# File 'lib/tty/shell.rb', line 194

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

Examples:

shell.suggest('sta', ['status', 'stage', 'commit', 'branch'])
# => "status, stage"

Parameters:

  • message (String)
  • possibilities (Array)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :indent (String)

    The number of spaces for indentation

  • :single_text (String)

    The text for a single suggestion

  • :plural_text (String)

    The text for multiple suggestions

Returns:

  • (String)


160
161
162
163
# File 'lib/tty/shell.rb', line 160

def suggest(message, possibilities, options={})
  suggestion = Suggestion.new(options)
  say(suggestion.suggest(message, possibilities))
end

#tty?Boolean

Check if outputing to shell

Returns:

  • (Boolean)


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.

Examples:

shell.warn "This action can have dire consequences"
shell.warn "Carefull young apprentice", "This is potentially dangerous."

Parameters:

  • messages (Array)

Returns:

  • (Array)

    messages



117
118
119
120
# File 'lib/tty/shell.rb', line 117

def warn(*args)
  options = Utils.extract_options!(args)
  args.each { |message| say message, options.merge(:color => :yellow) }
end

#yes?(statement, *args, &block) ⇒ Boolean

A shortcut method to ask the user positive question and return true for ‘yes’ reply.

Returns:

  • (Boolean)


57
58
59
# File 'lib/tty/shell.rb', line 57

def yes?(statement, *args, &block)
  ask(statement, *args, &block).read_bool
end