diff --git a/go.mod b/go.mod index 4ab6947..02838f9 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,7 @@ module git.milar.in/milarin/buf2d go 1.18 + +require github.com/mattn/go-runewidth v0.0.14 + +require github.com/rivo/uniseg v0.2.0 // indirect diff --git a/go.sum b/go.sum index e69de29..2579e19 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= diff --git a/write_string.go b/write_string.go index 522dc79..fe77652 100644 --- a/write_string.go +++ b/write_string.go @@ -2,19 +2,23 @@ package buf2d import ( "strings" + + "github.com/mattn/go-runewidth" ) -// WriteString writes a whole string to the buffer at position (x,y) -// no word wrap is applied at all. If the string does not fit, it will be truncated -func WriteString(b *Buffer[rune], str string, x, y int) { +// WriteString writes a whole string to the buffer at position (x,y). +// no word wrap is applied at all. If the string does not fit, it will be truncated. +// It returns the amount of runes in str +func WriteString(b *Buffer[rune], str string, x, y int) (width int) { dx := x for _, r := range str { - if dx >= b.width { + if dx >= b.Width() { return } b.Set(dx, y, r) - dx++ + dx += runewidth.RuneWidth(r) } + return dx - x } // WriteMultiLineString writes a multi-line string to the buffer at position (x,y)