-
Notifications
You must be signed in to change notification settings - Fork 301
/
canvas.go
229 lines (193 loc) · 4.92 KB
/
canvas.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package rty
import (
"fmt"
"github.com/gdamore/tcell"
)
// Canvases hold content.
type Canvas interface {
Size() (int, int)
SetContent(x int, y int, mainc rune, combc []rune, style tcell.Style) error
Close() (int, int)
GetContent(x, y int) (mainc rune, combc []rune, style tcell.Style, width int, err error)
}
func totalHeight(canvases []Canvas) int {
total := 0
for _, c := range canvases {
_, h := c.Size()
total += h
}
return total
}
// Implementations below
type cell struct {
ch rune
style tcell.Style
}
type TempCanvas struct {
width int
height int
cells [][]cell
style tcell.Style
}
var _ Canvas = &TempCanvas{}
func newTempCanvas(width, height int, style tcell.Style) *TempCanvas {
c := &TempCanvas{width: width, height: height}
if height != GROW {
c.cells = make([][]cell, height)
for i := 0; i < height; i++ {
c.cells[i] = c.makeRow()
}
}
return c
}
func (c *TempCanvas) Size() (int, int) {
return c.width, c.height
}
func (c *TempCanvas) Close() (int, int) {
if c.height == GROW {
c.height = len(c.cells)
}
return c.width, c.height
}
func (c *TempCanvas) makeRow() []cell {
row := make([]cell, c.width)
for i := 0; i < c.width; i++ {
row[i].style = c.style
}
return row
}
func (c *TempCanvas) SetContent(x int, y int, mainc rune, combc []rune, style tcell.Style) error {
if mainc == 0 {
mainc = ' '
}
if x < 0 || x >= c.width || y < 0 || y >= c.height {
return fmt.Errorf("cell %v,%v outside canvas %v,%v", x, y, c.width, c.height)
}
for y >= len(c.cells) {
c.cells = append(c.cells, c.makeRow())
}
c.cells[y][x] = cell{ch: mainc, style: style}
return nil
}
func (c *TempCanvas) GetContent(x, y int) (mainc rune, combc []rune, style tcell.Style, width int, err error) {
if x < 0 || x >= c.width || y < 0 || y >= c.height {
return 0, nil, 0, 0, fmt.Errorf("cell %d, %d outside bounds %d, %d", x, y, c.width, c.height)
}
if y >= len(c.cells) {
return 0, nil, tcell.StyleDefault, 1, nil
}
cell := c.cells[y][x]
return cell.ch, nil, cell.style, 1, nil
}
type SubCanvas struct {
del Canvas
startX int
startY int
width int
height int
highWater int
style tcell.Style
needsFill bool
}
func newSubCanvas(del Canvas, startX int, startY int, width int, height int, style tcell.Style) (*SubCanvas, error) {
_, delHeight := del.Size()
if height == GROW && delHeight != GROW {
return nil, fmt.Errorf("can't create a growing subcanvas from a non-growing subcanvas")
}
needsFill := true
delSubCanvas, ok := del.(*SubCanvas)
if ok {
// If this is a subcanvas of a subcanvas with the exact same style (or with
// only the foreground different), we already reset the canvas to the
// current style. No need to re-fill.
needsFill = style.Foreground(tcell.ColorDefault) !=
delSubCanvas.style.Foreground(tcell.ColorDefault)
}
r := &SubCanvas{
del: del,
startX: startX,
startY: startY,
width: width,
height: height,
highWater: -1,
style: style,
needsFill: needsFill,
}
if needsFill {
err := r.fill(-1)
if err != nil {
return nil, err
}
}
return r, nil
}
func (c *SubCanvas) Size() (int, int) {
return c.width, c.height
}
func (c *SubCanvas) Close() (int, int) {
if c.height == GROW {
c.height = c.highWater + 1
}
return c.width, c.height
}
func (c *SubCanvas) SetContent(x int, y int, mainc rune, combc []rune, style tcell.Style) error {
if mainc == 0 {
mainc = ' '
}
if x < 0 || x >= c.width || y < 0 || y >= c.height {
return fmt.Errorf("coord %d,%d is outside bounds %d,%d", x, y, c.width, c.height)
}
if c.height == GROW && y > c.highWater {
oldHighWater := c.highWater
c.highWater = y
if c.needsFill {
err := c.fill(oldHighWater)
if err != nil {
return err
}
}
}
return c.del.SetContent(c.startX+x, c.startY+y, mainc, combc, style)
}
func (c *SubCanvas) fill(lastFilled int) error {
startY := lastFilled + 1
maxY := c.height
if maxY == GROW {
maxY = c.highWater + 1
}
for y := startY; y < maxY; y++ {
for x := 0; x < c.width; x++ {
if err := c.del.SetContent(c.startX+x, c.startY+y, ' ', nil, c.style); err != nil {
return err
}
}
}
return nil
}
func (c *SubCanvas) GetContent(x int, y int) (rune, []rune, tcell.Style, int, error) {
return c.del.GetContent(x, y)
}
type ScreenCanvas struct {
del tcell.Screen
}
var _ Canvas = &ScreenCanvas{}
func newScreenCanvas(del tcell.Screen) *ScreenCanvas {
return &ScreenCanvas{del: del}
}
func (c *ScreenCanvas) Size() (int, int) {
return c.del.Size()
}
func (c *ScreenCanvas) SetContent(x int, y int, mainc rune, combc []rune, style tcell.Style) error {
if mainc == 0 {
mainc = ' '
}
c.del.SetContent(x, y, mainc, combc, style)
return nil
}
func (c *ScreenCanvas) Close() (int, int) {
return c.del.Size()
}
func (c *ScreenCanvas) GetContent(x, y int) (mainc rune, combc []rune, style tcell.Style, width int, err error) {
mainc, combc, style, width = c.del.GetContent(x, y)
return mainc, combc, style, width, nil
}