-
Notifications
You must be signed in to change notification settings - Fork 2
/
canvas.go
361 lines (332 loc) · 7.2 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package midterm
import (
"fmt"
"iter"
)
type Canvas struct {
Width int
Rows []*Region
}
// Region represents a segment of a row with a specific format.
type Region struct {
// Format that applies to this region.
F Format
// Size is the number of characters to which the format applies.
Size int
// Next is the next region in the row.
Next *Region
}
func (canvas *Canvas) Height() int {
return len(canvas.Rows)
}
func (canvas *Canvas) Regions(row int) iter.Seq[*Region] {
return func(yield func(*Region) bool) {
// Check if the requested row exists
if row >= len(canvas.Rows) || row < 0 {
return
}
for r := canvas.Rows[row]; r != nil; r = r.Next {
if !yield(r) {
break
}
}
}
}
func (canvas *Canvas) Paint(row, col int, format Format) {
// dbg.Printf("PAINTING %d:%d: %q", row, col, format.Render())
for len(canvas.Rows) <= row {
// initialize empty regions up to the cursor row
canvas.Rows = append(canvas.Rows, &Region{Size: canvas.Width})
}
var pos int
var prev *Region
for region := range canvas.Regions(row) {
next := region.Next
end := pos + region.Size
if end == col {
if region.Size == 0 {
// empty row; bootstrap it
region.Size++
region.F = format
region.consumeNext()
return
}
if format == region.F {
// same format; grow existing region
region.Size++
region.consumeNext()
return
} else if next != nil && format == next.F {
// next region already has same format; nothing to do
return
} else {
// eat into the next region
region.Next = &Region{
F: format,
Size: 1,
Next: region.Next,
}
region.Next.consumeNext()
return
}
} else if col == pos {
if region.Size == 0 {
// empty row; bootstrap it
region.Size++
region.F = format
region.consumeNext()
return
}
cp := *region
*region = Region{
F: format,
Size: 1,
Next: &cp,
}
region.consumeNext()
return
} else if end > col {
if format == region.F {
// nothing to do
return
} else {
// split the region
region.Size = col - pos
origNext := region.Next
region.Next = &Region{
F: format,
Size: 1,
}
remainder := end - col - 1
if remainder > 0 {
// add remainder, followed by original next
region.Next.Next = &Region{
F: region.F,
Size: remainder,
Next: origNext,
}
} else {
// clipped the end; restore original next
region.Next.Next = origNext
}
return
}
}
pos = end
prev = region
}
// painting beyond the end of the row; insert a blank gap followed by the
// cursor.
prev.Next = &Region{
F: EmptyFormat,
Size: col - pos,
Next: &Region{
F: format,
Size: 1,
},
}
// handle empty initial region
if prev.Size == 0 {
*prev = *prev.Next
}
}
func (canvas *Canvas) Insert(row, col int, f Format, n int) {
for len(canvas.Rows) <= row {
// initialize empty regions up to the cursor row
canvas.Rows = append(canvas.Rows, &Region{Size: canvas.Width})
}
var pos int
var prev *Region
for region := range canvas.Regions(row) {
next := region.Next
end := pos + region.Size
if end == col {
if region.Size == 0 {
// empty row; bootstrap it
region.Size += n
region.F = f
return
}
if f == region.F {
// same format; grow existing region
region.Size += n
return
} else if next != nil && f == next.F {
// next region already has same format; grow it
next.Size += n
return
} else {
// insert before the next region
region.Next = &Region{
F: f,
Size: n,
Next: region.Next,
}
return
}
} else if col == pos {
if region.Size == 0 {
// empty row; bootstrap it
region.Size++
region.F = f
return
}
if f == region.F {
// same format; grow existing region
region.Size += n
return
}
cp := *region
*region = Region{
F: f,
Size: n,
Next: &cp,
}
return
} else if end > col {
if f == region.F {
// grow the current region
region.Size += n
return
} else {
// split the region
region.Size = col - pos
origNext := region.Next
region.Next = &Region{
F: f,
Size: n,
Next: &Region{
F: region.F,
Size: end - col,
Next: origNext,
},
}
return
}
}
pos = end
prev = region
}
// painting beyond the end of the row; insert a blank gap followed by the
// cursor.
prev.Next = &Region{
F: EmptyFormat,
Size: col - pos,
Next: &Region{
F: f,
Size: n,
},
}
// handle empty initial region
if prev.Size == 0 {
*prev = *prev.Next
}
}
// TODO: untested
func (canvas *Canvas) Delete(row, col, n int) {
if row >= len(canvas.Rows) {
return // Row doesn't exist, nothing to delete
}
var pos int
var prev *Region
for region := range canvas.Regions(row) {
end := pos + region.Size
if end > col {
rem := end - col
if rem > 0 {
n -= (region.Size - rem)
region.Size = rem
region = region.Next
}
// Delete the specified number of characters
for n > 0 && region != nil {
if n >= region.Size {
// Fully delete this region
n -= region.Size
if prev != nil {
prev.Next = region.Next
} else if region.Next != nil {
// Head of the row being deleted
canvas.Rows[row] = region.Next
} else {
// Just leave an empty region behind
region.Size = 0
}
region = region.Next
} else {
// Partially delete from this region
region.Size -= n
n = 0
}
}
return
}
pos = end
prev = region
}
}
func (canvas *Canvas) Resize(h, w int) {
canvas.ResizeY(h)
canvas.ResizeX(w)
}
func (canvas *Canvas) ResizeY(h int) {
// Handle height adjustment
if h < len(canvas.Rows) {
// Truncate rows if the new height is less than the current height
canvas.Rows = canvas.Rows[:h]
} else if h > len(canvas.Rows) {
// Add new empty rows if the new height is greater than the current height
for i := len(canvas.Rows); i < h; i++ {
canvas.Rows = append(canvas.Rows, &Region{Size: canvas.Width})
}
}
}
func (canvas *Canvas) ResizeX(w int) {
// Handle width adjustment
for y := 0; y < len(canvas.Rows); y++ {
row := canvas.Rows[y]
if row == nil {
continue
}
current := row
position := 0
var previous *Region
// Traverse the row to find regions that exceed the new width
for current != nil {
if position+current.Size > w {
// Case 1: The current region exceeds the new width, so truncate it
if position < w {
current.Size = w - position
current.Next = nil // Remove the rest of the row
} else {
// Case 2: The entire region is beyond the new width, so remove it
if previous != nil {
previous.Next = nil
} else {
// If this was the first region, the row becomes empty
canvas.Rows[y] = nil
}
}
break
}
position += current.Size
previous = current
current = current.Next
}
}
}
func (region *Region) String() string {
return fmt.Sprintf("%s:%d", region.F.Render(), region.Size)
}
func (region *Region) consumeNext() {
next := region.Next
if next != nil {
next.Size--
if next.Size == 0 {
region.Next = nil
if next.Next != nil {
region.Next = next.Next
}
}
}
}