Skip to content

Commit

Permalink
fix: cellrenderer: reset cursor when at phantom cell
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 17, 2024
1 parent 3567f72 commit 41dd3ab
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions cell_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,10 @@ func (c *cellRenderer) changes() {
}

height := c.scr.Height()
var x int
if *c.lastRender == "" {
// We render the changes line by line to be able to get the cursor
// position using the width of each line.
var x int
for y := 0; y < height; y++ {
var line string
x, line = cellbuf.RenderLine(c.scr, y)
Expand Down Expand Up @@ -537,7 +537,6 @@ func (c *cellRenderer) flushSegment(seg *cellbuf.Segment, to image.Point, eraser
erased = true
} else {
c.renderSegment(seg)
c.scr.cur.X += seg.Width
}
return
}
Expand Down Expand Up @@ -567,6 +566,14 @@ func (c *cellRenderer) renderSegment(seg *cellbuf.Segment) {
}

c.buf.WriteString(seg.Content)
c.scr.cur.X += seg.Width

if c.scr.cur.X >= c.scr.Width() {
// NOTE: We need to reset the cursor when at phantom cell i.e. outside
// the screen, otherwise, the cursor position will be out of sync.
c.scr.cur.X = 0
c.buf.WriteByte(ansi.CR)
}
}

// moveCursor moves the cursor to the given position.
Expand Down Expand Up @@ -607,9 +614,9 @@ func (c *cellRenderer) moveCursor(x, y int) {
if dx >= 3 {
// [ansi.CursorLeft] is at least 3 bytes long, so we use [ansi.BS]
// when we can to avoid writing more bytes than necessary.
c.buf.WriteString(ansi.CursorLeft(c.scr.cur.X - x))
c.buf.WriteString(ansi.CursorLeft(dx))
} else {
c.buf.Write(bytes.Repeat([]byte{ansi.BS}, dx))
c.buf.WriteString(strings.Repeat("\b", dx))
}
}
}
Expand Down

0 comments on commit 41dd3ab

Please sign in to comment.