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][\n\r]?/.freeze
- MAX_READ_CHAR =
How must characters to read at a time.
1_000_000
Instance Attribute Summary collapse
-
#streams ⇒ Array<IO>
readonly
access to the array of streams.
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 Attribute Details
#streams ⇒ Array<IO> (readonly)
access to the array of streams. Be carefull accessing these, because you may prevent line_reader to run correctly.
34 35 36 |
# File 'lib/long-command-runner/line_reader.rb', line 34 def streams @streams end |
Instance Method Details
#[](index) ⇒ Array<String>
Access to the lines of a stream
75 76 77 |
# File 'lib/long-command-runner/line_reader.rb', line 75 def [](index) @streams_lines[index].dup end |
#eof? ⇒ Boolean
Are all the streams reached End-Of-File ?
59 60 61 62 63 64 65 66 67 |
# File 'lib/long-command-runner/line_reader.rb', line 59 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:
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/long-command-runner/line_reader.rb', line 45 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 |