Sha256: c61d7aab26d59fc43415a38102ccfc587065f087093153c256618358f52b901c

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

require 'timeout'

module Lucid
  class ContextLoader

    module InterfaceIO
      attr_writer :visitor

      def puts(*messages)
        @visitor.puts(*messages)
      end

      def ask(question, timeout_seconds)
        STDOUT.puts(question)
        STDOUT.flush
        puts(question)

        if(Lucid::JRUBY)
          answer = jruby_gets(timeout_seconds)
        else
          answer = mri_gets(timeout_seconds)
        end

        if(answer)
          puts(answer)
          answer
        else
          raise("Lucid waited for input for #{timeout_seconds} seconds, then timed out.")
        end
      end

      def embed(src, mime_type, label)
        @visitor.embed(src, mime_type, label)
      end

      private

      def mri_gets(timeout_seconds)
        begin
          Timeout.timeout(timeout_seconds) do
            STDIN.gets
          end
        rescue Timeout::Error
          nil
        end
      end

      def jruby_gets(timeout_seconds)
        answer = nil
        t = java.lang.Thread.new do
          answer = STDIN.gets
        end
        t.start
        t.join(timeout_seconds * 1000)
        answer
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lucid-0.5.1 lib/lucid/interface_io.rb
lucid-0.4.1 lib/lucid/interface_io.rb
lucid-0.4.0 lib/lucid/interface_io.rb