Sha256: d5f4f321fe99532818d505a35ea2b004ab99eafc685eebdcff492c0082789cfa

Contents?: true

Size: 1.28 KB

Versions: 15

Compression:

Stored size: 1.28 KB

Contents

# Code from https://github.com/wycats/thor/pull/317

class Thor
  module Shell
    class Basic
      def ask(statement, *args)
        options = args.last.is_a?(Hash) ? args.pop : {}
        color = args.first

        if options[:limited_to]
          ask_filtered(statement, color, options)
        else
          ask_simply(statement, color, options)
        end
      end

      protected

      def ask_simply(statement, color, options)
        default = options[:default]
        message = [statement, ("(#{default.inspect})" if default), nil].uniq.join(" ")
        say(message, color)
        result = stdin.gets

        return unless result

        result.strip!

        if default && result == ""
          default
        else
          result
        end
      end

      def ask_filtered(statement, color, options)
        answer_set = options[:limited_to]
        correct_answer = nil
        until correct_answer
          answer = ask_simply("#{statement} #{answer_set.inspect}", color, options)
          correct_answer = answer_set.include?(answer) ? answer : nil
          answers = answer_set.map(&:inspect).join(", ")
          say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer
        end
        correct_answer
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
motherbrain-1.5.0 lib/mb/thor_ext.rb
motherbrain-1.4.0 lib/mb/thor_ext.rb
motherbrain-1.3.0 lib/mb/thor_ext.rb
motherbrain-1.2.1 lib/mb/thor_ext.rb
motherbrain-1.2.0 lib/mb/thor_ext.rb
motherbrain-1.1.3 lib/mb/thor_ext.rb
motherbrain-1.1.2 lib/mb/thor_ext.rb
motherbrain-1.1.1 lib/mb/thor_ext.rb
motherbrain-1.1.0 lib/mb/thor_ext.rb
motherbrain-1.0.0 lib/mb/thor_ext.rb
motherbrain-0.14.5 lib/mb/thor_ext.rb
motherbrain-0.14.4 lib/mb/thor_ext.rb
motherbrain-0.14.3 lib/mb/thor_ext.rb
motherbrain-0.14.2 lib/mb/thor_ext.rb
motherbrain-0.13.1 lib/mb/thor_ext.rb