Start work on VT LineBuf in Go

This commit is contained in:
Kovid Goyal 2025-02-07 20:09:00 +05:30
parent 244f4597dc
commit b0c41a70bd
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 76 additions and 0 deletions

View file

@ -253,6 +253,7 @@ def make_bitfields() -> None:
f'width {WIDTH_BITS}', f'x {WIDTH_BITS + SCALE_BITS + 1}', f'y {SCALE_BITS + 1}', 'vertical_align 3',
)
make_bitfield('tools/vt', 'CellColor', 'is_idx 1', 'red 8', 'green 8', 'blue 8')
make_bitfield('tools/vt', 'LineAttrs', 'prompt_kind 2',)
# }}}
# Completions {{{

21
tools/vt/line.go Normal file
View file

@ -0,0 +1,21 @@
package vt
import (
"fmt"
)
var _ = fmt.Print
type PromptKind uint8
const (
UNKNOWN_PROMPT_KIND PromptKind = iota
PROMPT_START
SECONDARY_PROMPT
OUTPUT_START
)
type Line struct {
Cells []Cell
Attrs LineAttrs
}

54
tools/vt/linebuf.go Normal file
View file

@ -0,0 +1,54 @@
package vt
import (
"fmt"
)
var _ = fmt.Print
type LineBuf struct {
cells []Cell
attrs []LineAttrs
line_map []uint
xnum, ynum uint
}
func NewLineBuf(xnum, ynum uint) *LineBuf {
lm := make([]uint, ynum, ynum)
var i uint
for i = 0; i < ynum; i++ {
lm[i] = i
}
return &LineBuf{
cells: make([]Cell, xnum*ynum, xnum*ynum), attrs: make([]LineAttrs, ynum, ynum), xnum: xnum, ynum: ynum, line_map: lm,
}
}
func (self *LineBuf) Line(y uint) Line {
idx := self.line_map[y]
return Line{Attrs: self.attrs[y], Cells: self.cells[idx*self.xnum : (idx+1)*self.xnum]}
}
func (self *LineBuf) AddLines(n uint) {
ynum := self.ynum + n
if uint(cap(self.line_map)) >= ynum {
self.line_map = self.line_map[:ynum]
self.attrs = self.attrs[:ynum]
self.cells = self.cells[:ynum*self.xnum]
} else {
const extra_capacity uint = 2
newattrs := make([]LineAttrs, ynum, ynum+extra_capacity)
copy(newattrs, self.attrs)
newlinemap := make([]uint, ynum, ynum+extra_capacity)
copy(newlinemap, self.line_map)
for n = self.ynum; n < ynum; n++ {
newlinemap[n] = n
}
newcells := make([]Cell, self.xnum*ynum, self.xnum*(ynum+extra_capacity))
copy(newcells, self.cells)
self.attrs = newattrs
self.line_map = newlinemap
self.cells = newcells
}
self.ynum = ynum
}