Sha256: 70555564582a1e546556ed31ee19812696bba048df6b352725179f17a02fe78a

Contents?: true

Size: 1.99 KB

Versions: 41

Compression:

Stored size: 1.99 KB

Contents

require "interact"

module VMC
  module Interactive
    include ::Interactive::Rewindable

    def ask(question, options = {})
      if force? and options.key?(:default)
        options[:default]
      else
        super
      end
    end

    def list_choices(choices, options = {})
      choices.each_with_index do |o, i|
        puts "#{c(i + 1, :number)}: #{show_choice(o, options)}"
      end
    end

    def input_state(options)
      if options.key? :default
        answer = show_default(options)
      end

      CFState.new(options, answer)
    end

    def show_default(options)
      case options[:default]
      when true
        "y"
      when false
        "n"
      when nil
        ""
      else
        show_choice(options[:default], options)
      end
    end

    def prompt(question, options)
      value = show_default(options)

      print "#{question}"
      print c("> ", :prompt)

      unless value.empty?
        print "#{d(value) + "\b" * value.size}"
      end
    end

    def handler(event, state)
      ans = state.answer
      pos = state.position

      exit if event == :eof

      if state.default?
        if event.is_a?(Array) and event[0] == :key
          # initial non-movement keypress clears default answer
          clear_input(state)
        else
          # wipe away any coloring
          redraw_input(state)
        end

        state.clear_default!

        # tab with a default accepts it and moves to the end
        if event == :tab
          state.goto(ans.size)
        else
          super
        end
      else
        super
      end

      print "\n" if event == :enter
    end

    class CFState < ::Interactive::InputState
      def initialize(options = {}, answer = nil, position = 0)
        @options = options
        @answer = answer || ""
        @default = options.key? :default
        @position = position
        @done = false
      end

      def clear_default!
        @default = false
      end

      def default?
        @default
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
vmc-0.4.0.beta.93 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.92 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.91 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.90 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.89 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.88 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.87 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.86 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.85 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.84 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.83 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.82 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.81 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.80 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.79 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.78 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.77 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.76 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.75 vmc-ng/lib/vmc/cli/interactive.rb
vmc-0.4.0.beta.74 vmc-ng/lib/vmc/cli/interactive.rb