Class: Foruiman::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/foruiman/output.rb

Constant Summary collapse

MAX_BYTES =
16 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(logs, name:, stream:, pid:) ⇒ Output

Returns a new instance of Output.



8
9
10
11
12
13
14
15
16
17
# File 'lib/foruiman/output.rb', line 8

def initialize(logs, name:, stream:, pid:)
  @logs = logs
   = { name: name, stream: stream, pid: pid }
  @decoder = Foruiman::ANSI::Decoder.new(line_controls: true)
  @styles = Foruiman::ANSI::Styles.new
  @text = +""
  @previous = nil
  @dirty = false
  @split_boundary = false
end

Instance Method Details

#feed(bytes, eof: false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/foruiman/output.rb', line 19

def feed(bytes, eof: false)
  decoded = @decoder.feed(bytes, eof: eof)
  decoded.scan(/\e\[[0-9;:]*[mGCDK]|[\n\r\b]|[^\e\n\r\b]+/).each do |token|
    if token == "\n"
      if @split_boundary && !@dirty && !@previous
        @split_boundary = false
      else
        publish(true)
      end
    elsif token == "\r" || token == "\b" || token.match?(/\A\e\[\d*[GCDK]\z/)
      require_relative "output_line"
      @line ||= Foruiman::OutputLine.new(@text)
      @line.control(token, limit: MAX_BYTES / 2)
      @dirty = true
    elsif token.start_with?("\e[")
      split_record if @text.bytesize + token.bytesize > MAX_BYTES
      @text << token unless @line
      @styles.apply(token)
      @dirty = true
    else
      @line ? edit_text(token) : append_text(token)
    end
  end
  if eof
    publish(true) if @dirty || @previous
  elsif @dirty
    publish(false)
  end
end