| 1 |
|
| 2 |
package cellbuf |
| 3 |
|
| 4 |
import ( |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/mattn/go-runewidth" |
| 8 |
"github.com/rivo/uniseg" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
|
| 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 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
type Line []*Cell |
| 84 |
|
| 85 |
|
| 86 |
func (l Line) Width() int { |
| 87 |
return len(l) |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func (l Line) Len() int { |
| 92 |
return len(l) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 112 |
|
| 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 |
|
| 128 |
|
| 129 |
|
| 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 |
|
| 141 |
|
| 142 |
|
| 143 |
prev := l.At(x) |
| 144 |
if prev != nil && prev.Width > 1 { |
| 145 |
|
| 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 |
|
| 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 |
|
| 164 |
c = c.Clone() |
| 165 |
} |
| 166 |
|
| 167 |
if c != nil && x+c.Width > width { |
| 168 |
|
| 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 |
|
| 176 |
|
| 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 |
|
| 189 |
type Buffer struct { |
| 190 |
|
| 191 |
Lines []Line |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
func NewBuffer(width int, height int) *Buffer { |
| 197 |
b := new(Buffer) |
| 198 |
b.Resize(width, height) |
| 199 |
return b |
| 200 |
} |
| 201 |
|
| 202 |
|
| 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 |
|
| 214 |
|
| 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 |
|
| 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 |
|
| 231 |
const maxCellWidth = 4 |
| 232 |
|
| 233 |
|
| 234 |
func (b *Buffer) SetCell(x, y int, c *Cell) bool { |
| 235 |
return b.setCell(x, y, c, true) |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 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 |
|
| 248 |
func (b *Buffer) Height() int { |
| 249 |
return len(b.Lines) |
| 250 |
} |
| 251 |
|
| 252 |
|
| 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 |
|
| 261 |
func (b *Buffer) Bounds() Rectangle { |
| 262 |
return Rect(0, 0, b.Width(), b.Height()) |
| 263 |
} |
| 264 |
|
| 265 |
|
| 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 |
|
| 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 |
|
| 306 |
func (b *Buffer) Fill(c *Cell) { |
| 307 |
b.FillRect(c, b.Bounds()) |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
func (b *Buffer) Clear() { |
| 312 |
b.ClearRect(b.Bounds()) |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
func (b *Buffer) ClearRect(rect Rectangle) { |
| 318 |
b.FillRect(nil, rect) |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
func (b *Buffer) InsertLine(y, n int, c *Cell) { |
| 328 |
b.InsertLineRect(y, n, c, b.Bounds()) |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 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 |
|
| 341 |
if y+n > rect.Max.Y { |
| 342 |
n = rect.Max.Y - y |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
for i := rect.Max.Y - 1; i >= y+n; i-- { |
| 347 |
for x := rect.Min.X; x < rect.Max.X; x++ { |
| 348 |
|
| 349 |
b.setCell(x, i, b.Lines[i-n][x], false) |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
|
| 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 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 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 |
|
| 372 |
if n > rect.Max.Y-y { |
| 373 |
n = rect.Max.Y - y |
| 374 |
} |
| 375 |
|
| 376 |
|
| 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 |
|
| 381 |
|
| 382 |
b.setCell(x, dst, b.Lines[src][x], false) |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 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 |
|
| 395 |
|
| 396 |
|
| 397 |
func (b *Buffer) DeleteLine(y, n int, c *Cell) { |
| 398 |
b.DeleteLineRect(y, n, c, b.Bounds()) |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
func (b *Buffer) InsertCell(x, y, n int, c *Cell) { |
| 406 |
b.InsertCellRect(x, y, n, c, b.Bounds()) |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 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 |
|
| 419 |
if x+n > rect.Max.X { |
| 420 |
n = rect.Max.X - x |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
for i := rect.Max.X - 1; i >= x+n && i-n >= rect.Min.X; i-- { |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
b.setCell(i, y, b.Lines[y][i-n], false) |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
for i := x; i < x+n && i < rect.Max.X; i++ { |
| 433 |
b.setCell(i, y, c, true) |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
func (b *Buffer) DeleteCell(x, y, n int, c *Cell) { |
| 442 |
b.DeleteCellRect(x, y, n, c, b.Bounds()) |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 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 |
|
| 455 |
remainingCells := rect.Max.X - x |
| 456 |
if n > remainingCells { |
| 457 |
n = remainingCells |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
for i := x; i < rect.Max.X-n; i++ { |
| 462 |
if i+n < rect.Max.X { |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
b.setCell(i, y, b.Lines[y][i+n], false) |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
for i := rect.Max.X - n; i < rect.Max.X; i++ { |
| 472 |
b.setCell(i, y, c, true) |
| 473 |
} |
| 474 |
} |