| 1 |
package vt |
| 2 |
|
| 3 |
import uv "github.com/charmbracelet/ultraviolet" |
| 4 |
|
| 5 |
// Buffer is a terminal cell buffer. |
| 6 |
type Buffer struct { |
| 7 |
uv.Buffer |
| 8 |
} |
| 9 |
|
| 10 |
// InsertLine inserts n lines at the given line position, with the given |
| 11 |
// optional cell, within the specified rectangles. If no rectangles are |
| 12 |
// specified, it inserts lines in the entire buffer. Only cells within the |
| 13 |
// rectangle's horizontal bounds are affected. Lines are pushed out of the |
| 14 |
// rectangle bounds and lost. This follows terminal [ansi.IL] behavior. |
| 15 |
// It returns the pushed out lines. |
| 16 |
func (b *Buffer) InsertLine(y, n int, c *uv.Cell) { |
| 17 |
b.InsertLineRect(y, n, c, b.Bounds()) |
| 18 |
} |
| 19 |
|
| 20 |
// InsertLineRect inserts new lines at the given line position, with the |
| 21 |
// given optional cell, within the rectangle bounds. Only cells within the |
| 22 |
// rectangle's horizontal bounds are affected. Lines are pushed out of the |
| 23 |
// rectangle bounds and lost. This follows terminal [ansi.IL] behavior. |
| 24 |
func (b *Buffer) InsertLineRect(y, n int, c *uv.Cell, rect uv.Rectangle) { |
| 25 |
if n <= 0 || y < rect.Min.Y || y >= rect.Max.Y || y >= b.Height() { |
| 26 |
return |
| 27 |
} |
| 28 |
|
| 29 |
// Limit number of lines to insert to available space |
| 30 |
if y+n > rect.Max.Y { |
| 31 |
n = rect.Max.Y - y |
| 32 |
} |
| 33 |
|
| 34 |
// Move existing lines down within the bounds |
| 35 |
for i := rect.Max.Y - 1; i >= y+n; i-- { |
| 36 |
for x := rect.Min.X; x < rect.Max.X; x++ { |
| 37 |
// We don't need to clone c here because we're just moving lines down. |
| 38 |
b.Lines[i][x] = b.Lines[i-n][x] |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// Clear the newly inserted lines within bounds |
| 43 |
for i := y; i < y+n; i++ { |
| 44 |
for x := rect.Min.X; x < rect.Max.X; x++ { |
| 45 |
b.SetCell(x, i, c) |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// DeleteLineRect deletes lines at the given line position, with the given |
| 51 |
// optional cell, within the rectangle bounds. Only cells within the |
| 52 |
// rectangle's bounds are affected. Lines are shifted up within the bounds and |
| 53 |
// new blank lines are created at the bottom. This follows terminal [ansi.DL] |
| 54 |
// behavior. |
| 55 |
func (b *Buffer) DeleteLineRect(y, n int, c *uv.Cell, rect uv.Rectangle) { |
| 56 |
if n <= 0 || y < rect.Min.Y || y >= rect.Max.Y || y >= b.Height() { |
| 57 |
return |
| 58 |
} |
| 59 |
|
| 60 |
// Limit deletion count to available space in scroll region |
| 61 |
if n > rect.Max.Y-y { |
| 62 |
n = rect.Max.Y - y |
| 63 |
} |
| 64 |
|
| 65 |
// Shift cells up within the bounds |
| 66 |
for dst := y; dst < rect.Max.Y-n; dst++ { |
| 67 |
src := dst + n |
| 68 |
for x := rect.Min.X; x < rect.Max.X; x++ { |
| 69 |
// We don't need to clone c here because we're just moving cells up. |
| 70 |
b.Lines[dst][x] = b.Lines[src][x] |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// Fill the bottom n lines with blank cells |
| 75 |
for i := rect.Max.Y - n; i < rect.Max.Y; i++ { |
| 76 |
for x := rect.Min.X; x < rect.Max.X; x++ { |
| 77 |
b.SetCell(x, i, c) |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// DeleteLine deletes n lines at the given line position, with the given |
| 83 |
// optional cell, within the specified rectangles. If no rectangles are |
| 84 |
// specified, it deletes lines in the entire buffer. |
| 85 |
func (b *Buffer) DeleteLine(y, n int, c *uv.Cell) { |
| 86 |
b.DeleteLineRect(y, n, c, b.Bounds()) |
| 87 |
} |
| 88 |
|
| 89 |
// InsertCell inserts new cells at the given position, with the given optional |
| 90 |
// cell, within the specified rectangles. If no rectangles are specified, it |
| 91 |
// inserts cells in the entire buffer. This follows terminal [ansi.ICH] |
| 92 |
// behavior. |
| 93 |
func (b *Buffer) InsertCell(x, y, n int, c *uv.Cell) { |
| 94 |
b.InsertCellRect(x, y, n, c, b.Bounds()) |
| 95 |
} |
| 96 |
|
| 97 |
// InsertCellRect inserts new cells at the given position, with the given |
| 98 |
// optional cell, within the rectangle bounds. Only cells within the |
| 99 |
// rectangle's bounds are affected, following terminal [ansi.ICH] behavior. |
| 100 |
func (b *Buffer) InsertCellRect(x, y, n int, c *uv.Cell, rect uv.Rectangle) { |
| 101 |
if n <= 0 || y < rect.Min.Y || y >= rect.Max.Y || y >= b.Height() || |
| 102 |
x < rect.Min.X || x >= rect.Max.X || x >= b.Width() { |
| 103 |
return |
| 104 |
} |
| 105 |
|
| 106 |
// Limit number of cells to insert to available space |
| 107 |
if x+n > rect.Max.X { |
| 108 |
n = rect.Max.X - x |
| 109 |
} |
| 110 |
|
| 111 |
// Move existing cells within rectangle bounds to the right |
| 112 |
for i := rect.Max.X - 1; i >= x+n && i-n >= rect.Min.X; i-- { |
| 113 |
// We don't need to clone c here because we're just moving cells to the |
| 114 |
// right. |
| 115 |
// b.lines[y][i] = b.lines[y][i-n] |
| 116 |
b.Lines[y][i] = b.Lines[y][i-n] |
| 117 |
} |
| 118 |
|
| 119 |
// Clear the newly inserted cells within rectangle bounds |
| 120 |
for i := x; i < x+n && i < rect.Max.X; i++ { |
| 121 |
b.SetCell(i, y, c) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// DeleteCell deletes cells at the given position, with the given optional |
| 126 |
// cell, within the specified rectangles. If no rectangles are specified, it |
| 127 |
// deletes cells in the entire buffer. This follows terminal [ansi.DCH] |
| 128 |
// behavior. |
| 129 |
func (b *Buffer) DeleteCell(x, y, n int, c *uv.Cell) { |
| 130 |
b.DeleteCellRect(x, y, n, c, b.Bounds()) |
| 131 |
} |
| 132 |
|
| 133 |
// DeleteCellRect deletes cells at the given position, with the given |
| 134 |
// optional cell, within the rectangle bounds. Only cells within the |
| 135 |
// rectangle's bounds are affected, following terminal [ansi.DCH] behavior. |
| 136 |
func (b *Buffer) DeleteCellRect(x, y, n int, c *uv.Cell, rect uv.Rectangle) { |
| 137 |
if n <= 0 || y < rect.Min.Y || y >= rect.Max.Y || y >= b.Height() || |
| 138 |
x < rect.Min.X || x >= rect.Max.X || x >= b.Width() { |
| 139 |
return |
| 140 |
} |
| 141 |
|
| 142 |
// Calculate how many positions we can actually delete |
| 143 |
remainingCells := rect.Max.X - x |
| 144 |
if n > remainingCells { |
| 145 |
n = remainingCells |
| 146 |
} |
| 147 |
|
| 148 |
// Shift the remaining cells to the left |
| 149 |
for i := x; i < rect.Max.X-n; i++ { |
| 150 |
if i+n < rect.Max.X { |
| 151 |
// We don't need to clone c here because we're just moving cells to |
| 152 |
// the left. |
| 153 |
// b.lines[y][i] = b.lines[y][i+n] |
| 154 |
b.Lines[y][i] = b.Lines[y][i+n] |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
// Fill the vacated positions with the given cell |
| 159 |
for i := rect.Max.X - n; i < rect.Max.X; i++ { |
| 160 |
b.SetCell(i, y, c) |
| 161 |
} |
| 162 |
} |