-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathbuffer.go
More file actions
832 lines (732 loc) Β· 21.9 KB
/
Copy pathbuffer.go
File metadata and controls
832 lines (732 loc) Β· 21.9 KB
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
package uv
import (
"bytes"
"image"
"io"
"strings"
"github.com/charmbracelet/x/ansi"
)
// Position represents a position in a coordinate system.
type Position = image.Point
// Pos is a shorthand for creating a new [Position].
func Pos(x, y int) Position {
return Position{X: x, Y: y}
}
// Rectangle represents a rectangular area.
type Rectangle = image.Rectangle
// Rect is a shorthand for creating a new [Rectangle].
func Rect(x, y, w, h int) Rectangle {
return Rectangle{Min: image.Point{X: x, Y: y}, Max: image.Point{X: x + w, Y: y + h}}
}
// Line represents cells in a line.
type Line []Cell
// NewLine creates a new line with the given width, filled with empty cells.
func NewLine(width int) Line {
l := make(Line, width)
for i := range l {
l[i] = EmptyCell
}
return l
}
// Set sets the cell at the given x position.
func (l Line) Set(x int, c *Cell) {
lineWidth := len(l)
if x < 0 || x >= lineWidth {
return
}
// When a wide cell is partially overwritten, we need
// to fill the rest of the cell with space cells to
// avoid rendering issues.
var prev *Cell
if prev = l.At(x); prev != nil { //nolint:nestif
if pw := prev.Width; pw > 1 {
// Writing to the first wide cell
for j := 0; j < pw && x+j < lineWidth; j++ {
l[x+j] = *prev
l[x+j].Empty()
}
} else if pw == 0 {
// Writing to wide cell placeholders
for j := 1; x-j >= 0; j++ {
if wide := l.At(x - j); wide != nil {
if ww := wide.Width; ww > 1 && j < ww {
for k := range ww {
l[x-j+k] = *wide
l[x-j+k].Empty()
}
break
}
}
}
}
}
if c == nil {
// Nil cells are treated as blank empty cells.
l[x] = EmptyCell
return
}
l[x] = *c
cw := c.Width
if x+cw > lineWidth {
// If the cell is too wide, we write blanks with the same style.
for i := 0; i < cw && x+i < lineWidth; i++ {
l[x+i] = *c
l[x+i].Empty()
}
return
}
if cw > 1 {
// Mark wide cells with an zero cells.
// We set the wide cell down below
for j := 1; j < cw && x+j < lineWidth; j++ {
l[x+j] = Cell{}
}
}
}
// At returns the cell at the given x position.
// If the cell does not exist, it returns nil.
func (l Line) At(x int) *Cell {
if x < 0 || x >= len(l) {
return nil
}
return &l[x]
}
// String returns the string representation of the line. Any trailing spaces
// are removed.
func (l Line) String() string {
var buf strings.Builder
var pending bytes.Buffer
for _, c := range l {
if c.IsZero() {
continue
}
if c.Equal(&EmptyCell) {
pending.WriteByte(' ')
continue
}
if pending.Len() > 0 {
buf.WriteString(pending.String())
pending.Reset()
}
buf.WriteString(c.String())
}
return buf.String()
}
// Render renders the line to a string with all the required attributes and
// styles.
func (l Line) Render() string {
var buf strings.Builder
renderLine(&buf, l)
return buf.String()
}
func renderLine(buf io.StringWriter, l Line) {
var pen Style
var link Link
var pending bytes.Buffer
for _, c := range l {
if c.IsZero() {
continue
}
if c.Equal(&EmptyCell) {
if !pen.IsZero() {
_, _ = buf.WriteString(ansi.ResetStyle)
pen = Style{}
}
if !link.IsZero() {
_, _ = buf.WriteString(ansi.ResetHyperlink())
link = Link{}
}
pending.WriteByte(' ')
continue
}
if pending.Len() > 0 {
_, _ = buf.WriteString(pending.String())
pending.Reset()
}
if c.Style.IsZero() && !pen.IsZero() {
_, _ = buf.WriteString(ansi.ResetStyle)
pen = Style{}
}
if !c.Style.Equal(&pen) {
seq := c.Style.Diff(&pen)
_, _ = buf.WriteString(seq)
pen = c.Style
}
// Write the URL escape sequence
if c.Link != link && link.URL != "" {
_, _ = buf.WriteString(ansi.ResetHyperlink())
link = Link{}
}
if c.Link != link {
_, _ = buf.WriteString(ansi.SetHyperlink(c.Link.URL, c.Link.Params))
link = c.Link
}
_, _ = buf.WriteString(c.String())
}
if link.URL != "" {
_, _ = buf.WriteString(ansi.ResetHyperlink())
}
if !pen.IsZero() {
_, _ = buf.WriteString(ansi.ResetStyle)
}
}
// Lines represents a slice of lines.
type Lines []Line
// Height returns the height of the lines.
func (ls Lines) Height() int {
return len(ls)
}
// Width returns the width of the widest line.
func (ls Lines) Width() int {
maxWidth := 0
for _, l := range ls {
maxWidth = max(maxWidth, len(l))
}
return maxWidth
}
// String returns the string representation of the lines.
func (ls Lines) String() string {
var buf strings.Builder
for i, l := range ls {
buf.WriteString(l.String())
if i < len(ls)-1 {
_ = buf.WriteByte('\n')
}
}
return buf.String()
}
// Render renders the lines to a styled string with all the required attributes
// and styles.
func (ls Lines) Render() string {
var buf strings.Builder
for i, l := range ls {
renderLine(&buf, l)
if i < len(ls)-1 {
_ = buf.WriteByte('\n')
}
}
return buf.String()
}
// Buffer represents a cell buffer that contains the contents of a screen.
type Buffer struct {
// Lines is a slice of lines that make up the cells of the buffer.
Lines []Line
}
var _ Drawable = (*Buffer)(nil)
// NewBuffer creates a new buffer with the given width and height.
// This is a convenience function that initializes a new buffer and resizes it.
func NewBuffer(width int, height int) *Buffer {
b := new(Buffer)
b.Lines = make([]Line, height)
for i := range b.Lines {
b.Lines[i] = make(Line, width)
for j := range b.Lines[i] {
b.Lines[i][j] = EmptyCell
}
}
b.Resize(width, height)
return b
}
// String returns the string representation of the buffer.
func (b *Buffer) String() string {
return Lines(b.Lines).String()
}
// Render renders the buffer to a styled string with all the required
// attributes and styles.
func (b *Buffer) Render() string {
return Lines(b.Lines).Render()
}
// Line returns a pointer to the line at the given y position.
// If the line does not exist, it returns nil.
func (b *Buffer) Line(y int) Line {
if y < 0 || y >= len(b.Lines) {
return nil
}
return b.Lines[y]
}
// CellAt returns the cell at the given position. It returns nil if the
// position is out of bounds.
func (b *Buffer) CellAt(x int, y int) *Cell {
if y < 0 || y >= len(b.Lines) {
return nil
}
return b.Lines[y].At(x)
}
// SetCell sets the cell at the given x, y position.
func (b *Buffer) SetCell(x, y int, c *Cell) {
if y < 0 || y >= len(b.Lines) {
return
}
b.Lines[y].Set(x, c)
}
// Height implements Screen.
func (b *Buffer) Height() int {
return len(b.Lines)
}
// Width implements Screen.
func (b *Buffer) Width() int {
if len(b.Lines) == 0 {
return 0
}
return len(b.Lines[0])
}
// Bounds returns the bounds of the buffer.
// The origin is always at (0, 0) and the maximum coordinates are determined by
// the width and height of the buffer.
func (b *Buffer) Bounds() Rectangle {
return Rect(0, 0, b.Width(), b.Height())
}
// Resize resizes the buffer to the given width and height.
func (b *Buffer) Resize(width int, height int) {
curWidth, curHeight := b.Width(), b.Height()
if curWidth == width && curHeight == height {
// No need to resize if the dimensions are the same.
return
}
if width > curWidth {
line := make(Line, width-curWidth)
for i := range line {
line[i] = EmptyCell
}
for i := range b.Lines {
b.Lines[i] = append(b.Lines[i], line...)
}
} else if width < curWidth {
for i := range b.Lines {
b.Lines[i] = b.Lines[i][:width]
}
}
if height > len(b.Lines) {
for i := len(b.Lines); i < height; i++ {
line := make(Line, width)
for j := range line {
line[j] = EmptyCell
}
b.Lines = append(b.Lines, line)
}
} else if height < len(b.Lines) {
b.Lines = b.Lines[:height]
}
}
// Fill fills the buffer with the given cell and rectangle.
func (b *Buffer) Fill(c *Cell) {
b.FillArea(c, b.Bounds())
}
// FillArea fills the buffer with the given cell and rectangle.
func (b *Buffer) FillArea(c *Cell, area Rectangle) {
cellWidth := 1
if c != nil && c.Width > 1 {
cellWidth = c.Width
}
for y := area.Min.Y; y < area.Max.Y; y++ {
for x := area.Min.X; x < area.Max.X; x += cellWidth {
b.SetCell(x, y, c)
}
}
}
// Clear clears the buffer with space cells and rectangle.
func (b *Buffer) Clear() {
area := b.Bounds()
for y := area.Min.Y; y < area.Max.Y; y++ {
for x := area.Min.X; x < area.Max.X; x++ {
b.Lines[y][x] = EmptyCell
}
}
}
// ClearArea clears the buffer with space cells within the specified
// rectangles. Only cells within the rectangle's bounds are affected.
func (b *Buffer) ClearArea(area Rectangle) {
b.FillArea(nil, area)
}
// CloneArea clones the area of the buffer within the specified rectangle. If
// the area is out of bounds, it returns nil.
func (b *Buffer) CloneArea(area Rectangle) *Buffer {
bounds := b.Bounds()
if !area.In(bounds) {
return nil
}
n := NewBuffer(area.Dx(), area.Dy())
for y := area.Min.Y; y < area.Max.Y; y++ {
for x := area.Min.X; x < area.Max.X; {
c := b.CellAt(x, y)
if c == nil || c.IsZero() {
x++
continue
}
n.SetCell(x-area.Min.X, y-area.Min.Y, c)
x += max(c.Width, 1)
}
}
return n
}
// Clone clones the entire buffer into a new buffer.
func (b *Buffer) Clone() *Buffer {
return b.CloneArea(b.Bounds())
}
// Draw draws the buffer to the given screen at the specified area.
// It implements the [Drawable] interface.
func (b *Buffer) Draw(scr Screen, area Rectangle) {
if area.Empty() {
return
}
// Ensure the area is within the bounds of the screen.
bounds := scr.Bounds()
if !area.Overlaps(bounds) {
return
}
for y := area.Min.Y; y < area.Max.Y; y++ {
for x := area.Min.X; x < area.Max.X; {
c := b.CellAt(x-area.Min.X, y-area.Min.Y)
if c == nil || c.IsZero() {
x++
continue
}
scr.SetCell(x, y, c)
width := c.Width
if width <= 0 {
width = 1
}
x += width
}
}
}
// InsertLine inserts n lines at the given line position, with the given
// optional cell, within the specified rectangles. If no rectangles are
// specified, it inserts lines in the entire buffer. Only cells within the
// rectangle's horizontal bounds are affected. Lines are pushed out of the
// rectangle bounds and lost. This follows terminal [ansi.IL] behavior.
// It returns the pushed out lines.
func (b *Buffer) InsertLine(y, n int, c *Cell) {
b.InsertLineArea(y, n, c, b.Bounds())
}
// InsertLineArea inserts new lines at the given line position, with the
// given optional cell, within the rectangle bounds. Only cells within the
// rectangle's horizontal bounds are affected. Lines are pushed out of the
// rectangle bounds and lost. This follows terminal [ansi.IL] behavior.
func (b *Buffer) InsertLineArea(y, n int, c *Cell, area Rectangle) {
if n <= 0 || y < area.Min.Y || y >= area.Max.Y || y >= b.Height() {
return
}
// Limit number of lines to insert to available space
if y+n > area.Max.Y {
n = area.Max.Y - y
}
// Move existing lines down within the bounds
for i := area.Max.Y - 1; i >= y+n; i-- {
for x := area.Min.X; x < area.Max.X; x++ {
// We don't need to clone c here because we're just moving lines down.
b.Lines[i][x] = b.Lines[i-n][x]
}
}
// Clear the newly inserted lines within bounds
for i := y; i < y+n; i++ {
for x := area.Min.X; x < area.Max.X; x++ {
b.SetCell(x, i, c)
}
}
}
// DeleteLineArea deletes lines at the given line position, with the given
// optional cell, within the rectangle bounds. Only cells within the
// rectangle's bounds are affected. Lines are shifted up within the bounds and
// new blank lines are created at the bottom. This follows terminal [ansi.DL]
// behavior.
func (b *Buffer) DeleteLineArea(y, n int, c *Cell, area Rectangle) {
if n <= 0 || y < area.Min.Y || y >= area.Max.Y || y >= b.Height() {
return
}
// Limit deletion count to available space in scroll region
if n > area.Max.Y-y {
n = area.Max.Y - y
}
// Shift cells up within the bounds
for dst := y; dst < area.Max.Y-n; dst++ {
src := dst + n
for x := area.Min.X; x < area.Max.X; x++ {
// We don't need to clone c here because we're just moving cells up.
b.Lines[dst][x] = b.Lines[src][x]
}
}
// Fill the bottom n lines with blank cells
for i := area.Max.Y - n; i < area.Max.Y; i++ {
for x := area.Min.X; x < area.Max.X; x++ {
b.SetCell(x, i, c)
}
}
}
// DeleteLine deletes n lines at the given line position, with the given
// optional cell, within the specified rectangles. If no rectangles are
// specified, it deletes lines in the entire buffer.
func (b *Buffer) DeleteLine(y, n int, c *Cell) {
b.DeleteLineArea(y, n, c, b.Bounds())
}
// InsertCell inserts new cells at the given position, with the given optional
// cell, within the specified rectangles. If no rectangles are specified, it
// inserts cells in the entire buffer. This follows terminal [ansi.ICH]
// behavior.
func (b *Buffer) InsertCell(x, y, n int, c *Cell) {
b.InsertCellArea(x, y, n, c, b.Bounds())
}
// InsertCellArea inserts new cells at the given position, with the given
// optional cell, within the rectangle bounds. Only cells within the
// rectangle's bounds are affected, following terminal [ansi.ICH] behavior.
func (b *Buffer) InsertCellArea(x, y, n int, c *Cell, area Rectangle) {
if n <= 0 || y < area.Min.Y || y >= area.Max.Y || y >= b.Height() ||
x < area.Min.X || x >= area.Max.X || x >= b.Width() {
return
}
// Limit number of cells to insert to available space
if x+n > area.Max.X {
n = area.Max.X - x
}
// Move existing cells within rectangle bounds to the right
for i := area.Max.X - 1; i >= x+n && i-n >= area.Min.X; i-- {
// We don't need to clone c here because we're just moving cells to the
// right.
b.Lines[y][i] = b.Lines[y][i-n]
}
// Clear the newly inserted cells within rectangle bounds
for i := x; i < x+n && i < area.Max.X; i++ {
b.SetCell(i, y, c)
}
}
// DeleteCell deletes cells at the given position, with the given optional
// cell, within the specified rectangles. If no rectangles are specified, it
// deletes cells in the entire buffer. This follows terminal [ansi.DCH]
// behavior.
func (b *Buffer) DeleteCell(x, y, n int, c *Cell) {
b.DeleteCellArea(x, y, n, c, b.Bounds())
}
// DeleteCellArea deletes cells at the given position, with the given
// optional cell, within the rectangle bounds. Only cells within the
// rectangle's bounds are affected, following terminal [ansi.DCH] behavior.
func (b *Buffer) DeleteCellArea(x, y, n int, c *Cell, area Rectangle) {
if n <= 0 || y < area.Min.Y || y >= area.Max.Y || y >= b.Height() ||
x < area.Min.X || x >= area.Max.X || x >= b.Width() {
return
}
// Calculate how many positions we can actually delete
remainingCells := area.Max.X - x
if n > remainingCells {
n = remainingCells
}
// Shift the remaining cells to the left
for i := x; i < area.Max.X-n; i++ {
if i+n < area.Max.X {
// We need to use SetCell here to ensure we blank out any wide
// cells we encounter.
b.SetCell(i, y, b.CellAt(i+n, y))
}
}
// Fill the vacated positions with the given cell
for i := area.Max.X - n; i < area.Max.X; i++ {
b.SetCell(i, y, c)
}
}
// ScreenBuffer is a buffer that can be used as a [Screen].
type ScreenBuffer struct {
*RenderBuffer
Method ansi.Method
}
var (
_ Screen = ScreenBuffer{}
_ Drawable = ScreenBuffer{}
)
// NewScreenBuffer creates a new ScreenBuffer with the given width and height.
func NewScreenBuffer(width, height int) ScreenBuffer {
return ScreenBuffer{
RenderBuffer: NewRenderBuffer(width, height),
Method: ansi.WcWidth,
}
}
// WidthMethod returns the width method used by the screen.
// It defaults to [ansi.WcWidth].
func (s ScreenBuffer) WidthMethod() WidthMethod {
return s.Method
}
// TrimSpace trims trailing spaces from the end of each line in the given
// string.
func TrimSpace(s string) string {
lines := strings.Split(s, "\n")
for i, line := range lines {
// Check if we have a trailing '\r' and preserve it
hasCR := strings.HasSuffix(line, "\r")
if hasCR {
line = strings.TrimSuffix(line, "\r")
}
line = strings.TrimRight(line, " ")
if hasCR {
line = line + "\r"
}
lines[i] = line
}
return strings.Join(lines, "\n")
}
// RenderBuffer represents a buffer that keeps track of the current and new
// state of the screen, allowing for efficient rendering by only updating the
// parts of the screen that have changed.
type RenderBuffer struct {
*Buffer
Touched []*LineData
}
// NewRenderBuffer creates a new [RenderBuffer] with the given width and height.
func NewRenderBuffer(width, height int) *RenderBuffer {
return &RenderBuffer{
Buffer: NewBuffer(width, height),
Touched: make([]*LineData, height),
}
}
// TouchLine marks a line n times starting at the given x position as touched.
func (b *RenderBuffer) TouchLine(x, y, n int) {
if y < 0 || y >= len(b.Lines) {
return
}
if y >= len(b.Touched) {
b.Touched = append(b.Touched, make([]*LineData, y-len(b.Touched)+1)...)
}
// Re-check bounds: a concurrent resize may have cleared Touched
if y >= len(b.Touched) {
return
}
ch := b.Touched[y]
if ch == nil {
ch = &LineData{FirstCell: x, LastCell: x + n}
} else {
ch.FirstCell = min(ch.FirstCell, x)
ch.LastCell = max(ch.LastCell, x+n)
}
b.Touched[y] = ch
}
// Touch marks the cell at the given x, y position as touched.
func (b *RenderBuffer) Touch(x, y int) {
b.TouchLine(x, y, 0)
}
// TouchedLines returns the number of touched lines in the buffer.
func (b *RenderBuffer) TouchedLines() int {
if b.Touched == nil {
return 0
}
count := 0
for _, t := range b.Touched {
if t != nil {
count++
}
}
return count
}
// SetCell sets the cell at the given x, y position and marks the line as
// touched.
func (b *RenderBuffer) SetCell(x, y int, c *Cell) {
if p := b.CellAt(x, y); !cellEqual(p, c) {
width := 1
if c != nil && c.Width > 0 {
width = c.Width
}
if p != nil && p.Width > 0 {
width = max(width, p.Width)
}
b.TouchLine(x, y, width)
}
b.Buffer.SetCell(x, y, c)
}
// InsertLine inserts n lines at the given line position, with the given
// optional cell, within the specified rectangles. If no rectangles are
// specified, it inserts lines in the entire buffer. Only cells within the
// rectangle's horizontal bounds are affected. Lines are pushed out of the
// rectangle bounds and lost. This follows terminal [ansi.IL] behavior.
func (b *RenderBuffer) InsertLine(y, n int, c *Cell) {
b.InsertLineArea(y, n, c, b.Bounds())
}
// InsertLineArea inserts new lines at the given line position, with the given
// optional cell, within the rectangle bounds. Only cells within the
// rectangle's horizontal bounds are affected. Lines are pushed out of the
// rectangle bounds and lost. This follows terminal [ansi.IL] behavior.
func (b *RenderBuffer) InsertLineArea(y, n int, c *Cell, area Rectangle) {
b.Buffer.InsertLineArea(y, n, c, area)
for i := area.Min.Y; i < area.Max.Y; i++ {
b.TouchLine(area.Min.X, i, area.Max.X-area.Min.X)
b.TouchLine(area.Min.X, i-n, area.Max.X-area.Min.X)
}
}
// DeleteLine deletes n lines at the given line position, with the given
// optional cell, within the specified rectangles. If no rectangles are
// specified, it deletes lines in the entire buffer.
func (b *RenderBuffer) DeleteLine(y, n int, c *Cell) {
b.DeleteLineArea(y, n, c, b.Bounds())
}
// DeleteLineArea deletes lines at the given line position, with the given
// optional cell, within the rectangle bounds. Only cells within the
// rectangle's bounds are affected. Lines are shifted up within the bounds and
// new blank lines are created at the bottom. This follows terminal [ansi.DL]
// behavior.
func (b *RenderBuffer) DeleteLineArea(y, n int, c *Cell, area Rectangle) {
b.Buffer.DeleteLineArea(y, n, c, area)
for i := area.Min.Y; i < area.Max.Y; i++ {
b.TouchLine(area.Min.X, i, area.Max.X-area.Min.X)
b.TouchLine(area.Min.X, i+n, area.Max.X-area.Min.X)
}
}
// InsertCell inserts new cells at the given position, with the given optional
// cell, within the specified rectangles. If no rectangles are specified, it
// inserts cells in the entire buffer. This follows terminal [ansi.ICH]
// behavior.
func (b *RenderBuffer) InsertCell(x, y, n int, c *Cell) {
b.InsertCellArea(x, y, n, c, b.Bounds())
}
// InsertCellArea inserts new cells at the given position, with the given
// optional cell, within the rectangle bounds. Only cells within the
// rectangle's bounds are affected, following terminal [ansi.ICH] behavior.
func (b *RenderBuffer) InsertCellArea(x, y, n int, c *Cell, area Rectangle) {
b.Buffer.InsertCellArea(x, y, n, c, area)
if x+n > area.Max.X {
n = area.Max.X - x
}
b.TouchLine(x, y, n)
}
// DeleteCell deletes cells at the given position, with the given optional
// cell, within the specified rectangles. If no rectangles are specified, it
// deletes cells in the entire buffer. This follows terminal [ansi.DCH]
// behavior.
func (b *RenderBuffer) DeleteCell(x, y, n int, c *Cell) {
b.DeleteCellArea(x, y, n, c, b.Bounds())
}
// DeleteCellArea deletes cells at the given position, with the given
// optional cell, within the rectangle bounds. Only cells within the
// rectangle's bounds are affected, following terminal [ansi.DCH] behavior.
func (b *RenderBuffer) DeleteCellArea(x, y, n int, c *Cell, area Rectangle) {
b.Buffer.DeleteCellArea(x, y, n, c, area)
remainingCells := area.Max.X - x
if n > remainingCells {
n = remainingCells
}
b.TouchLine(x, y, n)
}
// Clear clears the buffer with space cells and marks all lines as touched.
func (b *RenderBuffer) Clear() {
b.Buffer.Clear()
w := b.Width()
for y := range b.Lines {
b.TouchLine(0, y, w)
}
}
// ClearArea clears the buffer with space cells within the specified rectangle
// and marks the affected lines as touched.
func (b *RenderBuffer) ClearArea(area Rectangle) {
b.Buffer.ClearArea(area)
w := area.Max.X - area.Min.X
for y := area.Min.Y; y < area.Max.Y; y++ {
b.TouchLine(area.Min.X, y, w)
}
}
// Fill fills the buffer with the given cell and marks all lines as touched.
func (b *RenderBuffer) Fill(c *Cell) {
b.FillArea(c, b.Bounds())
}
// FillArea fills the buffer with the given cell within the specified rectangle
// and marks the affected lines as touched.
func (b *RenderBuffer) FillArea(c *Cell, area Rectangle) {
b.Buffer.FillArea(c, area)
w := area.Max.X - area.Min.X
for y := area.Min.Y; y < area.Max.Y; y++ {
b.TouchLine(area.Min.X, y, w)
}
}