-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
494 lines (384 loc) · 10.7 KB
/
context.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package gui
import (
"context"
"math"
"time"
"github.com/nsf/termbox-go"
)
type (
Context struct {
cells *[][]Cell
defaultCell *Cell
viewPositionX *int
viewPositionY *int
viewSizeX *int
viewSizeY *int
stateIndex *int
states *[]State
killChannel chan struct{}
handlerIndex int
context context.Context
cancelFunc context.CancelFunc
}
)
func newContext(defaultCell Cell) (*Context, error) {
cells := make([][]Cell, 0)
viewPositionX := 0
viewPositionY := 0
viewSizeX := 0
viewSizeY := 0
stateIndex := 0
states := []State{NoState}
killChannel := make(chan struct{})
handlerIndex := 0
context, cancelFunc := context.WithCancel(context.Background())
ctx := Context{
cells: &cells,
defaultCell: &defaultCell,
viewPositionX: &viewPositionX,
viewPositionY: &viewPositionY,
viewSizeX: &viewSizeX,
viewSizeY: &viewSizeY,
stateIndex: &stateIndex,
states: &states,
killChannel: killChannel,
handlerIndex: handlerIndex,
context: context,
cancelFunc: cancelFunc,
}
return &ctx, nil
}
func (ctx *Context) newChildContext() *Context {
handlerIndex := 0
context, cancelFunc := context.WithCancel(ctx)
childContext := Context{
cells: ctx.cells,
defaultCell: ctx.defaultCell,
viewPositionX: ctx.viewPositionX,
viewPositionY: ctx.viewPositionY,
viewSizeX: ctx.viewSizeX,
viewSizeY: ctx.viewSizeY,
stateIndex: ctx.stateIndex,
states: ctx.states,
killChannel: ctx.killChannel,
handlerIndex: handlerIndex,
context: context,
cancelFunc: cancelFunc,
}
return &childContext
}
// draw
func (ctx *Context) SetViewPosition(viewPositionX, viewPositionY int) {
*ctx.viewPositionX = viewPositionX
*ctx.viewPositionY = viewPositionY
}
func (ctx *Context) UpdateViewContent() error {
foregroundAttribute := ctx.defaultCell.Foreground.toAttribute()
backgroundAttribute := ctx.defaultCell.Background.toAttribute()
// очистка видимой области
err := ctx.clearTermboxScreen(foregroundAttribute, backgroundAttribute)
if err != nil {
return err
}
// перерисовка клеток которые попадают в видимую область
viewEndY := min(*ctx.viewPositionY+*ctx.viewSizeY, len(*ctx.cells))
for y := *ctx.viewPositionY; y < viewEndY; y++ {
viewEndX := min(*ctx.viewPositionX+*ctx.viewSizeX, len((*ctx.cells)[y]))
for x := *ctx.viewPositionX; x < viewEndX; x++ {
ctx.setTermboxCell(x, y, (*ctx.cells)[y][x])
}
}
return nil
}
func (ctx *Context) SetCell(x, y int, cell Cell) {
ctx.setlocalCell(x, y, cell)
ctx.setTermboxCell(x, y, cell)
}
func (ctx *Context) setlocalCell(x, y int, cell Cell) {
// добавление недостающих строк
maxY := len(*ctx.cells) - 1
if y > maxY {
*ctx.cells = growSlice(*ctx.cells, y+1)
newRows := y - maxY
for range newRows {
*ctx.cells = append(*ctx.cells, []Cell{})
}
}
// добавление недостающего столбца в нужную строку
maxX := len((*ctx.cells)[y]) - 1
if x > maxX {
(*ctx.cells)[y] = growSlice((*ctx.cells)[y], x+1)
newColumns := x - maxX
for range newColumns {
(*ctx.cells)[y] = append((*ctx.cells)[y], *ctx.defaultCell)
}
}
(*ctx.cells)[y][x] = cell
}
func (ctx *Context) setTermboxCell(x, y int, cell Cell) {
// отрисовка клетки со смещением по видимой области
x -= *ctx.viewPositionX
y -= *ctx.viewPositionY
foregroundAttribute := cell.Foreground.toAttribute()
backgroundAttribute := cell.Background.toAttribute()
termbox.SetCell(x, y, cell.Symbol, foregroundAttribute, backgroundAttribute)
}
func (ctx *Context) SetText(x, y int, text string, foreground, background Color) {
ctx.setLocalText(x, y, text, foreground, background)
ctx.setTermboxText(x, y, text, foreground, background)
}
func (ctx *Context) setLocalText(x, y int, text string, foreground, background Color) {
// добавление недостающих строк
maxY := len(*ctx.cells) - 1
if y > maxY {
*ctx.cells = growSlice(*ctx.cells, y+1)
newRows := y - maxY
for range newRows {
*ctx.cells = append(*ctx.cells, []Cell{})
}
}
// добавление недостающих столбцов в нужную строку
maxX := len((*ctx.cells)[y]) - 1
newMaxX := x + len(text)
if newMaxX > maxX {
(*ctx.cells)[y] = growSlice((*ctx.cells)[y], newMaxX+1)
newColumns := newMaxX - maxX
for range newColumns {
(*ctx.cells)[y] = append((*ctx.cells)[y], *ctx.defaultCell)
}
}
textCell := Cell{
Foreground: foreground,
Background: background,
}
for _, symbox := range text {
textCell.Symbol = symbox
(*ctx.cells)[y][x] = textCell
x++
}
}
func (ctx *Context) setTermboxText(x, y int, text string, foreground, background Color) {
textCell := Cell{
Foreground: foreground,
Background: background,
}
for _, symbol := range text {
textCell.Symbol = symbol
ctx.setTermboxCell(x, y, textCell)
x++
}
}
func (ctx *Context) SetRow(y int, cells []Cell) {
ctx.ClearRow(y)
ctx.setLocalRow(y, cells)
ctx.setTermboxRow(y, cells)
}
func (ctx *Context) setLocalRow(y int, cells []Cell) {
// добавление недостающих строк
maxY := len(*ctx.cells) - 1
if y > maxY {
*ctx.cells = growSlice(*ctx.cells, y+1)
newRows := y - maxY
for range newRows {
*ctx.cells = append(*ctx.cells, []Cell{})
}
}
(*ctx.cells)[y] = cells
}
func (ctx *Context) setTermboxRow(y int, cells []Cell) {
// отрисовка всех клеток новой строки
for x := *ctx.viewPositionX; x < len(cells); x++ {
ctx.setTermboxCell(x, y, cells[x])
}
}
func (ctx *Context) SetColumn(x int, cells []Cell) {
ctx.ClearColumn(x)
ctx.setLocalColumn(x, cells)
ctx.setTermboxColumn(x, cells)
}
func (ctx *Context) setLocalColumn(x int, cells []Cell) {
// добавление недостающих строк
maxY := len(*ctx.cells) - 1
newMaxY := len(cells) - 1
if newMaxY > maxY {
*ctx.cells = growSlice(*ctx.cells, newMaxY+1)
newRows := newMaxY - maxY
for range newRows {
*ctx.cells = append(*ctx.cells, make([]Cell, 0, len(cells)))
}
}
// добавление недостающих столбцов в нужные строки
for y := range len(cells) {
maxX := len((*ctx.cells)[y]) - 1
if x > maxX {
(*ctx.cells)[y] = growSlice((*ctx.cells)[y], x+1)
newColumns := x - maxX
for range newColumns {
(*ctx.cells)[y] = append((*ctx.cells)[y], *ctx.defaultCell)
}
}
}
// устанавливаем значения в столбец. Можно это было делать и в предыдущем цикле, но я не хочу засорять логику. Исправлю когда буду релизить
for y := range len(cells) {
(*ctx.cells)[y][x] = cells[y]
}
}
func (ctx *Context) setTermboxColumn(x int, cells []Cell) {
// отрисовка всех клеток нового столбца
for y := *ctx.viewPositionY; y < len(cells); y++ {
ctx.setTermboxCell(x, y, cells[y])
}
}
func (ctx *Context) GetCell(x, y int) Cell {
if y >= len(*ctx.cells) || x >= len((*ctx.cells)[y]) {
return *ctx.defaultCell
}
localCell := ctx.getLocalCell(x, y)
return localCell
}
func (ctx *Context) getLocalCell(x, y int) Cell {
cell := (*ctx.cells)[y][x]
return cell
}
// func (ctx *Context) getTermboxCell(x, y int) Cell {
// x -= *ctx.viewPositionX
// y -= *ctx.viewPositionY
// termboxCell := termbox.GetCell(x, y)
// foregroundColor := (&Color{}).fromAttribute(termboxCell.Fg)
// backgroundColor := (&Color{}).fromAttribute(termboxCell.Bg)
// cell := Cell{
// Symbol: termboxCell.Ch,
// Foreground: foregroundColor,
// Background: backgroundColor,
// }
// return cell
// }
func (ctx *Context) Flush() error {
err := termbox.Flush()
if err != nil {
return err
}
return nil
}
// clear
func (ctx *Context) Clear() error {
foregroundAttribute := ctx.defaultCell.Foreground.toAttribute()
backgroundAttribute := ctx.defaultCell.Background.toAttribute()
err := ctx.clear(foregroundAttribute, backgroundAttribute)
if err != nil {
return err
}
return nil
}
func (ctx *Context) clear(foregroundAttribute, backgroundAttribute termbox.Attribute) error {
ctx.clearLocalScreen()
err := ctx.clearTermboxScreen(foregroundAttribute, backgroundAttribute)
if err != nil {
return err
}
return nil
}
func (ctx *Context) clearLocalScreen() {
*ctx.cells = nil
}
func (ctx *Context) clearTermboxScreen(foregroundAttribute, backgroundAttribute termbox.Attribute) error {
err := termbox.Clear(foregroundAttribute, backgroundAttribute)
if err != nil {
return err
}
return nil
}
func (ctx *Context) ClearRow(y int) {
ctx.clearTermboxRow(y)
ctx.clearLocalRow(y)
}
func (ctx *Context) clearLocalRow(y int) {
maxY := len((*ctx.cells)) - 1
if y > maxY {
return
}
(*ctx.cells)[y] = nil
}
func (ctx *Context) clearTermboxRow(y int) {
maxY := len((*ctx.cells)) - 1
if y > maxY {
return
}
maxX := len((*ctx.cells)[y])
for x := range maxX {
ctx.setTermboxCell(x, y, *ctx.defaultCell)
}
}
func (ctx *Context) ClearColumn(x int) {
ctx.clearTermboxColumn(x)
ctx.clearLocalColumn(x)
}
func (ctx *Context) clearLocalColumn(x int) {
for y := range len((*ctx.cells)) {
maxX := len((*ctx.cells)[y]) - 1
if x > maxX {
continue
}
(*ctx.cells)[y][x] = *ctx.defaultCell
}
}
func (ctx *Context) clearTermboxColumn(x int) {
for y := range len((*ctx.cells)) {
maxX := len((*ctx.cells)[y]) - 1
if x > maxX {
continue
}
ctx.setTermboxCell(x, y, *ctx.defaultCell)
}
}
// state
func (ctx *Context) Abort() {
ctx.handlerIndex = math.MaxInt - 1
}
// user util
func (ctx *Context) Kill() {
ctx.killChannel <- struct{}{}
}
// util
func (ctx *Context) ViewSize() (int, int) {
return *ctx.viewSizeX, *ctx.viewSizeY
}
func (ctx *Context) setViewSize(x, y int) {
*ctx.viewSizeX = x
*ctx.viewSizeY = y
}
func (ctx *Context) getCurrentState() State {
state := (*ctx.states)[*ctx.stateIndex]
return state
}
func (ctx *Context) resetData(context *Context) {
if ctx.cancelFunc != nil {
ctx.cancelFunc()
}
ctx.cancelFunc = context.cancelFunc
ctx.handlerIndex = 0
}
func (ctx *Context) addHandlerIndex() {
ctx.handlerIndex++
}
func (ctx *Context) getHandlerIndex() int {
handlerIndex := ctx.handlerIndex
return handlerIndex
}
// имплементация интерфейса context.Context
func (ctx *Context) Deadline() (deadline time.Time, ok bool) {
return ctx.context.Deadline()
}
func (ctx *Context) Done() <-chan struct{} {
return ctx.context.Done()
}
func (ctx *Context) Err() error {
return ctx.context.Err()
}
func (ctx *Context) Value(key any) any {
return ctx.context.Value(key)
}
// отмена контекста
func (ctx *Context) Cancel() {
ctx.cancelFunc()
}