Class: RuVim::Stream::Stdin
- Inherits:
-
RuVim::Stream
- Object
- RuVim::Stream
- RuVim::Stream::Stdin
- Defined in:
- lib/ruvim/stream/stdin.rb
Instance Attribute Summary collapse
-
#io ⇒ Object
Returns the value of attribute io.
-
#thread ⇒ Object
Returns the value of attribute thread.
Attributes inherited from RuVim::Stream
Instance Method Summary collapse
-
#initialize(io:, buffer_id:, queue:, stop_handler: nil, ¬ify) ⇒ Stdin
constructor
A new instance of Stdin.
- #status ⇒ Object
- #stop! ⇒ Object
Methods inherited from RuVim::Stream
Constructor Details
#initialize(io:, buffer_id:, queue:, stop_handler: nil, ¬ify) ⇒ Stdin
Returns a new instance of Stdin.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ruvim/stream/stdin.rb', line 7 def initialize(io:, buffer_id:, queue:, stop_handler: nil, ¬ify) super(stop_handler: stop_handler) @io = io @state = :live @thread = Thread.new do loop do chunk = io.readpartial(4096) next if chunk.nil? || chunk.empty? queue << { type: :stream_data, buffer_id: buffer_id, data: Buffer.decode_text(chunk) } notify.call end rescue EOFError unless @state == :closed queue << { type: :stream_eof, buffer_id: buffer_id } notify.call end rescue IOError, StandardError => e unless @state == :closed queue << { type: :stream_error, buffer_id: buffer_id, error: e..to_s } notify.call end end end |
Instance Attribute Details
#io ⇒ Object
Returns the value of attribute io.
5 6 7 |
# File 'lib/ruvim/stream/stdin.rb', line 5 def io @io end |
#thread ⇒ Object
Returns the value of attribute thread.
5 6 7 |
# File 'lib/ruvim/stream/stdin.rb', line 5 def thread @thread end |
Instance Method Details
#status ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/ruvim/stream/stdin.rb', line 32 def status case @state when :live then "stdin" when :closed then "stdin/EOF" when :error then "stdin/error" end end |
#stop! ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ruvim/stream/stdin.rb', line 40 def stop! io = @io; @io = nil begin io&.close unless io&.closed? rescue IOError nil end thread = @thread; @thread = nil if thread&.alive? thread.kill thread.join(0.05) end @state = :closed end |