Add editor packages
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user