Skip to content

Commit

Permalink
fix(cellbuf): ensure inline mode first cursor movement moves to first…
Browse files Browse the repository at this point in the history
… column

When using the relative cursor mode, the first cursor movement should
move the cursor to the first column before moving to the target
position. This is necessary to ensure that the cursor is in the correct
position on first render.
  • Loading branch information
aymanbagabas committed Feb 7, 2025
1 parent 04d48ff commit ab21d71
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions cellbuf/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,12 @@ func moveCursor(s *Screen, x, y int, overwrite bool) (seq string) {

// moveCursor moves the cursor to the specified position.
func (s *Screen) moveCursor(x, y int, overwrite bool) {
if s.opts.RelativeCursor && s.cur.X == -1 && s.cur.Y == -1 {
// First cursor movement in inline mode, move the cursor to the first
// column before moving to the target position.
s.buf.WriteByte('\r') //nolint:errcheck
s.cur.X, s.cur.Y = 0, 0
}
s.buf.WriteString(moveCursor(s, x, y, overwrite)) //nolint:errcheck
s.cur.X, s.cur.Y = x, y
}
Expand Down Expand Up @@ -1334,10 +1340,6 @@ func (s *Screen) render() {
s.queuedText = false
}

// undefinedPos is the position used when the cursor position is undefined and
// in its initial state.
var undefinedPos = Pos(-1, -1)

// Close writes the final screen update and resets the screen.
func (s *Screen) Close() (err error) {
s.mu.Lock()
Expand Down Expand Up @@ -1372,11 +1374,7 @@ func (s *Screen) Close() (err error) {
func (s *Screen) reset() {
s.cursorHidden = false
s.altScreenMode = false
if s.opts.RelativeCursor {
s.cur = Cursor{}
} else {
s.cur = Cursor{Position: undefinedPos}
}
s.cur = Cursor{Position: Pos(-1, -1)} // start at -1 to force a move
s.saved = s.cur
s.touch = make(map[int]lineData, s.newbuf.Height())
if s.curbuf != nil {
Expand Down

0 comments on commit ab21d71

Please sign in to comment.