Class: RuVim::Stream::Stdin

Inherits:
RuVim::Stream show all
Defined in:
lib/ruvim/stream/stdin.rb

Instance Attribute Summary collapse

Attributes inherited from RuVim::Stream

#state, #stop_handler

Instance Method Summary collapse

Methods inherited from RuVim::Stream

#command, #live?

Constructor Details

#initialize(io:, buffer_id:, queue:, stop_handler: nil, &notify) ⇒ 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, &notify)
  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.message.to_s }
      notify.call
    end
  end
end

Instance Attribute Details

#ioObject

Returns the value of attribute io.



5
6
7
# File 'lib/ruvim/stream/stdin.rb', line 5

def io
  @io
end

#threadObject

Returns the value of attribute thread.



5
6
7
# File 'lib/ruvim/stream/stdin.rb', line 5

def thread
  @thread
end

Instance Method Details

#statusObject



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