Module: Squared::Common::Prompt

Defined in:
lib/squared/common/prompt.rb

Class Method Summary collapse

Class Method Details

.choice(msg, list = nil, min: 1, max: 1, multiple: false, index: false, grep: nil, border: nil, auto: true, force: true, attempts: 3, timeout: 0) ⇒ Object



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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/squared/common/prompt.rb', line 56

def choice(msg, list = nil, min: 1, max: 1, multiple: false, index: false, grep: nil, border: nil, auto: true,
           force: true, attempts: 3, timeout: 0)
  require 'timeout'
  if list
    grep &&= Array(grep).map { |val| Regexp.new(val) }
    items = list.each_with_object([]) do |val, out|
      next if grep&.none? { |pat| pat.match?(line) }

      out << val.to_s.chomp
      puts '%2d. %s' % [out.size, val]
    end
    max = items.size
    raise ArgumentError, 'empty selection list' if max == 0

    min = grep ? 1 : [min, max].min
    if auto
      auto.times { puts } if auto.is_a?(::Numeric)
      if border == true
        puts print_footer
      elsif border
        puts print_footer(border: border)
      end
      msg = "#{msg + (force ? ':' : '?')} [#{min}-#{max}#{if (n = multiple)
                                                            "|,#{n.is_a?(::Numeric) ? "{#{n}}" : '*'}"
                                                          end}] "
    end
  end
  between = ->(s) { s.match?(/^\d+$/) && s.to_i.between?(min, max) }
  Timeout.timeout(timeout) do
    while (ch = Readline.readline(msg))
      ch.strip!
      unless ch.empty?
        if multiple
          k = if ch == '*'
                (min..max).to_a
              else
                ch.split(',').flat_map do |s|
                  s.strip!
                  if s =~ /^(\d+)-(\d+)$/
                    next unless between.call($1) && between.call($2)

                    i = $1.to_i
                    j = $2.to_i
                    next (i..j).to_a if i < j
                  elsif between.call(s)
                    s.to_i
                  end
                end
              end
          unless k.include?(nil)
            k.uniq!
            k.sort!
            unless multiple.is_a?(::Numeric) && multiple != k.size
              return index || !items ? k : k.map { |i| items[i.pred] }
            end
          end
        elsif between.call(ch)
          return index || !items ? ch.to_i : items[ch.to_i.pred]
        end
      end
      attempts -= 1
      next if attempts > 0

      exit 1 if force
      break
    end
  rescue Interrupt
    puts
    exit 0
  else
    [] if multiple
  end
end

.confirm(msg, default = nil, agree: 'Y', cancel: 'N', force: false, attempts: 3, timeout: 60) ⇒ Object



22
23
24
25
26
27
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
# File 'lib/squared/common/prompt.rb', line 22

def confirm(msg, default = nil, agree: 'Y', cancel: 'N', force: false, attempts: 3, timeout: 60)
  require 'timeout'
  if agree == 'Y' && cancel == 'N' && !msg.match?(%r{\[(?:Yn|nY|Y/n|y/N)\]})
    case default
    when 'Y'
      msg = "#{msg} [Y/n] "
    when 'N'
      msg = "#{msg} [y/N] "
    end
  end
  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
        exit 1 if force
        return false
      end
      attempts -= 1
      exit 1 unless attempts > 0
    end
  end
rescue Interrupt
  puts
  exit 0
else
  exit 1 if force
  false
end

.readline(msg, history = false, force: nil, multiline: nil, &blk) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/squared/common/prompt.rb', line 130

def readline(msg, history = false, force: nil, multiline: nil, &blk)
  multiline = nil unless Readline.respond_to?(:readmultiline)
  read = lambda do
    if !multiline
      Readline.readline(msg, history)
    elsif block_given?
      Readline.readmultiline(msg, history, &blk)
    else
      Readline.readmultiline(msg, history) do |line|
        next if line.strip.empty?

        Array(multiline).any? { |val| line.split.last.end_with?(val.to_s) }
      end
    end
  end
  case force
  when true, false
    msg = "#{msg}#{unless msg.match?(/[>:\]}$|]\z/)
                     '%s%s' % if multiline.is_a?(::Enumerable)
                                [' ', "{#{multiline.to_a.join('|')}}"]
                              else
                                [multiline || (force ? ':' : '?'), '']
                              end
                   end} "
    ret = (read.call || '').strip
    multiline.each { |val| break if ret.delete_suffix!(val.to_s) } if multiline.is_a?(::Enumerable)
    exit 1 if force && ret.empty?
    ret
  else
    read.call
  end
end