Class: Clin::Shell
- Inherits:
-
Object
- Object
- Clin::Shell
- Defined in:
- lib/clin/shell.rb
Overview
Class the offer helper method to interact with the user using the command line
Instance Attribute Summary collapse
-
#in ⇒ Object
Input stream, default: STDIN.
-
#out ⇒ Object
Output stream, default: STDOUT.
-
#text ⇒ Object
Text builder instance that is used to stream.
Instance Method Summary collapse
-
#ask(statement, default: nil, autocomplete: nil, echo: true, add_to_history: true) ⇒ Object
Ask a question.
-
#choose(statement, choices, default: nil, allow_initials: false) ⇒ Object
Ask a question and expect the result to be in the list of choices Will continue asking until the input is correct or if a default value is supplied then empty will return.
-
#file_conflict(filename, default: nil, &block) ⇒ Boolean
File conflict helper method.
-
#indent(indent, &block) ⇒ Object
Indent the current output.
-
#initialize(input: STDIN, output: STDOUT) ⇒ Shell
constructor
A new instance of Shell.
-
#keep?(filename, &block) ⇒ Boolean
File conflict question defaulted to no.
-
#no?(statement, options = {}) ⇒ Boolean
Yes or no question defaulted to no.
-
#overwrite?(filename, &block) ⇒ Boolean
File conflict question defaulted to yes.
- #password(statement, default: nil) ⇒ Object
- #say(line, indent: '') ⇒ Object
-
#select(statement, choices, default: nil) ⇒ Object
Ask a question with a list of possible answer.
-
#yes?(statement, options = {}) ⇒ Boolean
Yes or no question defaulted to yes.
-
#yes_or_no(statement, default: nil, persist: false) ⇒ Object
Expect the user the return yes or no(y/n also works) call to yes_or_no with persist: true will return true instead of asking the user.
Constructor Details
Instance Attribute Details
#in ⇒ Object
Input stream, default: STDIN
7 8 9 |
# File 'lib/clin/shell.rb', line 7 def in @in end |
#out ⇒ Object
Output stream, default: STDOUT
10 11 12 |
# File 'lib/clin/shell.rb', line 10 def out @out end |
#text ⇒ Object
Text builder instance that is used to stream
13 14 15 |
# File 'lib/clin/shell.rb', line 13 def text @text end |
Instance Method Details
#ask(statement, default: nil, autocomplete: nil, echo: true, add_to_history: true) ⇒ Object
Ask a question
38 39 40 41 42 43 44 45 |
# File 'lib/clin/shell.rb', line 38 def ask(statement, default: nil, autocomplete: nil, echo: true, add_to_history: true) answer = scan(statement, autocomplete: autocomplete, echo: echo, add_to_history: add_to_history) if answer.blank? default else answer.strip end end |
#choose(statement, choices, default: nil, allow_initials: false) ⇒ Object
Ask a question and expect the result to be in the list of choices Will continue asking until the input is correct or if a default value is supplied then empty will return. If multiple choices start with the same initial ONLY the first one will be able to be selected using its initial
61 62 63 64 |
# File 'lib/clin/shell.rb', line 61 def choose(statement, choices, default: nil, allow_initials: false) Clin::ShellInteraction::Choose.new(self).run(statement, choices, default: default, allow_initials: allow_initials) end |
#file_conflict(filename, default: nil, &block) ⇒ Boolean
File conflict helper method. Give the following options to the user
-
yes, Yes for this one
-
no, No for this one
-
all, Yes for all one
-
quit, Quit the program
-
diff, Diff the 2 files
112 113 114 |
# File 'lib/clin/shell.rb', line 112 def file_conflict(filename, default: nil, &block) Clin::ShellInteraction::FileConflict.new(self).run(filename, default: default, &block) end |
#indent(indent, &block) ⇒ Object
Indent the current output
28 29 30 |
# File 'lib/clin/shell.rb', line 28 def indent(indent, &block) text.indent(indent, &block) end |
#keep?(filename, &block) ⇒ Boolean
File conflict question defaulted to no
122 123 124 |
# File 'lib/clin/shell.rb', line 122 def keep?(filename, &block) file_conflict(filename, default: :no, &block) end |
#no?(statement, options = {}) ⇒ Boolean
Yes or no question defaulted to no
97 98 99 100 |
# File 'lib/clin/shell.rb', line 97 def no?(statement, = {}) [:default] = :no yes_or_no(statement, **) end |
#overwrite?(filename, &block) ⇒ Boolean
File conflict question defaulted to yes
117 118 119 |
# File 'lib/clin/shell.rb', line 117 def overwrite?(filename, &block) file_conflict(filename, default: :yes, &block) end |
#password(statement, default: nil) ⇒ Object
47 48 49 |
# File 'lib/clin/shell.rb', line 47 def password(statement, default: nil) ask(statement, default: default, echo: false, add_to_history: false) end |
#say(line, indent: '') ⇒ Object
23 24 25 |
# File 'lib/clin/shell.rb', line 23 def say(line, indent: '') @out.puts text.line(line, indent: indent) end |
#select(statement, choices, default: nil) ⇒ Object
Ask a question with a list of possible answer. Answer can either be selected using their name or their index e.g. Select answer:
-
Choice A
-
Choice B
-
Choice C
73 74 75 |
# File 'lib/clin/shell.rb', line 73 def select(statement, choices, default: nil) Clin::ShellInteraction::Select.new(self).run(statement, choices, default: default) end |
#yes?(statement, options = {}) ⇒ Boolean
Yes or no question defaulted to yes
89 90 91 92 |
# File 'lib/clin/shell.rb', line 89 def yes?(statement, = {}) [:default] = :yes yes_or_no(statement, **) end |
#yes_or_no(statement, default: nil, persist: false) ⇒ Object
Expect the user the return yes or no(y/n also works) call to yes_or_no with persist: true will return true instead of asking the user.
82 83 84 |
# File 'lib/clin/shell.rb', line 82 def yes_or_no(statement, default: nil, persist: false) Clin::ShellInteraction::YesOrNo.new(self).run(statement, default: default, persist: persist) end |