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.
Instance Method Summary collapse
-
#ask(statement, default: nil, autocomplete: nil) ⇒ 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.
-
#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.
-
#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
#initialize(input: STDIN, output: STDOUT) ⇒ Shell
Returns a new instance of Shell.
11 12 13 14 15 16 |
# File 'lib/clin/shell.rb', line 11 def initialize(input: STDIN, output: STDOUT) @in = input @out = output @yes_or_no_persist = false @override_persist = false end |
Instance Attribute Details
#in ⇒ Object
Input stream, default: STDIN
6 7 8 |
# File 'lib/clin/shell.rb', line 6 def in @in end |
#out ⇒ Object
Output stream, default: STDOUT
9 10 11 |
# File 'lib/clin/shell.rb', line 9 def out @out end |
Instance Method Details
#ask(statement, default: nil, autocomplete: nil) ⇒ Object
Ask a question
22 23 24 25 26 27 28 29 |
# File 'lib/clin/shell.rb', line 22 def ask(statement, default: nil, autocomplete: nil) answer = scan(statement, autocomplete: autocomplete) 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
41 42 43 44 |
# File 'lib/clin/shell.rb', line 41 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
81 82 83 |
# File 'lib/clin/shell.rb', line 81 def file_conflict(filename, default: nil, &block) Clin::ShellInteraction::FileConflict.new(self).run(filename, default: default, &block) end |
#keep?(filename, &block) ⇒ Boolean
File conflict question defaulted to no
91 92 93 |
# File 'lib/clin/shell.rb', line 91 def keep?(filename, &block) file_conflict(filename, default: :no, &block) end |
#no?(statement, options = {}) ⇒ Boolean
Yes or no question defaulted to no
66 67 68 69 |
# File 'lib/clin/shell.rb', line 66 def no?(statement, = {}) [:default] = :no yes_or_no(statement, **) end |
#overwrite?(filename, &block) ⇒ Boolean
File conflict question defaulted to yes
86 87 88 |
# File 'lib/clin/shell.rb', line 86 def overwrite?(filename, &block) file_conflict(filename, default: :yes, &block) end |
#yes?(statement, options = {}) ⇒ Boolean
Yes or no question defaulted to yes
58 59 60 61 |
# File 'lib/clin/shell.rb', line 58 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.
51 52 53 |
# File 'lib/clin/shell.rb', line 51 def yes_or_no(statement, default: nil, persist: false) Clin::ShellInteraction::YesOrNo.new(self).run(statement, default: default, persist: persist) end |