-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid.go
171 lines (156 loc) · 3.75 KB
/
grid.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
package anansi
import (
"image"
"github.com/jcorbin/anansi/ansi"
)
// Grid is a grid of screen cells.
type Grid struct {
Rect ansi.Rectangle
Stride int
Attr []ansi.SGRAttr
Rune []rune
// TODO []string for multi-rune glyphs
}
// Resize the grid to have room for n cells.
// Returns true only if the resize was a change, false if it was a no-op.
func (g *Grid) Resize(size image.Point) bool {
if size == g.Rect.Size() {
return false
}
if g.IsSub() {
if size.X > g.Stride {
size.X = g.Stride
}
if g.Stride*size.Y > len(g.Rune) {
size.Y = len(g.Rune) / g.Stride
}
g.Rect.Max = g.Rect.Min.Add(size)
} else {
if g.Rect.Min.Point == image.ZP {
g.Rect.Min = ansi.Pt(1, 1)
}
g.Rect.Max = g.Rect.Min.Add(size)
g.Stride = size.X
n := g.Stride * size.Y
if n > cap(g.Rune) {
as := make([]ansi.SGRAttr, n)
rs := make([]rune, n)
copy(as, g.Attr)
copy(rs, g.Rune)
g.Attr, g.Rune = as, rs
} else {
g.Attr = g.Attr[:n]
g.Rune = g.Rune[:n]
}
// TODO re-stride data
}
return true
}
// Clear the (maybe sub) grid; zeros all runes an attributes.
func (g Grid) Clear() {
if !g.IsSub() {
for i := range g.Rune {
g.Rune[i] = 0
g.Attr[i] = 0
}
return
}
pt := g.Rect.Min
i, _ := g.CellOffset(pt)
dx := g.Rect.Dx()
for ; pt.Y < g.Rect.Max.Y; pt.Y++ {
for pt.X = g.Rect.Min.X; pt.X < g.Rect.Max.X; pt.X++ {
g.Rune[i] = 0
g.Attr[i] = 0
i++
}
i -= dx // CR
i += g.Stride // LF
}
}
// Bounds returns the screen bounding rectangle of the grid.
func (g Grid) Bounds() ansi.Rectangle {
return g.Rect
}
// CellOffset returns the offset of the screen cell and true if it's
// within the Grid's Bounds().
func (g Grid) CellOffset(pt ansi.Point) (int, bool) {
if !pt.In(g.Bounds()) {
return 0, false
}
p := pt.ToImage() // convert to normal 0-indexed point
return p.Y*g.Stride + p.X, true
}
// IsSub returns true if the grid's bounding rectangle only covers a
// sub-section of its underlying data.
func (g *Grid) IsSub() bool {
return g.Rect.Size() != g.fullSize()
}
func (g *Grid) fullSize() image.Point {
if g.Stride == 0 {
return image.ZP
}
return image.Pt(g.Stride, len(g.Rune)/g.Stride)
}
// Full returns the full grid containing the receiver grid, reversing any
// sub-grid targeting done by SubRect().
func (g Grid) Full() Grid {
g.Rect.Min = ansi.Pt(1, 1)
g.Rect.Max = g.Rect.Min.Add(g.fullSize())
return g
}
// SubAt is a convenience for calling SubRect with at as the new Min point, and
// the receiver's Rect.Max point.
func (g Grid) SubAt(at ansi.Point) Grid {
return g.SubRect(ansi.Rectangle{Min: at, Max: g.Rect.Max})
}
// SubSize is a convenience for calling SubRect with a Max point determined by
// adding the given size to the receiver's Rect.Min point.
func (g Grid) SubSize(sz image.Point) Grid {
return g.SubRect(ansi.Rectangle{Min: g.Rect.Min, Max: g.Rect.Min.Add(sz)})
}
// SubRect returns a sub-grid, sharing the receiver's Rune/Attr/Stride data,
// but with a new bounding Rect. Clamps r.Max to g.Rect.Max, and returns the
// zero Grid if r.Min is not in g.Rect.
func (g Grid) SubRect(r ansi.Rectangle) Grid {
if !r.Min.In(g.Rect) {
return Grid{}
}
if r.Max.X > g.Rect.Max.X {
r.Max.X = g.Rect.Max.X
}
if r.Max.Y > g.Rect.Max.Y {
r.Max.Y = g.Rect.Max.Y
}
return Grid{
Attr: g.Attr,
Rune: g.Rune,
Stride: g.Stride,
Rect: r,
}
}
// Eq returns true only if the other grid has the same size and contents as the
// receiver.
func (g Grid) Eq(other Grid, zero rune) bool {
n := len(g.Rune)
if n != len(other.Rune) {
return false
}
i := 0
for ; i < n; i++ {
if g.Attr[i] != other.Attr[i] {
return false
}
gr, or := g.Rune[i], other.Rune[i]
if gr == 0 {
gr = zero
}
if or == 0 {
or = zero
}
if gr != or {
return false
}
}
return true
}