Skip to content

Commit

Permalink
feat: update cellbuf to v0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 28, 2024
1 parent 945617e commit 7d137bf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
29 changes: 18 additions & 11 deletions cell_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func (s *screen) reset() {
}

// Set implements [cellbuf.Grid] and marks changed cells as dirty.
func (s *screen) Set(x, y int, cell cellbuf.Cell) (v bool) {
c, err := s.At(x, y)
if err != nil {
func (s *screen) SetCell(x, y int, cell cellbuf.Cell) (v bool) {
c, ok := s.Cell(x, y)
if !ok {
return
}

Expand All @@ -55,7 +55,7 @@ func (s *screen) Set(x, y int, cell cellbuf.Cell) (v bool) {
// Mark the cell as dirty. You nasty one ;)
idx := y*s.Width() + x
s.dirty[idx] = 1
return s.Buffer.Set(x, y, cell)
return s.Buffer.SetCell(x, y, cell)
}

// cellRenderer is a cell-based terminal renderer.
Expand All @@ -67,9 +67,12 @@ type cellRenderer struct {
scrs [2]screen // Both inline and alt-screen
scr *screen // Points to the current used screen

method cellbuf.WidthMethod
pen cellbuf.Style
link cellbuf.Link
method cellbuf.Method

finalCur image.Point // The final cursor position

pen cellbuf.Style
link cellbuf.Link

queueAbove []string
lastRenders [2]string // The last render for both inline and alt-screen buffers
Expand Down Expand Up @@ -200,7 +203,7 @@ func (c *cellRenderer) render(s string) {
// Ensure the buffer is at least the height of the new frame.
height := cellbuf.Height(s)
c.scr.Resize(c.scr.Width(), height)
linew := c.method.SetContent(c.scr, s)
linew := cellbuf.SetContent(c.scr, c.method, s)
c.scr.linew = linew
}

Expand Down Expand Up @@ -362,7 +365,10 @@ func (c *cellRenderer) changes() {
var segX int // The start position of the current segment.
var eraser bool // Whether we're erasing using spaces and no styles or links.
for x := 0; x < width; x++ {
cell, _ := c.scr.At(x, y)
cell, ok := c.scr.Cell(x, y)
if !ok || cell.Width == 0 {
continue
}
if !c.scr.isDirty(x, y) {
if seg != nil {
erased := c.flushSegment(seg, image.Pt(segX, y), eraser)
Expand Down Expand Up @@ -452,6 +458,7 @@ func (c *cellRenderer) flushSegment(seg *cellbuf.Segment, to image.Point, eraser
// 2. The segment reaches the end of the line to erase i.e. the new line is shorter.
// 3. The segment takes more bytes than [ansi.EraseLineRight] to erase which is 4 bytes.
if eraser && to.Y < len(c.scr.linew) && seg.Width > 4 && (c.scr.linew)[to.Y] < seg.Width+to.X {
c.renderReset(seg)
c.buf.WriteString(ansi.EraseLineRight) //nolint:errcheck
erased = true
} else {
Expand Down Expand Up @@ -513,8 +520,8 @@ func (c *cellRenderer) moveCursor(x, y int) {
// OPTIM: We write the cell content under the cursor if it's the same
// style and link. This is more efficient than moving the cursor which
// costs at least 3 bytes [ansi.CursorRight].
cell, err := c.scr.At(c.scr.cur.X, c.scr.cur.Y)
if err == nil &&
cell, ok := c.scr.Cell(c.scr.cur.X, c.scr.cur.Y)
if ok &&
(cell.Style.Equal(c.pen) && cell.Link == c.link) {
c.buf.WriteString(cell.Content)
break
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/charmbracelet/x/ansi v0.4.0
github.com/charmbracelet/x/cellbuf v0.0.1
github.com/charmbracelet/x/cellbuf v0.0.2
github.com/charmbracelet/x/term v0.2.0
github.com/charmbracelet/x/windows v0.2.0
github.com/muesli/cancelreader v0.2.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/charmbracelet/x/ansi v0.4.0 h1:NqwHA4B23VwsDn4H3VcNX1W1tOmgnvY1NDx5tO
github.com/charmbracelet/x/ansi v0.4.0/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/cellbuf v0.0.1 h1:us+C8dDO1NzO+2I8WItenR6V/oBZr7oVmhU4n3mi4Yg=
github.com/charmbracelet/x/cellbuf v0.0.1/go.mod h1:mFpvlGowTd0Fiv4TdoKyGZaZdigSUHtBJralbADonwE=
github.com/charmbracelet/x/cellbuf v0.0.2 h1:m2HAmB4I8Zf7cQhvVrm5AHiZGKtzMWJV3RvBuBXqSiU=
github.com/charmbracelet/x/cellbuf v0.0.2/go.mod h1:lX6M1begBn73Vjeg85MfYIoIqUwo1T3KGr6sIAUnb0s=
github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=
github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0=
github.com/charmbracelet/x/wcwidth v0.0.0-20241011142426-46044092ad91 h1:D5OO0lVavz7A+Swdhp62F9gbkibxmz9B2hZ/jVdMPf0=
Expand Down

0 comments on commit 7d137bf

Please sign in to comment.