Sha256: 8b2a8500a1b340badbb98b9ea1c4c7dd2962b5d937b13ef06a1b9d5ba43dbf8c

Contents?: true

Size: 812 Bytes

Versions: 27

Compression:

Stored size: 812 Bytes

Contents

require 'readline'

module EverydayCliUtils
  module Ask
    def self::setup_options(options)
      mapped = []
      options.each_with_index { |v, k|
        mapped << "#{k+1}) #{v.to_s}"
      }
      mapped.join("\n")
    end

    def self::ask(question, options, &block)
      val = '-1'
      while true
        print "#{question}\n\n#{setup_options(options)}\n\n"
        val = Readline.readline("Please enter your choice (1 - #{options.count}): ", true)
        if !(val =~ /^\d+$/).nil? && (1..options.count).include?(val.to_i)
          break
        end
        print "\n\nYou must enter an integer between 1 and #{options.count}. Please try again.\n\n"
      end
      block.call(options[val.to_i - 1])
    end

    def self::hash_to_options(hash, extra = [])
      hash.keys + extra
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
everyday-cli-utils-0.5.0 lib/everyday-cli-utils/ask.rb
everyday-cli-utils-0.4.0 lib/everyday-cli-utils/ask.rb
everyday-cli-utils-0.3.0 lib/everyday-cli-utils/ask.rb
everyday-cli-utils-0.2.1 lib/everyday-cli-utils/ask.rb
everyday-cli-utils-0.2.0 lib/everyday-cli-utils/ask.rb
everyday-cli-utils-0.1.0 lib/everyday-cli-utils/ask.rb
everyday-cli-utils-0.0.2 lib/everyday-cli-utils/ask.rb