Skip to content

Commit

Permalink
chore(cellbuf): merge pull request #224 from charmbracelet/cellbuf2
Browse files Browse the repository at this point in the history
Cellbuf: refactor, fix bugs, and tidy
  • Loading branch information
aymanbagabas authored Oct 28, 2024
2 parents 1db682b + 9c548f1 commit e3a3eae
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 318 deletions.
53 changes: 45 additions & 8 deletions cellbuf/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ func (b *Buffer) Height() int {
return len(b.cells) / b.width
}

// At returns the cell at the given x, y position.
func (b *Buffer) At(x, y int) (Cell, error) {
// Cell returns the cell at the given x, y position.
func (b *Buffer) Cell(x, y int) (Cell, bool) {
if b.width == 0 {
return Cell{}, ErrOutOfBounds
return Cell{}, false
}
height := len(b.cells) / b.width
if x < 0 || x >= b.width || y < 0 || y >= height {
return Cell{}, ErrOutOfBounds
return Cell{}, false
}
idx := y*b.width + x
if idx < 0 || idx >= len(b.cells) {
return Cell{}, ErrOutOfBounds
return Cell{}, false
}
return b.cells[idx], nil
return b.cells[idx], true
}

// Set sets the cell at the given x, y position.
func (b *Buffer) Set(x, y int, c Cell) (v bool) {
// SetCell sets the cell at the given x, y position.
func (b *Buffer) SetCell(x, y int, c Cell) (v bool) {
if b.width == 0 {
return
}
Expand All @@ -49,7 +49,44 @@ func (b *Buffer) Set(x, y int, c Cell) (v bool) {
return
}

// When a wide cell is partially overwritten, we need
// to fill the rest of the cell with space cells to
// avoid rendering issues.
prev := b.cells[idx]
if prev.Width > 1 {
// Writing to the first wide cell
for j := 0; j < prev.Width; j++ {
newCell := prev
newCell.Content = " "
newCell.Width = 1
b.cells[idx+j] = newCell
}
} else if prev.Width == 0 {
// Writing to wide cell placeholders
for j := 1; j < 4; j++ {
wide := b.cells[idx-j]
if wide.Width > 1 {
for k := 0; k < wide.Width; k++ {
newCell := wide
newCell.Content = " "
newCell.Width = 1
b.cells[idx-j+k] = newCell
}
break
}
}
}

b.cells[idx] = c

// Mark wide cells with emptyCell zero width
// We set the wide cell down below
if c.Width > 1 {
for j := 1; j < c.Width; j++ {
b.cells[idx+j] = emptyCell
}
}

return true
}

Expand Down
15 changes: 3 additions & 12 deletions cellbuf/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,9 @@ type Cell struct {

// Equal returns whether the cell is equal to the other cell.
func (c Cell) Equal(o Cell) bool {
spaceStyleEqual := func(lhs, rhs Style) bool {
return colorEqual(lhs.Bg, rhs.Bg) &&
colorEqual(lhs.Ul, rhs.Ul) &&
lhs.Attrs == rhs.Attrs &&
lhs.UlStyle == rhs.UlStyle
}

return c.Content == o.Content &&
// OPTIM: If the cell is a space, we don't care about its FG color.
((c.Content == " " && spaceStyleEqual(c.Style, o.Style)) ||
(c.Style.Equal(o.Style))) &&
c.Width == o.Width &&
return c.Width == o.Width &&
c.Content == o.Content &&
c.Style.Equal(o.Style) &&
c.Link.Equal(o.Link)
}

Expand Down
2 changes: 1 addition & 1 deletion cellbuf/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/charmbracelet/x/cellbuf
go 1.18

require (
github.com/charmbracelet/x/ansi v0.3.2
github.com/charmbracelet/x/ansi v0.4.0
github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91
)

Expand Down
4 changes: 2 additions & 2 deletions cellbuf/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/charmbracelet/x/ansi v0.3.2 h1:wsEwgAN+C9U06l9dCVMX0/L3x7ptvY1qmjMwyfE6USY=
github.com/charmbracelet/x/ansi v0.3.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/ansi v0.4.0 h1:NqwHA4B23VwsDn4H3VcNX1W1tOmgnvY1NDx5tOXdnOU=
github.com/charmbracelet/x/ansi v0.4.0/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91 h1:D5OO0lVavz7A+Swdhp62F9gbkibxmz9B2hZ/jVdMPf0=
github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91/go.mod h1:Ey8PFmYwH+/td9bpiEx07Fdx9ZVkxfIjWXxBluxF4Nw=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
Expand Down
256 changes: 0 additions & 256 deletions cellbuf/grid_write.go

This file was deleted.

8 changes: 4 additions & 4 deletions cellbuf/method.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cellbuf

// WidthMethod is a type that represents the how the renderer should calculate
// the display width of cells.
type WidthMethod uint8
// Method is a type that represents the how the renderer should calculate the
// display width of cells.
type Method uint8

// Display width modes.
const (
WcWidth WidthMethod = iota
WcWidth Method = iota
GraphemeWidth
)
Loading

0 comments on commit e3a3eae

Please sign in to comment.