Class: EY::CLI::UI::Prompter
- Inherits:
-
Object
- Object
- EY::CLI::UI::Prompter
- Defined in:
- lib/engineyard/cli/ui.rb
Class Method Summary collapse
- .add_answer(arg) ⇒ Object
- .agree(question, default) ⇒ Object
- .ask(question, password = false, default = nil) ⇒ Object
- .enable_mock! ⇒ Object
- .highline ⇒ Object
- .interactive? ⇒ Boolean
- .questions ⇒ Object
- .timeout_if_not_interactive(&block) ⇒ Object
Class Method Details
.add_answer(arg) ⇒ Object
19 20 21 22 |
# File 'lib/engineyard/cli/ui.rb', line 19 def self.add_answer(arg) @answers ||= [] @answers << arg end |
.agree(question, default) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/engineyard/cli/ui.rb', line 58 def self.agree(question, default) if @mock @questions ||= [] @questions << question answer = @answers.shift answer == '' ? default : %w[y yes].include?(answer) else timeout_if_not_interactive do answer = highline.agree(question) {|q| q.default = default ? 'Y/n' : 'N/y' } case answer when 'Y/n' then true when 'N/y' then false else answer end end end end |
.ask(question, password = false, default = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/engineyard/cli/ui.rb', line 42 def self.ask(question, password = false, default = nil) if @mock @questions ||= [] @questions << question answer = @answers.shift (answer == '' && default) ? default : answer else timeout_if_not_interactive do highline.ask(question) do |q| q.echo = "*" if password q.default = default if default end.to_s end end end |
.enable_mock! ⇒ Object
28 29 30 31 32 |
# File 'lib/engineyard/cli/ui.rb', line 28 def self.enable_mock! @questions = [] @answers = [] @mock = true end |
.highline ⇒ Object
34 35 36 |
# File 'lib/engineyard/cli/ui.rb', line 34 def self.highline @highline ||= HighLine.new($stdin) end |
.interactive? ⇒ Boolean
38 39 40 |
# File 'lib/engineyard/cli/ui.rb', line 38 def self.interactive? @mock || ($stdout && $stdout.tty?) end |
.questions ⇒ Object
24 25 26 |
# File 'lib/engineyard/cli/ui.rb', line 24 def self.questions @questions end |
.timeout_if_not_interactive(&block) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/engineyard/cli/ui.rb', line 76 def self.timeout_if_not_interactive(&block) if interactive? block.call else Timeout.timeout(2, &block) end end |