Class: LCR::LineReader
- Inherits:
-
Object
- Object
- LCR::LineReader
- Defined in:
- lib/long-command-runner/line_reader.rb
Overview
This class aims to read over a groupe of stream and triger a block when new lines are read on one of them.
Constant Summary collapse
- LINE_END =
What is considered as a return line
/[\n\r]/.freeze
- MAX_READ_CHAR =
How must characters to read at a time.
1_000_000
Instance Method Summary collapse
-
#[](index) ⇒ Array<String>
Access to the lines of a stream.
-
#eof? ⇒ Boolean
Are all the streams reached End-Of-File ?.
-
#initialize(streams) {|io0_new_line, io1_new_line, ...| ... } ⇒ LineReader
constructor
Initializer takes an array of IO streams.
-
#read { ... } ⇒ Array<Integer>
Blocking method to read on the streams (you may call it in a dedicated thread).
Constructor Details
#initialize(streams) {|io0_new_line, io1_new_line, ...| ... } ⇒ LineReader
Initializer takes an array of IO streams. You can optionaly pass a block that will be called at each new line.
The block will be called with as must as IO stream there is to treat.
25 26 27 28 29 |
# File 'lib/long-command-runner/line_reader.rb', line 25 def initialize(streams, &on_input) @streams = streams @streams_lines = Array.new(streams.length) { [] } @on_input = on_input end |
Instance Method Details
#[](index) ⇒ Array<String>
Access to the lines of a stream
70 71 72 |
# File 'lib/long-command-runner/line_reader.rb', line 70 def [](index) @streams_lines[index].dup end |
#eof? ⇒ Boolean
Are all the streams reached End-Of-File ?
54 55 56 57 58 59 60 61 62 |
# File 'lib/long-command-runner/line_reader.rb', line 54 def eof? @streams.all? do |stream| begin Timeout.timeout(0.001) { stream.eof? } rescue Timeout::Error false end end end |
#read { ... } ⇒ Array<Integer>
Blocking method to read on the streams (you may call it in a dedicated thread).
It will stop when eof is reach. Never the less it may not be the end.
Block is optional:
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/long-command-runner/line_reader.rb', line 40 def read buffers = Array.new(@streams.length) { nil } loop do # puts "read - loop" timeout = block_given? ? yield : nil break if internal_read(buffers, timeout) end # puts "read - quitting: #{@streams_lines.map(&:length)}" @streams_lines.map(&:length) end |