Class: Thaum::Rendering::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/thaum/rendering/buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width:, height:) ⇒ Buffer

Returns a new instance of Buffer.



10
11
12
13
14
15
16
17
# File 'lib/thaum/rendering/buffer.rb', line 10

def initialize(width:, height:)
  @width  = width
  @height = height
  @cursor = nil # [x, y] in buffer coords, or nil to hide
  # Stored row-major: @cells[y][x]. Public API takes (x:, y:) per convention,
  # but rows-of-columns makes row iteration and row_text natural.
  @cells = Array.new(height) { Array.new(width) { Cell.new } }
end

Instance Attribute Details

#cursorObject

Returns the value of attribute cursor.



8
9
10
# File 'lib/thaum/rendering/buffer.rb', line 8

def cursor
  @cursor
end

#heightObject (readonly)

Returns the value of attribute height.



7
8
9
# File 'lib/thaum/rendering/buffer.rb', line 7

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



7
8
9
# File 'lib/thaum/rendering/buffer.rb', line 7

def width
  @width
end

Instance Method Details

#cell(x:, y:) ⇒ Object



19
20
21
# File 'lib/thaum/rendering/buffer.rb', line 19

def cell(x:, y:)
  @cells[y][x]
end

#cover?(x:, y:) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/thaum/rendering/buffer.rb', line 38

def cover?(x:, y:)
  x >= 0 && x < @width && y >= 0 && y < @height
end

#row_text(y:) ⇒ Object



23
24
25
# File 'lib/thaum/rendering/buffer.rb', line 23

def row_text(y:)
  @cells[y].map(&:char).join
end

#set(x:, y:, char:, style: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/thaum/rendering/buffer.rb', line 27

def set(x:, y:, char:, style: nil)
  return unless cover?(x:, y:)

  current = @cells[y][x]
  # When both the existing and incoming chars are box-drawing glyphs,
  # merge their segments so adjacent borders form correct junctions.
  # For any other pair, the new char wins (existing behavior).
  resolved = BoxDrawing.merge(existing: current.char, incoming: char)
  @cells[y][x] = current.with(char: resolved, style: style || current.style)
end

#to_ansi_snapshotObject

One line per row, with inline ANSI style transitions. Each row resets style at the end so rows are independent. Use for visual assertions including color.



51
52
53
# File 'lib/thaum/rendering/buffer.rb', line 51

def to_ansi_snapshot
  (0...@height).map { |y| ansi_row(y) }.join("\n")
end

#to_text_snapshotObject

One line per row, plain text — no ANSI sequences. Use for content and layout assertions in snapshot tests.



44
45
46
# File 'lib/thaum/rendering/buffer.rb', line 44

def to_text_snapshot
  (0...@height).map { |y| row_text(y: y) }.join("\n")
end