Module: Coo::Helper::Question
- Defined in:
- lib/coo/helper/question.rb
Class Method Summary collapse
- .easy_make_sure?(remind_str = 'Are you sure you want to do this?') ⇒ Boolean
- .make_sure?(remind_str = 'Are you sure you want to do this?') ⇒ 状态值 turn 为继续执行;false 为取消执行
- .question_and_options(question_str, accept_enter, *options, &block) ⇒ 选中的值, -1 为直接 enter; 其他值为选中值得下标
- .remind_input(question_str, valid_regexp = /[\s\S]*/, &block) ⇒ Object
Class Method Details
.easy_make_sure?(remind_str = 'Are you sure you want to do this?') ⇒ Boolean
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/coo/helper/question.rb', line 21 def self.easy_make_sure?(remind_str = 'Are you sure you want to do this?') answer = self.(remind_str, true, 'yes', 'no') flag = false if answer == 0 || answer == -1 puts 'Continue 🚀'.green flag = true else puts 'Cancel 💥'.red end if block_given? yield flag end flag end |
.make_sure?(remind_str = 'Are you sure you want to do this?') ⇒ 状态值 turn 为继续执行;false 为取消执行
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/coo/helper/question.rb', line 6 def self.make_sure?(remind_str = 'Are you sure you want to do this?') answer = self.(remind_str, false, 'yes', 'no') flag = false if answer == 0 puts 'Continue 🚀'.green flag = true else puts 'Cancel 💥'.red end if block_given? yield flag end flag end |
.question_and_options(question_str, accept_enter, *options, &block) ⇒ 选中的值, -1 为直接 enter; 其他值为选中值得下标
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/coo/helper/question.rb', line 40 def self.(question_str, accept_enter, *, &block) if (question_str.to_s.empty? || .length == 0) puts 'question_and_options 参数错误' exit end option_str = '[ ' for i in 0....length op = [i].to_s if op.empty? puts 'question_and_options 参数错误' exit end option_str = option_str.concat(op) if i < .length - 1 option_str = option_str.concat(' / ') end end option_str = option_str.concat(' ]') show_str = question_str + ' ' + option_str puts show_str.blue answer = $stdin.gets.chomp.downcase selected = -2 if answer.to_s.empty? selected = -1 else .each_with_index do |opi, ii| if answer == opi.to_s.downcase selected = ii break end end end lower_limit = 0 if accept_enter == true lower_limit = -1 end if selected < lower_limit if block_given? return self.("💥 Input error, please input again!", accept_enter, *, &block) else return self.("💥 Input error, please input again!", accept_enter, *) end else if block_given? value = '' if selected >= 0 value = [selected].to_s end yield selected, value end return selected end end |
.remind_input(question_str, valid_regexp = /[\s\S]*/, &block) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/coo/helper/question.rb', line 101 def self.remind_input(question_str, valid_regexp = /[\s\S]*/, &block) puts question_str.blue answer = $stdin.gets.chomp if valid_regexp.match(answer) if block_given? yield answer end return answer else return remind_input("💥 Input error, please input again!", valid_regexp, &block) end end |