render.go (1024B)
1 package buffer 2 3 type Output struct { 4 Glyph int 5 Line int 6 Lines []string 7 } 8 9 // TODO: wrap lines. 10 // TODO: add highlighting. 11 func (b *Buffer) Render(height, width int) *Output { 12 // Vertical scroll. 13 if b.cursor.line < b.offset.line { 14 b.offset.line = b.cursor.line 15 } 16 if b.cursor.line > height+b.offset.line { 17 b.offset.line = b.cursor.line - height 18 } 19 20 // Horizontal scroll. 21 if b.cursor.glyph < b.offset.glyph { 22 b.offset.glyph = b.cursor.glyph 23 } 24 if b.cursor.glyph > width+b.offset.glyph { 25 b.offset.glyph = b.cursor.glyph - width 26 } 27 28 // Generate lines. 29 var lines = []string{} 30 for i := b.offset.line; i <= height+b.offset.line; i++ { 31 // Return empty lines for indexes past the buffer's lines. 32 if i >= len(b.lines) { 33 lines = append(lines, "") 34 continue 35 } 36 37 line := b.lines[i].Render(b.offset.glyph, width) 38 lines = append(lines, line) 39 } 40 41 return &Output{ 42 // Terminals are 1-indexed. 43 Glyph: b.cursor.glyph - b.offset.glyph + 1, 44 Line: b.cursor.line - b.offset.line + 1, 45 Lines: lines, 46 } 47 }