Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial commit

  • Loading branch information...
commit 8ef376f2d726b2cdd2a112e32b978040bc257a58 0 parents
Sven Fuchs authored
0  README
No changes.
10 Rakefile
... ... @@ -0,0 +1,10 @@
  1 +require 'rake'
  2 +require 'rake/testtask'
  3 +
  4 +Rake::TestTask.new do |t|
  5 + t.libs << 'lib'
  6 + t.pattern = 'test/**/*_test.rb'
  7 + t.verbose = false
  8 +end
  9 +
  10 +task :default => :test
19 em-stdout.gemspec
... ... @@ -0,0 +1,19 @@
  1 +# encoding: utf-8
  2 +
  3 +$:.unshift File.expand_path('../lib', __FILE__)
  4 +require 'em_stdout/version'
  5 +
  6 +Gem::Specification.new do |s|
  7 + s.name = "em-stdout"
  8 + s.version = EmStdout::VERSION
  9 + s.authors = ["Sven Fuchs"]
  10 + s.email = "svenfuchs@artweb-design.de"
  11 + s.homepage = "http://github.com/svenfuchs/em-stdout"
  12 + s.summary = "[summary]"
  13 + s.description = "[description]"
  14 +
  15 + s.files = `git ls-files app lib`.split("\n")
  16 + s.platform = Gem::Platform::RUBY
  17 + s.require_path = 'lib'
  18 + s.rubyforge_project = '[none]'
  19 +end
51 lib/em/stdout.rb
... ... @@ -0,0 +1,51 @@
  1 +require 'eventmachine'
  2 +
  3 +module EventMachine
  4 + class << self
  5 + def split_stdout
  6 + stdout = nil
  7 + EM.next_tick do
  8 + stdout, read, write = STDOUT.clone, *IO.pipe
  9 + EM.attach(read, Stdout) do |connection|
  10 + connection.stdout = stdout
  11 + yield connection if block_given?
  12 + end
  13 + STDOUT.reopen(write)
  14 + end
  15 + sleep(0.01) until stdout
  16 + stdout
  17 + end
  18 + end
  19 +
  20 + class Stdout < EventMachine::Connection
  21 + class << self
  22 + def output
  23 + defined?(@@output) ? @@output : @@output = true
  24 + end
  25 +
  26 + def output=(output)
  27 + @@output = output
  28 + end
  29 + end
  30 +
  31 + attr_accessor :stdout
  32 +
  33 + def callback(&block)
  34 + @callback = block
  35 + end
  36 +
  37 + def on_close(&block)
  38 + @on_close = block
  39 + end
  40 +
  41 + def receive_data(data)
  42 + stdout.print(data) if self.class.output
  43 + @callback.call(data) if @callback
  44 + end
  45 +
  46 + def unbind
  47 + STDOUT.reopen(stdout)
  48 + @on_close.call if @on_close
  49 + end
  50 + end
  51 +end
1  lib/em_stdout.rb
... ... @@ -0,0 +1 @@
  1 +require 'em/stdout'
3  lib/em_stdout/version.rb
... ... @@ -0,0 +1,3 @@
  1 +module EmStdout
  2 + VERSION = "0.0.1"
  3 +end
23 test/stdout_test.rb
... ... @@ -0,0 +1,23 @@
  1 +require 'test_helper'
  2 +require 'stringio'
  3 +require 'em/stdout'
  4 +
  5 +class EMStdoutTest < Test::Unit::TestCase
  6 + test 'splits stdout' do
  7 + out = StringIO.new('')
  8 + EM::Stdout.output = false
  9 +
  10 + EM.run do
  11 + EM.defer do
  12 + EM.split_stdout do |c|
  13 + c.callback { |data| out << data }
  14 + end
  15 + print '.'
  16 + EM.stop
  17 + end
  18 + end
  19 +
  20 + assert_equal '.', out.string
  21 + end
  22 +end
  23 +
4 test/test_helper.rb
... ... @@ -0,0 +1,4 @@
  1 +require 'ruby-debug'
  2 +require 'test/unit'
  3 +require 'test_declarative'
  4 +require 'mocha'

0 comments on commit 8ef376f

Please sign in to comment.
Something went wrong with that request. Please try again.