Module: Squared::Common::Prompt
- Defined in:
- lib/squared/common/prompt.rb
Class Method Summary collapse
- .choice(msg, list = nil, min: 1, max: 1, multiple: false, force: true, grep: nil, auto: true, attempts: 5, timeout: 0) ⇒ Object
- .confirm(msg, default = nil, agree: 'Y', cancel: 'N', attempts: 5, timeout: 30) ⇒ Object
- .readline(msg, history = false, force: nil, multiline: nil, &blk) ⇒ Object
Class Method Details
.choice(msg, list = nil, min: 1, max: 1, multiple: false, force: true, grep: nil, auto: true, attempts: 5, timeout: 0) ⇒ Object
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/squared/common/prompt.rb', line 33 def choice(msg, list = nil, min: 1, max: 1, multiple: false, force: true, grep: nil, auto: true, attempts: 5, timeout: 0) require 'readline' require 'timeout' if list grep &&= (grep.is_a?(::Enumerable) ? grep : [grep]).map { |val| Regexp.new(val) } items = [] list.each do |val| next if grep&.none? { |pat| pat.match?(line) } items << val.chomp puts "#{items.size.to_s.rjust(2)}. #{val}" end max = items.size raise_error 'empty selection list' if max == 0 min = [min, max].min if auto msg = "#{msg}: [1-#{max}#{if multiple "|,#{multiple.is_a?(Numeric) ? "{#{multiple}}" : ''}" end}] " end end valid = ->(s) { s.match?(/^-?\d+$/) && s.to_i.between?(min, max) } Timeout.timeout(timeout) do while (ch = Readline.readline(msg)) unless (ch = ch.strip).empty? if multiple a = ch.split(/\s*,\s*/) b = a.select { |s| valid.call(s) }.map!(&:to_i).sort next unless a.size == b.size return b unless items next if multiple.is_a?(::Numeric) && multiple != b.size return b.map! { |i| items[i - 1] } elsif valid.call(ch) return items ? items[ch.to_i - 1] : ch.to_i end end attempts -= 1 next if attempts > 0 break unless force exit 1 end rescue Interrupt puts exit 0 else multiple ? [] : nil end end |
.confirm(msg, default = nil, agree: 'Y', cancel: 'N', attempts: 5, timeout: 30) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/squared/common/prompt.rb', line 8 def confirm(msg, default = nil, agree: 'Y', cancel: 'N', attempts: 5, timeout: 30) require 'readline' require 'timeout' agree = /^#{Regexp.escape(agree)}$/i if agree.is_a?(::String) cancel = /^#{Regexp.escape(cancel)}$/i if cancel.is_a?(::String) Timeout.timeout(timeout) do while (ch = Readline.readline(msg)) ch = ch.chomp case (ch.empty? ? default : ch) when agree return true when cancel return false end attempts -= 1 exit 1 unless attempts > 0 end rescue Interrupt puts exit 0 else false end end |
.readline(msg, history = false, force: nil, multiline: nil, &blk) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/squared/common/prompt.rb', line 85 def readline(msg, history = false, force: nil, multiline: nil, &blk) require 'readline' multiline = if multiline && Readline.respond_to?(:readmultiline) multiline.is_a?(::Enumerable) || block_given? ? multiline : [multiline.to_s] end prompt = lambda do if !multiline Readline.readline(msg, history) elsif block_given? Readline.readmultiline(msg, history, &blk) else Readline.readmultiline(msg, history) { |line| multiline.any? { |val| line.split.last.end_with?(val) } } end end case force when ::TrueClass, ::FalseClass msg = "#{msg} %s " % if multiline multiline.is_a?(::Enumerable) ? "{#{multiline.join('|')}}" : multiline else "(#{force ? 'required' : 'optional'}):" end ret = (prompt.call || '').strip multiline.each { |val| break if ret.delete_suffix!(val) } if multiline.is_a?(::Enumerable) raise_error 'user cancelled' if force && ret.empty? ret else prompt.call end end |