Class: EY::CLI::UI::Prompter

Inherits:
Object
  • Object
show all
Defined in:
lib/engineyard/cli/ui.rb

Class Method Summary collapse

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

.highlineObject



34
35
36
# File 'lib/engineyard/cli/ui.rb', line 34

def self.highline
  @highline ||= HighLine.new($stdin)
end

.interactive?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/engineyard/cli/ui.rb', line 38

def self.interactive?
  @mock || ($stdout && $stdout.tty?)
end

.questionsObject



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