Skip to content
Browse files

Add on_read

  • Loading branch information...
1 parent fc4af13 commit 86456f26c3989b0fd9008e1095d7b3f24c23b5cc @gdb committed Feb 3, 2014
Showing with 26 additions and 2 deletions.
  1. +19 −2 README.md
  2. +7 −0 examples/on_read_example.sh
View
21 README.md
@@ -67,9 +67,26 @@ You can easily read and write data interactively:
>> runner.read(:how => :partial) # block until some output available
=> ".\n.\n.\n.\n.\n.\n.\n.\n.\n"
>> runner.read(:how => :nonblock)
- => ""
+ => nil
>> runner.read # block until all output available
- => [truncated]
+ => ".\n[...]"
+
+## Reactive output
+
+You can also receive real-time notifications as data becomes available:
+
+ >> runner = Rubysh(
+ 'examples/on_read_example.sh',
+ Rubysh.stdout > :stdout, Rubysh.stderr > :stderr,
+ on_read: Proc.new {|target, data| puts "[#{target}]: #{data}"}
+ )
+ => Command: examples/on_read_example.sh >:stdout 2>:stderr {:on_read=>#<Proc:0x007f8ad3bc5790@(irb):4>}
+ >> runner.run
+ [stdout]: [1] Hello from stdout
+ [stderr]: [1] Hello from stderr
+ [stdout]: [2] Hello from stdout
+ [stderr]: [2] Hello from stderr
+ [...]
## API
View
7 examples/on_read_example.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+for i in {1..10}; do
+ echo "[$i] Hello from stdout"
+ echo >&2 "[$i] Hello from stderr"
+ sleep 1
+done

0 comments on commit 86456f2

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