Skip to content

Commit

Permalink
feat(cellbuf): add PrintAt, PrintTruncateAt functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jan 17, 2025
1 parent 1f2384c commit 03d64dd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
3 changes: 0 additions & 3 deletions cellbuf/utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package cellbuf

import (
"image/color"
"strings"

"github.com/charmbracelet/x/ansi"
)

// Height returns the height of a string.
Expand Down
31 changes: 25 additions & 6 deletions cellbuf/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,27 +188,46 @@ func (c *Window) MoveTo(x, y int) (v bool) {
return
}
c.cur.X, c.cur.Y = x, y
if c.s.opts.ShowCursor {
return c.s.MoveTo(c.x+x, c.y+y)
}
return true
return c.s.MoveTo(c.x+x, c.y+y)
}

// Print prints the given string at the current cursor position. If the cursor
// is out of bounds, it will do nothing.
// Print prints the given string at the current cursor position wrapping the
// text if necessary. If the cursor is out of bounds, it will do nothing.
func (c *Window) Print(format string, v ...interface{}) {
if len(v) > 0 {
format = fmt.Sprintf(format, v...)
}
c.drawString(format, c.cur.X, c.cur.Y, defaultDrawOpts)
}

// PrintAt prints the given string at the given position wrapping the text if
// necessary. If the position is out of bounds, it will do nothing.
func (c *Window) PrintAt(x, y int, format string, v ...interface{}) {
if !Pos(c.x+x, c.y+y).In(c.Bounds()) {
return
}
if len(v) > 0 {
format = fmt.Sprintf(format, v...)
}
c.drawString(format, x, y, defaultDrawOpts)
}

// PrintTruncate draws a string starting at the given position and
// truncates the string with the given tail if necessary.
func (c *Window) PrintTruncate(s string, tail string) {
c.drawString(s, c.cur.X, c.cur.Y, &drawOpts{tail: tail, truncate: true})
}

// PrintTruncateAt draws a string starting at the given position and
// truncates the string with the given tail if necessary.
// If the position is out of bounds, it will do nothing.
func (c *Window) PrintTruncateAt(x, y int, s string, tail string) {
if !Pos(c.x+x, c.y+y).In(c.Bounds()) {
return
}
c.drawString(s, x, y, &drawOpts{tail: tail, truncate: true})
}

// SetCell sets a cell at the given position. If the position is out of bounds,
// it will do nothing.
func (c *Window) SetCell(x, y int, cell *Cell) (v bool) {
Expand Down

0 comments on commit 03d64dd

Please sign in to comment.