Skip to content

Commit

Permalink
feat(cellbuf): add MoveTo method
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Dec 9, 2024
1 parent df67bb2 commit 88fec18
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions cellbuf/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ type Screen struct {
newbuf *Buffer // the new buffer
queueAbove []string // the queue of strings to write above the screen
touch map[int][2]int
cur, saved Cursor // the current and saved cursors
cur, saved Cursor // the current and saved cursors
pos Position // the position of the cursor after the last render
opts ScreenOptions
mu sync.Mutex
lastChar rune // the last character written to the screen
Expand Down Expand Up @@ -1074,6 +1075,12 @@ func (s *Screen) render(b *bytes.Buffer) {

s.updatePen(b, nil) // nil indicates a blank cell with no styles

// Move the cursor to the specified position.
if s.pos != undefinedPos {
s.move(b, s.pos.X, s.pos.Y)
s.pos = undefinedPos
}

if b.Len() > 0 {
// Is the cursor visible? If so, disable it while rendering.
if s.opts.ShowCursor && !s.cursorHidden {
Expand All @@ -1086,6 +1093,10 @@ func (s *Screen) render(b *bytes.Buffer) {
}
}

// 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 @@ -1125,7 +1136,7 @@ func (s *Screen) reset() {
if s.opts.RelativeCursor {
s.cur = Cursor{}
} else {
s.cur = Cursor{Position: Position{X: -1, Y: -1}}
s.cur = Cursor{Position: undefinedPos}
}
s.saved = s.cur
s.touch = make(map[int][2]int)
Expand Down Expand Up @@ -1169,6 +1180,16 @@ func (s *Screen) Resize(width, height int) bool {
return true
}

// MoveTo moves the cursor to the specified position.
func (s *Screen) MoveTo(x, y int) bool {
pos := Pos(x, y)
if !s.Bounds().Contains(pos) {
return false
}
s.pos = pos
return true
}

// InsertAbove inserts string above the screen. The inserted string is not
// managed by the screen. This does nothing when alternate screen mode is
// enabled.
Expand Down Expand Up @@ -1208,6 +1229,7 @@ type Window interface {
Draw(x int, y int, cell *Cell) (v bool)
Bounds() Rectangle
Resize(width, height int) bool
MoveTo(x, y int) bool
}

// SubWindow represents a terminal SubWindow.
Expand All @@ -1231,6 +1253,17 @@ func (w *SubWindow) NewWindow(x, y, width, height int) (s *SubWindow, err error)
return
}

// MoveTo moves the cursor to the specified position.
func (w *SubWindow) MoveTo(x, y int) bool {
pos := Pos(x, y)
if !w.Bounds().Contains(pos) {
return false
}

x, y = w.bounds.Min.X+x, w.bounds.Min.Y+y
return w.scr.MoveTo(x, y)
}

// Cell implements Window.
func (w *SubWindow) Cell(x int, y int) *Cell {
if !Pos(x, y).In(w.Bounds().Rectangle) {
Expand Down

0 comments on commit 88fec18

Please sign in to comment.