Class: PDK::CLI::Util::Interview
- Inherits:
-
TTY::Prompt::AnswersCollector
- Object
- TTY::Prompt::AnswersCollector
- PDK::CLI::Util::Interview
- Defined in:
- lib/pdk/cli/util/interview.rb
Constant Summary collapse
- READER =
defined?(TTY::Reader) ? TTY::Reader : TTY::Prompt::Reader
Instance Method Summary collapse
- #add_question(options = {}) ⇒ Object
- #add_questions(questions) ⇒ Object
- #num_questions ⇒ Object
- #pastel ⇒ Object
- #run ⇒ Object
Instance Method Details
#add_question(options = {}) ⇒ Object
20 21 22 |
# File 'lib/pdk/cli/util/interview.rb', line 20 def add_question( = {}) (@questions ||= {})[[:name]] = end |
#add_questions(questions) ⇒ Object
14 15 16 17 18 |
# File 'lib/pdk/cli/util/interview.rb', line 14 def add_questions(questions) questions.each do |question| add_question(question) end end |
#num_questions ⇒ Object
24 25 26 |
# File 'lib/pdk/cli/util/interview.rb', line 24 def num_questions (@questions ||= {}).count end |
#pastel ⇒ Object
10 11 12 |
# File 'lib/pdk/cli/util/interview.rb', line 10 def pastel @pastel ||= Pastel.new end |
#run ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/pdk/cli/util/interview.rb', line 28 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] if question.key?(:help) case question[:type] when :yes yes?(_('-->')) do |q| q.default(question[:default]) if question.key?(:default) end when :multi_select multi_select(_('-->'), per_page: question[:choices].count) do |q| q.enum ')' q.default(*question[:default]) if question.key?(:default) question[:choices].each do |text, | q.choice text, end end else 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 end i += 1 @prompt.puts '' end @answers rescue READER::InputInterrupt nil end |