Class: Fatty::OutputBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/fatty/output_buffer.rb

Constant Summary collapse

DEFAULT_MAX_LINES =
10_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_lines: DEFAULT_MAX_LINES) ⇒ OutputBuffer

Returns a new instance of OutputBuffer.



10
11
12
13
14
15
# File 'lib/fatty/output_buffer.rb', line 10

def initialize(max_lines: DEFAULT_MAX_LINES)
  @lines = []
  @scroll = 0
  @max_lines = Integer(max_lines)
  @line_open = false
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



7
8
9
# File 'lib/fatty/output_buffer.rb', line 7

def lines
  @lines
end

#max_linesObject

Returns the value of attribute max_lines.



8
9
10
# File 'lib/fatty/output_buffer.rb', line 8

def max_lines
  @max_lines
end

Instance Method Details

#append(text) ⇒ Object

Append text to the output buffer. Text may contain complete lines, partial lines, or both. Returns the number of lines trimmed. The Terminal can use this to adjust the Viewport.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fatty/output_buffer.rb', line 20

def append(text)
  ntrimmed = 0
  str = text.to_s
  return ntrimmed if str.empty?

  str.split(/(\n)/).each do |part|
    if part == "\n"
      if @line_open
        @line_open = false
      else
        ntrimmed += append_new_line("")
      end
    elsif !part.empty?
      ntrimmed += append_fragment(part)
    end
  end

  ntrimmed
end

#scroll_downObject



49
50
51
# File 'lib/fatty/output_buffer.rb', line 49

def scroll_down
  @scroll -= 1 if @scroll.positive?
end

#scroll_upObject



45
46
47
# File 'lib/fatty/output_buffer.rb', line 45

def scroll_up
  @scroll += 1
end

#visible_lines(height) ⇒ Object



40
41
42
43
# File 'lib/fatty/output_buffer.rb', line 40

def visible_lines(height)
  start = [@lines.length - height - @scroll, 0].max
  @lines[start, height] || []
end