Sha256: 3d151fc6638ab23a0cf93cd86fab64d10c1a8ae8fb277192e06b0f2842b147f4

Contents?: true

Size: 1.13 KB

Versions: 11

Compression:

Stored size: 1.13 KB

Contents

require 'timeout'

module Lucid
  class Runtime

    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

11 entries across 11 versions & 1 rubygems

Version Path
lucid-0.3.3 lib/lucid/runtime/interface_io.rb
lucid-0.3.0 lib/lucid/runtime/interface_io.rb
lucid-0.2.1 lib/lucid/runtime/interface_io.rb
lucid-0.2.0 lib/lucid/runtime/interface_io.rb
lucid-0.1.1 lib/lucid/runtime/interface_io.rb
lucid-0.1.0 lib/lucid/runtime/interface_io.rb
lucid-0.0.9 lib/lucid/runtime/interface_io.rb
lucid-0.0.8 lib/lucid/runtime/interface_io.rb
lucid-0.0.7 lib/lucid/runtime/interface_io.rb
lucid-0.0.6 lib/lucid/runtime/interface_io.rb
lucid-0.0.5 lib/lucid/runtime/interface_io.rb