Class: Termfront::AsyncWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/termfront/async_writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ AsyncWriter

Returns a new instance of AsyncWriter.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/termfront/async_writer.rb', line 5

def initialize(io)
  @io = io
  @queue = Queue.new
  @closed = false
  @thread = Thread.new do
    Thread.current.report_on_exception = false
    while (data = @queue.pop)
      begin
        TerminalOutput.write_all(@io, data)
      rescue IOError, Errno::EBADF, Errno::EPIPE
        break
      end
    end
  end
end

Instance Method Details

#closeObject



37
38
39
40
41
42
43
# File 'lib/termfront/async_writer.rb', line 37

def close
  return if @closed

  @closed = true
  @queue.push(nil)
  @thread.join
end

#raw(&block) ⇒ Object



33
34
35
# File 'lib/termfront/async_writer.rb', line 33

def raw(&block)
  @io.raw(&block)
end

#syswrite(data) ⇒ Object

Raises:

  • (IOError)


21
22
23
24
25
26
27
# File 'lib/termfront/async_writer.rb', line 21

def syswrite(data)
  raise IOError, "writer closed" if @closed

  @queue.clear if @queue.size >= 1
  @queue.push(data)
  data.bytesize
end

#winsizeObject



29
30
31
# File 'lib/termfront/async_writer.rb', line 29

def winsize
  @io.winsize
end