Class: Fatty::OutputBuffer

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

Defined Under Namespace

Classes: Fragment

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.



12
13
14
15
16
17
18
# File 'lib/fatty/output_buffer.rb', line 12

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

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



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

def lines
  @lines
end

#max_linesObject

Returns the value of attribute max_lines.



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

def max_lines
  @max_lines
end

Instance Method Details

#append(text, role: nil) ⇒ 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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fatty/output_buffer.rb', line 32

def append(text, role: nil)
  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, role: role)
    end
  end

  ntrimmed
end

#clearObject



20
21
22
23
24
25
26
# File 'lib/fatty/output_buffer.rb', line 20

def clear
  @lines.clear
  @fragments.clear
  @scroll = 0
  @line_open = false
  nil
end

#fragments_for(index) ⇒ Object



52
53
54
# File 'lib/fatty/output_buffer.rb', line 52

def fragments_for(index)
  @fragments[index] || []
end

#scroll_downObject



65
66
67
# File 'lib/fatty/output_buffer.rb', line 65

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

#scroll_upObject



61
62
63
# File 'lib/fatty/output_buffer.rb', line 61

def scroll_up
  @scroll += 1
end

#visible_lines(height) ⇒ Object



56
57
58
59
# File 'lib/fatty/output_buffer.rb', line 56

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