Files
wen/internal/editor/buffer/render.go
T
2026-10-26 02:32:03 +00:00

52 lines
1.2 KiB
Go

package buffer
// Output holds the visible lines for a terminal viewport and the cursor
// position adjusted for scrolling.
// Unlike Cursor, Glyph and Line are 1-indexed terminal coordinates for ANSI
// cursor positioning.
type Output struct {
Glyph int
Line int
Lines []string
}
// TODO: wrap lines.
// TODO: add highlighting.
func (b *Buffer) Render(height, width int) *Output {
// Vertical scroll.
if b.cursor.line < b.offset.line {
b.offset.line = b.cursor.line
}
if b.cursor.line >= height+b.offset.line {
b.offset.line = b.cursor.line - height + 1
}
// Horizontal scroll.
if b.cursor.glyph < b.offset.glyph {
b.offset.glyph = b.cursor.glyph
}
if b.cursor.glyph >= width+b.offset.glyph {
b.offset.glyph = b.cursor.glyph - width + 1
}
// Generate lines.
var lines = []string{}
for i := b.offset.line; i < height+b.offset.line; i++ {
// Return empty lines for indexes past the buffer's lines.
if i >= len(b.lines) {
lines = append(lines, "")
continue
}
line := b.lines[i].Render(b.offset.glyph, width)
lines = append(lines, line)
}
return &Output{
// Terminals are 1-indexed.
Glyph: b.cursor.glyph - b.offset.glyph + 1,
Line: b.cursor.line - b.offset.line + 1,
Lines: lines,
}
}