Sha256: df3a1698884b4850198684c14fe7d99135326b4cb9591488a7c1142f64af168d

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

class InputLoop
  DEFAULT_HINT = { 'y' => 'Yes', 'n' => 'No' }.freeze

  def gets(message, list = 'Yn', **hint)
    default, list = build_list(list)
    keys_hint, hint = build_hint(list, hint)

    print "#{message} (#{hint}) #{keys_hint}"
    wait_valid_input(keys_hint, Set.new(list.map(&:downcase)), default)
  end

  private

  def wait_valid_input(keys_hint, keys, default)
    loop do
      r = STDIN.gets.chop.downcase
      break default if r == ''
      break r if keys.include?(r)

      print "  [INVALID!] Please retry: #{keys_hint}:"
    end
  end

  def build_list(list)
    list = list.chars
    default = list.find { |c| c.upcase == c }
    [default.downcase, list.map { |c| c == default ? c : c.downcase }.uniq]
  end

  def build_hint(list, hint)
    valid = "[#{list.join('')}]"
    hint = DEFAULT_HINT.merge(hint.map { |k, v| [k.to_s.downcase, v] }.to_h)
    hint = list.map do |c|
      h = hint[c.downcase]
      next if h.blank?

      "#{c.upcase}=#{h}"
    end
    [valid, hint.join(', ')]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vue_cli-rails-0.1.6 lib/helpers/lib/input_loop.rb