-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.go
181 lines (137 loc) · 3.95 KB
/
buffer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package bento
import (
"math"
"slices"
"github.com/rivo/uniseg"
)
type Buffer struct {
area Rect
// content of the buffer. The length of this Vec should always be equal to [Area.Width] * [Area.Height]
content []Cell
}
func NewBufferEmpty(area Rect) Buffer {
return NewBufferFilled(area, NewEmptyCell())
}
func NewBufferFilled(area Rect, cell Cell) Buffer {
size := area.Area()
content := make([]Cell, 0, size)
for i := 0; i < size; i++ {
content = append(content, cell)
}
return Buffer{
area: area,
content: content,
}
}
// Area of the buffer
func (b *Buffer) Area() Rect {
return b.area
}
// Diff builds a minimal sequence of coordinates and Cells necessary to update the UI from
// self to other.
//
// We're assuming that buffers are well-formed, that is no double-width cell is followed by
// a non-blank cell.
func (b *Buffer) Diff(other *Buffer) []PositionedCell {
prevBuffer := b.content
nextBuffer := other.content
var (
updates []PositionedCell
invalidated int
toSkip int
)
for i := 0; i < min(len(nextBuffer), len(prevBuffer)); i++ {
current := nextBuffer[i]
previous := prevBuffer[i]
if !current.Skip && (current != previous || invalidated > 0) && toSkip == 0 {
pos := b.PosOf(i)
updates = append(updates, PositionedCell{
Cell: nextBuffer[i],
Position: pos,
})
}
previousWidth := uniseg.StringWidth(previous.Symbol)
currentWidth := uniseg.StringWidth(current.Symbol)
toSkip = max(0, currentWidth-1)
affectedWidth := max(previousWidth, currentWidth)
invalidated = max(0, max(invalidated, affectedWidth)-1)
}
return updates
}
func (b *Buffer) PosOf(index int) Position {
if index >= len(b.content) {
panic("trying to get coords of a cell outside the buffer")
}
x := index%b.area.Width + b.area.X
y := index/b.area.Width + b.area.Y
return Position{X: x, Y: y}
}
// SetString prints a string, starting at the position (x, y)
func (b *Buffer) SetString(x, y int, value string, style Style) {
b.SetStringN(x, y, value, math.MaxInt, style)
}
func (b *Buffer) Reset() {
for i := range b.content {
b.content[i].Reset()
}
}
// SetStringN prints at most the first n characters of a string if enough space is available
// until the end of the line. Skips zero-width graphemes and control characters.
//
// Use [Buffer.SetString] when the maximum amount of characters can be printed.
func (b *Buffer) SetStringN(x, y int, value string, maxWidth int, style Style) (int, int) {
remainingWidth := min(maxWidth, max(0, b.area.Right()-x))
graphemes := uniseg.NewGraphemes(value)
for remainingWidth > 0 && graphemes.Next() {
symbol := graphemes.Str()
width := graphemes.Width()
remainingWidth -= width
b.CellAt(Position{x, y}).SetSymbol(symbol).SetStyle(style)
nextSymbol := x + width
x++
for x < nextSymbol {
b.CellAt(Position{x, y}).Reset()
x++
}
}
return x, y
}
func (b *Buffer) CellAt(position Position) *Cell {
return &b.content[b.indexOf(position)]
}
// indexOf returns the (global) coordinates of a cell given its index
//
// Global coordinates are offset by the Buffer's area offset (`x`/`y`).
//
// Panics when given an index that is outside the Buffer's content.
func (b *Buffer) indexOf(position Position) int {
if !b.area.Contains(position) {
panic("position out of bounds")
}
// remove offset
y := max(0, position.Y-b.area.Y)
x := max(0, position.X-b.area.X)
width := b.area.Width
return y*width + x
}
func (b *Buffer) Resize(area Rect) {
length := area.Area()
if len(b.content) > length {
b.content = slices.Delete(b.content, length, len(b.content))
} else {
toAdd := length - len(b.content)
for i := 0; i < toAdd; i++ {
b.content = append(b.content, NewEmptyCell())
}
}
b.area = area
}
func (b *Buffer) SetStyle(area Rect, style Style) {
area = b.area.Intersection(area)
for y := area.Top(); y < area.Bottom(); y++ {
for x := area.Left(); x < area.Right(); x++ {
pos := Position{X: x, Y: y}
b.CellAt(pos).SetStyle(style)
}
}
}