Class: RuVim::Stream::Git

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

Instance Attribute Summary collapse

Attributes inherited from RuVim::Stream

#state, #stop_handler

Instance Method Summary collapse

Methods inherited from RuVim::Stream

#command, #live?, #status

Constructor Details

#initialize(cmd:, root:, buffer_id:, queue:, stop_handler: nil, &notify) ⇒ Git

Returns a new instance of Git.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruvim/stream/git.rb', line 7

def initialize(cmd:, root:, buffer_id:, queue:, stop_handler: nil, &notify)
  super(stop_handler: stop_handler)
  @io = nil
  stream = self
  @thread = Thread.new do
    IO.popen(cmd, chdir: root, err: [:child, :out]) do |io|
      stream.io = io
      while (chunk = io.read(4096))
        queue << { type: :stream_data, buffer_id: buffer_id, data: Buffer.decode_text(chunk) }
        notify.call
      end
    end
    stream.io = nil
    queue << { type: :stream_eof, buffer_id: buffer_id }
    notify.call
  rescue StandardError => e
    stream.io = nil
    queue << { type: :stream_error, buffer_id: buffer_id, error: e.message.to_s }
    notify.call
  end
end

Instance Attribute Details

#ioObject

Returns the value of attribute io.



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

def io
  @io
end

#threadObject

Returns the value of attribute thread.



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

def thread
  @thread
end

Instance Method Details

#stop!Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruvim/stream/git.rb', line 29

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
end