-
Notifications
You must be signed in to change notification settings - Fork 296
/
canvas.go
195 lines (163 loc) · 4.12 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
package rty
import (
"fmt"
"github.com/windmilleng/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)
}
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
}
type lineRange struct {
start int
end int
}
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 x < 0 || x >= c.width || y < 0 || y >= c.height {
panic(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) {
if x < 0 || x >= c.width || y < 0 || y >= c.height {
panic(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
}
cell := c.cells[y][x]
return cell.ch, nil, cell.style, 1
}
type SubCanvas struct {
del Canvas
startX int
startY int
width int
height int
highWater int
style tcell.Style
}
func newSubCanvas(del Canvas, startX int, startY int, width int, height int, style tcell.Style) *SubCanvas {
_, delHeight := del.Size()
if height == GROW && delHeight != GROW {
panic(fmt.Errorf("can't create a growing subcanvas from a non-growing subcanvas"))
}
r := &SubCanvas{
del: del,
startX: startX,
startY: startY,
width: width,
height: height,
highWater: -1,
style: style,
}
r.fill(-1)
return r
}
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 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
c.fill(oldHighWater)
}
return c.del.SetContent(c.startX+x, c.startY+y, mainc, combc, style)
}
func (c *SubCanvas) fill(lastFilled int) {
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, 0, nil, c.style); err != nil {
panic(err)
}
}
}
}
func (c *SubCanvas) GetContent(x int, y int) (rune, []rune, tcell.Style, int) {
return c.del.GetContent(x, y)
}
type ScreenCanvas struct {
del tcell.Screen
}
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 {
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) {
return c.del.GetContent(x, y)
}