| 1 |
// Package cellbuf provides terminal cell buffer functionality. |
| 2 |
package cellbuf |
| 3 |
|
| 4 |
import ( |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/mattn/go-runewidth" |
| 8 |
"github.com/rivo/uniseg" |
| 9 |
) |
| 10 |
|
| 11 |
// NewCell returns a new cell. This is a convenience function that initializes a |
| 12 |
// new cell with the given content. The cell's width is determined by the |
| 13 |
// content using [runewidth.RuneWidth]. |
| 14 |
// This will only account for the first combined rune in the content. If the |
| 15 |
// content is empty, it will return an empty cell with a width of 0. |
| 16 |
func NewCell(r rune, comb ...rune) (c *Cell) { |
| 17 |
c = new(Cell) |
| 18 |
c.Rune = r |
| 19 |
c.Width = runewidth.RuneWidth(r) |
| 20 |
for _, r := range comb { |
| 21 |
if runewidth.RuneWidth(r) > 0 { |
| 22 |
break |
| 23 |
} |
| 24 |
c.Comb = append(c.Comb, r) |
| 25 |
} |
| 26 |
c.Comb = comb |
| 27 |
c.Width = runewidth.StringWidth(string(append([]rune{r}, comb...))) |
| 28 |
return c |
| 29 |
} |
| 30 |
|
| 31 |
// NewCellString returns a new cell with the given string content. This is a |
| 32 |
// convenience function that initializes a new cell with the given content. The |
| 33 |
// cell's width is determined by the content using [runewidth.StringWidth]. |
| 34 |
// This will only use the first combined rune in the string. If the string is |
| 35 |
// empty, it will return an empty cell with a width of 0. |
| 36 |
func NewCellString(s string) (c *Cell) { |
| 37 |
c = new(Cell) |
| 38 |
for i, r := range s { |
| 39 |
if i == 0 { |
| 40 |
c.Rune = r |
| 41 |
// We only care about the first rune's width |
| 42 |
c.Width = runewidth.RuneWidth(r) |
| 43 |
} else { |
| 44 |
if runewidth.RuneWidth(r) > 0 { |
| 45 |
break |
| 46 |
} |
| 47 |
c.Comb = append(c.Comb, r) |
| 48 |
} |
| 49 |
} |
| 50 |
return c |
| 51 |
} |
| 52 |
|
| 53 |
// NewGraphemeCell returns a new cell. This is a convenience function that |
| 54 |
// initializes a new cell with the given content. The cell's width is determined |
| 55 |
// by the content using [uniseg.FirstGraphemeClusterInString]. |
| 56 |
// This is used when the content is a grapheme cluster i.e. a sequence of runes |
| 57 |
// that form a single visual unit. |
| 58 |
// This will only return the first grapheme cluster in the string. If the |
| 59 |
// string is empty, it will return an empty cell with a width of 0. |
| 60 |
func NewGraphemeCell(s string) (c *Cell) { |
| 61 |
g, _, w, _ := uniseg.FirstGraphemeClusterInString(s, -1) |
| 62 |
return newGraphemeCell(g, w) |
| 63 |
} |
| 64 |
|
| 65 |
func newGraphemeCell(s string, w int) (c *Cell) { |
| 66 |
c = new(Cell) |
| 67 |
c.Width = w |
| 68 |
for i, r := range s { |
| 69 |
if i == 0 { |
| 70 |
c.Rune = r |
| 71 |
} else { |
| 72 |
c.Comb = append(c.Comb, r) |
| 73 |
} |
| 74 |
} |
| 75 |
return c |
| 76 |
} |
| 77 |
|
| 78 |
// Line represents a line in the terminal. |
| 79 |
// A nil cell represents an blank cell, a cell with a space character and a |
| 80 |
// width of 1. |
| 81 |
// If a cell has no content and a width of 0, it is a placeholder for a wide |
| 82 |
// cell. |
| 83 |
type Line []*Cell |
| 84 |
|
| 85 |
// Width returns the width of the line. |
| 86 |
func (l Line) Width() int { |
| 87 |
return len(l) |
| 88 |
} |
| 89 |
|
| 90 |
// Len returns the length of the line. |
| 91 |
func (l Line) Len() int { |
| 92 |
return len(l) |
| 93 |
} |
| 94 |
|
| 95 |
// String returns the string representation of the line. Any trailing spaces |
| 96 |
// are removed. |
| 97 |
func (l Line) String() (s string) { |
| 98 |
for _, c := range l { |
| 99 |
if c == nil { |
| 100 |
s += " " |
| 101 |
} else if c.Empty() { |
| 102 |
continue |
| 103 |
} else { |
| 104 |
s += c.String() |
| 105 |
} |
| 106 |
} |
| 107 |
s = strings.TrimRight(s, " ") |
| 108 |
return s |
| 109 |
} |
| 110 |
|
| 111 |
// At returns the cell at the given x position. |
| 112 |
// If the cell does not exist, it returns nil. |
| 113 |
func (l Line) At(x int) *Cell { |
| 114 |
if x < 0 || x >= len(l) { |
| 115 |
return nil |
| 116 |
} |
| 117 |
|
| 118 |
c := l[x] |
| 119 |
if c == nil { |
| 120 |
newCell := BlankCell |
| 121 |
return &newCell |
| 122 |
} |
| 123 |
|
| 124 |
return c |
| 125 |
} |
| 126 |
|
| 127 |
// Set sets the cell at the given x position. If a wide cell is given, it will |
| 128 |
// set the cell and the following cells to [EmptyCell]. It returns true if the |
| 129 |
// cell was set. |
| 130 |
func (l Line) Set(x int, c *Cell) bool { |
| 131 |
return l.set(x, c, true) |
| 132 |
} |
| 133 |
|
| 134 |
func (l Line) set(x int, c *Cell, clone bool) bool { |
| 135 |
width := l.Width() |
| 136 |
if x < 0 || x >= width { |
| 137 |
return false |
| 138 |
} |
| 139 |
|
| 140 |
// When a wide cell is partially overwritten, we need |
| 141 |
// to fill the rest of the cell with space cells to |
| 142 |
// avoid rendering issues. |
| 143 |
prev := l.At(x) |
| 144 |
if prev != nil && prev.Width > 1 { |
| 145 |
// Writing to the first wide cell |
| 146 |
for j := 0; j < prev.Width && x+j < l.Width(); j++ { |
| 147 |
l[x+j] = prev.Clone().Blank() |
| 148 |
} |
| 149 |
} else if prev != nil && prev.Width == 0 { |
| 150 |
// Writing to wide cell placeholders |
| 151 |
for j := 1; j < maxCellWidth && x-j >= 0; j++ { |
| 152 |
wide := l.At(x - j) |
| 153 |
if wide != nil && wide.Width > 1 && j < wide.Width { |
| 154 |
for k := range wide.Width { |
| 155 |
l[x-j+k] = wide.Clone().Blank() |
| 156 |
} |
| 157 |
break |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if clone && c != nil { |
| 163 |
// Clone the cell if not nil. |
| 164 |
c = c.Clone() |
| 165 |
} |
| 166 |
|
| 167 |
if c != nil && x+c.Width > width { |
| 168 |
// If the cell is too wide, we write blanks with the same style. |
| 169 |
for i := 0; i < c.Width && x+i < width; i++ { |
| 170 |
l[x+i] = c.Clone().Blank() |
| 171 |
} |
| 172 |
} else { |
| 173 |
l[x] = c |
| 174 |
|
| 175 |
// Mark wide cells with an empty cell zero width |
| 176 |
// We set the wide cell down below |
| 177 |
if c != nil && c.Width > 1 { |
| 178 |
for j := 1; j < c.Width && x+j < l.Width(); j++ { |
| 179 |
var wide Cell |
| 180 |
l[x+j] = &wide |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return true |
| 186 |
} |
| 187 |
|
| 188 |
// Buffer is a 2D grid of cells representing a screen or terminal. |
| 189 |
type Buffer struct { |
| 190 |
// Lines holds the lines of the buffer. |
| 191 |
Lines []Line |
| 192 |
} |
| 193 |
|
| 194 |
// NewBuffer creates a new buffer with the given width and height. |
| 195 |
// This is a convenience function that initializes a new buffer and resizes it. |
| 196 |
func NewBuffer(width int, height int) *Buffer { |
| 197 |
b := new(Buffer) |
| 198 |
b.Resize(width, height) |
| 199 |
return b |
| 200 |
} |
| 201 |
|
| 202 |
// String returns the string representation of the buffer. |
| 203 |
func (b *Buffer) String() (s string) { |
| 204 |
for i, l := range b.Lines { |
| 205 |
s += l.String() |
| 206 |
if i < len(b.Lines)-1 { |
| 207 |
s += "\r\n" |
| 208 |
} |
| 209 |
} |
| 210 |
return s |
| 211 |
} |
| 212 |
|
| 213 |
// Line returns a pointer to the line at the given y position. |
| 214 |
// If the line does not exist, it returns nil. |
| 215 |
func (b *Buffer) Line(y int) Line { |
| 216 |
if y < 0 || y >= len(b.Lines) { |
| 217 |
return nil |
| 218 |
} |
| 219 |
return b.Lines[y] |
| 220 |
} |
| 221 |
|
| 222 |
// Cell implements Screen. |
| 223 |
func (b *Buffer) Cell(x int, y int) *Cell { |
| 224 |
if y < 0 || y >= len(b.Lines) { |
| 225 |
return nil |
| 226 |
} |
| 227 |
return b.Lines[y].At(x) |
| 228 |
} |
| 229 |
|
| 230 |
// maxCellWidth is the maximum width a terminal cell can get. |
| 231 |
const maxCellWidth = 4 |
| 232 |
|
| 233 |
// SetCell sets the cell at the given x, y position. |
| 234 |
func (b *Buffer) SetCell(x, y int, c *Cell) bool { |
| 235 |
return b.setCell(x, y, c, true) |
| 236 |
} |
| 237 |
|
| 238 |
// setCell sets the cell at the given x, y position. This will always clone and |
| 239 |
// allocates a new cell if c is not nil. |
| 240 |
func (b *Buffer) setCell(x, y int, c *Cell, clone bool) bool { |
| 241 |
if y < 0 || y >= len(b.Lines) { |
| 242 |
return false |
| 243 |
} |
| 244 |
return b.Lines[y].set(x, c, clone) |
| 245 |
} |
| 246 |
|
| 247 |
// Height implements Screen. |
| 248 |
func (b *Buffer) Height() int { |
| 249 |
return len(b.Lines) |
| 250 |
} |
| 251 |
|
| 252 |
// Width implements Screen. |
| 253 |
func (b *Buffer) Width() int { |
| 254 |
if len(b.Lines) == 0 { |
| 255 |
return 0 |
| 256 |
} |
| 257 |
return b.Lines[0].Width() |
| 258 |
} |
| 259 |
|
| 260 |
// Bounds returns the bounds of the buffer. |
| 261 |
func (b *Buffer) Bounds() Rectangle { |
| 262 |
return Rect(0, 0, b.Width(), b.Height()) |
| 263 |
} |
| 264 |
|
| 265 |
// Resize resizes the buffer to the given width and height. |
| 266 |
func (b *Buffer) Resize(width int, height int) { |
| 267 |
if width == 0 || height == 0 { |
| 268 |
b.Lines = nil |
| 269 |
return |
| 270 |
} |
| 271 |
|
| 272 |
if width > b.Width() { |
| 273 |
line := make(Line, width-b.Width()) |
| 274 |
for i := range b.Lines { |
| 275 |
b.Lines[i] = append(b.Lines[i], line...) |
| 276 |
} |
| 277 |
} else if width < b.Width() { |
| 278 |
for i := range b.Lines { |
| 279 |
b.Lines[i] = b.Lines[i][:width] |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
if height > len(b.Lines) { |
| 284 |
for i := len(b.Lines); i < height; i++ { |
| 285 |
b.Lines = append(b.Lines, make(Line, width)) |
| 286 |
} |
| 287 |
} else if height < len(b.Lines) { |
| 288 |
b.Lines = b.Lines[:height] |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
// FillRect fills the buffer with the given cell and rectangle. |
| 293 |
func (b *Buffer) FillRect(c *Cell, rect Rectangle) { |
| 294 |
cellWidth := 1 |
| 295 |
if c != nil && c.Width > 1 { |
| 296 |
cellWidth = c.Width |
| 297 |
} |
| 298 |
for y := rect.Min.Y; y < rect.Max.Y; y++ { |
| 299 |
for x := rect.Min.X; x < rect.Max.X; x += cellWidth { |
| 300 |
b.setCell(x, y, c, false) |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
// Fill fills the buffer with the given cell and rectangle. |
| 306 |
func (b *Buffer) Fill(c *Cell) { |
| 307 |
b.FillRect(c, b.Bounds()) |
| 308 |
} |
| 309 |
|
| 310 |
// Clear clears the buffer with space cells and rectangle. |
| 311 |
func (b *Buffer) Clear() { |
| 312 |
b.ClearRect(b.Bounds()) |
| 313 |
} |
| 314 |
|
| 315 |
// ClearRect clears the buffer with space cells within the specified |
| 316 |
// rectangles. Only cells within the rectangle's bounds are affected. |
| 317 |
func (b *Buffer) ClearRect(rect Rectangle) { |
| 318 |
b.FillRect(nil, rect) |
| 319 |
} |
| 320 |
|
| 321 |
// InsertLine inserts n lines at the given line position, with the given |
| 322 |
// optional cell, within the specified rectangles. If no rectangles are |
| 323 |
// specified, it inserts lines in the entire buffer. Only cells within the |
| 324 |
// rectangle's horizontal bounds are affected. Lines are pushed out of the |
| 325 |
// rectangle bounds and lost. This follows terminal [ansi.IL] behavior. |
| 326 |
// It returns the pushed out lines. |
| 327 |
func (b *Buffer) InsertLine(y, n int, c *Cell) { |
| 328 |
b.InsertLineRect(y, n, c, b.Bounds()) |
| 329 |
} |
| 330 |
|
| 331 |
// InsertLineRect inserts new lines at the given line position, with the |
| 332 |
// given optional cell, within the rectangle bounds. Only cells within the |
| 333 |
// rectangle's horizontal bounds are affected. Lines are pushed out of the |
| 334 |
// rectangle bounds and lost. This follows terminal [ansi.IL] behavior. |
| 335 |
func (b *Buffer) InsertLineRect(y, n int, c *Cell, rect Rectangle) { |
| 336 |
if n <= 0 || y < rect.Min.Y || y >= rect.Max.Y || y >= b.Height() { |
| 337 |
return |
| 338 |
} |
| 339 |
|
| 340 |
// Limit number of lines to insert to available space |
| 341 |
if y+n > rect.Max.Y { |
| 342 |
n = rect.Max.Y - y |
| 343 |
} |
| 344 |
|
| 345 |
// Move existing lines down within the bounds |
| 346 |
for i := rect.Max.Y - 1; i >= y+n; i-- { |
| 347 |
for x := rect.Min.X; x < rect.Max.X; x++ { |
| 348 |
// We don't need to clone c here because we're just moving lines down. |
| 349 |
b.setCell(x, i, b.Lines[i-n][x], false) |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
// Clear the newly inserted lines within bounds |
| 354 |
for i := y; i < y+n; i++ { |
| 355 |
for x := rect.Min.X; x < rect.Max.X; x++ { |
| 356 |
b.setCell(x, i, c, true) |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
// DeleteLineRect deletes lines at the given line position, with the given |
| 362 |
// optional cell, within the rectangle bounds. Only cells within the |
| 363 |
// rectangle's bounds are affected. Lines are shifted up within the bounds and |
| 364 |
// new blank lines are created at the bottom. This follows terminal [ansi.DL] |
| 365 |
// behavior. |
| 366 |
func (b *Buffer) DeleteLineRect(y, n int, c *Cell, rect Rectangle) { |
| 367 |
if n <= 0 || y < rect.Min.Y || y >= rect.Max.Y || y >= b.Height() { |
| 368 |
return |
| 369 |
} |
| 370 |
|
| 371 |
// Limit deletion count to available space in scroll region |
| 372 |
if n > rect.Max.Y-y { |
| 373 |
n = rect.Max.Y - y |
| 374 |
} |
| 375 |
|
| 376 |
// Shift cells up within the bounds |
| 377 |
for dst := y; dst < rect.Max.Y-n; dst++ { |
| 378 |
src := dst + n |
| 379 |
for x := rect.Min.X; x < rect.Max.X; x++ { |
| 380 |
// We don't need to clone c here because we're just moving cells up. |
| 381 |
// b.lines[dst][x] = b.lines[src][x] |
| 382 |
b.setCell(x, dst, b.Lines[src][x], false) |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
// Fill the bottom n lines with blank cells |
| 387 |
for i := rect.Max.Y - n; i < rect.Max.Y; i++ { |
| 388 |
for x := rect.Min.X; x < rect.Max.X; x++ { |
| 389 |
b.setCell(x, i, c, true) |
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
// DeleteLine deletes n lines at the given line position, with the given |
| 395 |
// optional cell, within the specified rectangles. If no rectangles are |
| 396 |
// specified, it deletes lines in the entire buffer. |
| 397 |
func (b *Buffer) DeleteLine(y, n int, c *Cell) { |
| 398 |
b.DeleteLineRect(y, n, c, b.Bounds()) |
| 399 |
} |
| 400 |
|
| 401 |
// InsertCell inserts new cells at the given position, with the given optional |
| 402 |
// cell, within the specified rectangles. If no rectangles are specified, it |
| 403 |
// inserts cells in the entire buffer. This follows terminal [ansi.ICH] |
| 404 |
// behavior. |
| 405 |
func (b *Buffer) InsertCell(x, y, n int, c *Cell) { |
| 406 |
b.InsertCellRect(x, y, n, c, b.Bounds()) |
| 407 |
} |
| 408 |
|
| 409 |
// InsertCellRect inserts new cells at the given position, with the given |
| 410 |
// optional cell, within the rectangle bounds. Only cells within the |
| 411 |
// rectangle's bounds are affected, following terminal [ansi.ICH] behavior. |
| 412 |
func (b *Buffer) InsertCellRect(x, y, n int, c *Cell, rect Rectangle) { |
| 413 |
if n <= 0 || y < rect.Min.Y || y >= rect.Max.Y || y >= b.Height() || |
| 414 |
x < rect.Min.X || x >= rect.Max.X || x >= b.Width() { |
| 415 |
return |
| 416 |
} |
| 417 |
|
| 418 |
// Limit number of cells to insert to available space |
| 419 |
if x+n > rect.Max.X { |
| 420 |
n = rect.Max.X - x |
| 421 |
} |
| 422 |
|
| 423 |
// Move existing cells within rectangle bounds to the right |
| 424 |
for i := rect.Max.X - 1; i >= x+n && i-n >= rect.Min.X; i-- { |
| 425 |
// We don't need to clone c here because we're just moving cells to the |
| 426 |
// right. |
| 427 |
// b.lines[y][i] = b.lines[y][i-n] |
| 428 |
b.setCell(i, y, b.Lines[y][i-n], false) |
| 429 |
} |
| 430 |
|
| 431 |
// Clear the newly inserted cells within rectangle bounds |
| 432 |
for i := x; i < x+n && i < rect.Max.X; i++ { |
| 433 |
b.setCell(i, y, c, true) |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
// DeleteCell deletes cells at the given position, with the given optional |
| 438 |
// cell, within the specified rectangles. If no rectangles are specified, it |
| 439 |
// deletes cells in the entire buffer. This follows terminal [ansi.DCH] |
| 440 |
// behavior. |
| 441 |
func (b *Buffer) DeleteCell(x, y, n int, c *Cell) { |
| 442 |
b.DeleteCellRect(x, y, n, c, b.Bounds()) |
| 443 |
} |
| 444 |
|
| 445 |
// DeleteCellRect deletes cells at the given position, with the given |
| 446 |
// optional cell, within the rectangle bounds. Only cells within the |
| 447 |
// rectangle's bounds are affected, following terminal [ansi.DCH] behavior. |
| 448 |
func (b *Buffer) DeleteCellRect(x, y, n int, c *Cell, rect Rectangle) { |
| 449 |
if n <= 0 || y < rect.Min.Y || y >= rect.Max.Y || y >= b.Height() || |
| 450 |
x < rect.Min.X || x >= rect.Max.X || x >= b.Width() { |
| 451 |
return |
| 452 |
} |
| 453 |
|
| 454 |
// Calculate how many positions we can actually delete |
| 455 |
remainingCells := rect.Max.X - x |
| 456 |
if n > remainingCells { |
| 457 |
n = remainingCells |
| 458 |
} |
| 459 |
|
| 460 |
// Shift the remaining cells to the left |
| 461 |
for i := x; i < rect.Max.X-n; i++ { |
| 462 |
if i+n < rect.Max.X { |
| 463 |
// We don't need to clone c here because we're just moving cells to |
| 464 |
// the left. |
| 465 |
// b.lines[y][i] = b.lines[y][i+n] |
| 466 |
b.setCell(i, y, b.Lines[y][i+n], false) |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
// Fill the vacated positions with the given cell |
| 471 |
for i := rect.Max.X - n; i < rect.Max.X; i++ { |
| 472 |
b.setCell(i, y, c, true) |
| 473 |
} |
| 474 |
} |