Skip to content

Commit 86456f2

Browse files
committed
Add on_read
1 parent fc4af13 commit 86456f2

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

README.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,26 @@ You can easily read and write data interactively:
6767
>> runner.read(:how => :partial) # block until some output available
6868
=> ".\n.\n.\n.\n.\n.\n.\n.\n.\n"
6969
>> runner.read(:how => :nonblock)
70-
=> ""
70+
=> nil
7171
>> runner.read # block until all output available
72-
=> [truncated]
72+
=> ".\n[...]"
73+
74+
## Reactive output
75+
76+
You can also receive real-time notifications as data becomes available:
77+
78+
>> runner = Rubysh(
79+
'examples/on_read_example.sh',
80+
Rubysh.stdout > :stdout, Rubysh.stderr > :stderr,
81+
on_read: Proc.new {|target, data| puts "[#{target}]: #{data}"}
82+
)
83+
=> Command: examples/on_read_example.sh >:stdout 2>:stderr {:on_read=>#<Proc:0x007f8ad3bc5790@(irb):4>}
84+
>> runner.run
85+
[stdout]: [1] Hello from stdout
86+
[stderr]: [1] Hello from stderr
87+
[stdout]: [2] Hello from stdout
88+
[stderr]: [2] Hello from stderr
89+
[...]
7390

7491
## API
7592

examples/on_read_example.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
for i in {1..10}; do
4+
echo "[$i] Hello from stdout"
5+
echo >&2 "[$i] Hello from stderr"
6+
sleep 1
7+
done

0 commit comments

Comments
 (0)