Sha256: 216813aa20d0f7bd480ca9b9d8fae6433a0f95bf3dd69c67ec8d5cbe94bb5049

Contents?: true

Size: 818 Bytes

Versions: 1

Compression:

Stored size: 818 Bytes

Contents

module Cotta
  # A command line interface tha can
  # be stubbed out by InMemorySystem
  class CommandInterface
    def initialize(io = SystemIo.new)
      @io = io
    end

    def puts(content)
      @io.puts(content)
    end

    def prompt(question)
      puts(question)
      gets
    end

    def prompt_for_choice(question, candidates)
      puts(question)
      0.upto(candidates.size - 1) do |index|
        puts "[#{index + 1}] #{candidates[index]}"
      end
      answer = gets.to_i
      seletion = nil
      if answer > 0 && answer <= candidates.size
        selection = candidates[answer - 1]
      end
      return selection
    end

    def gets
      @io.gets
    end

  end

  class SystemIo
    def puts(content)
      $stdout.puts(content)
    end

    def gets
      $stdin.gets
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cotta-1.0.0 lib/cotta/impl/command_interface.rb