Class: PDK::CLI::Util::Interview

Inherits:
TTY::Prompt::AnswersCollector
  • Object
show all
Defined in:
lib/pdk/cli/util/interview.rb

Instance Method Summary collapse

Instance Method Details

#add_question(options = {}) ⇒ Object



18
19
20
# File 'lib/pdk/cli/util/interview.rb', line 18

def add_question(options = {})
  (@questions ||= {})[options[:name]] = options
end

#add_questions(questions) ⇒ Object



12
13
14
15
16
# File 'lib/pdk/cli/util/interview.rb', line 12

def add_questions(questions)
  questions.each do |question|
    add_question(question)
  end
end

#num_questionsObject



22
23
24
# File 'lib/pdk/cli/util/interview.rb', line 22

def num_questions
  (@questions ||= {}).count
end

#pastelObject



8
9
10
# File 'lib/pdk/cli/util/interview.rb', line 8

def pastel
  @pastel ||= Pastel.new
end

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pdk/cli/util/interview.rb', line 26

def run
  i = 1
  num_questions = @questions.count
  @questions.each do |question_name, question|
    @name = question_name
    @prompt.print pastel.bold(_('[Q %{current_number}/%{questions_total}]') % { current_number: i, questions_total: num_questions }) + ' '
    @prompt.puts pastel.bold(question[:question])
    @prompt.puts question[:help]
    ask(_('-->')) do |q|
      q.required(question.fetch(:required, false))

      if question.key?(:validate_pattern)
        q.validate(question[:validate_pattern], question[:validate_message])
      end

      q.default(question[:default]) if question.key?(:default)
    end
    i += 1
    @prompt.puts ''
  end
  @answers
rescue TTY::Prompt::Reader::InputInterrupt
  nil
end