2020-10-01 13:56:05 +02:00
|
|
|
package buf2d
|
|
|
|
|
2021-01-15 21:40:09 +01:00
|
|
|
import (
|
|
|
|
"strings"
|
2023-07-10 20:25:19 +02:00
|
|
|
|
|
|
|
"github.com/mattn/go-runewidth"
|
2021-01-15 21:40:09 +01:00
|
|
|
)
|
2020-10-01 15:08:17 +02:00
|
|
|
|
2023-07-10 20:25:19 +02:00
|
|
|
// 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) {
|
2020-10-01 14:46:28 +02:00
|
|
|
dx := x
|
|
|
|
for _, r := range str {
|
2023-07-10 20:25:19 +02:00
|
|
|
if dx >= b.Width() {
|
2020-10-01 13:56:05 +02:00
|
|
|
return
|
|
|
|
}
|
2022-03-31 12:37:35 +02:00
|
|
|
b.Set(dx, y, r)
|
2023-07-10 20:25:19 +02:00
|
|
|
dx += runewidth.RuneWidth(r)
|
2020-10-01 13:56:05 +02:00
|
|
|
}
|
2023-07-10 20:25:19 +02:00
|
|
|
return dx - x
|
2020-10-01 13:56:05 +02:00
|
|
|
}
|
2020-10-01 14:14:43 +02:00
|
|
|
|
2020-10-01 15:08:17 +02:00
|
|
|
// WriteMultiLineString writes a multi-line string to the buffer at position (x,y)
|
|
|
|
// no word wrap is applied at all. If a line does not fit horizontally, it will be truncated
|
|
|
|
// All lines which do not fit vertically will be truncated as well
|
2022-03-31 12:37:35 +02:00
|
|
|
func WriteMultiLineString(b *Buffer[rune], str string, x, y int) {
|
2020-10-01 15:08:17 +02:00
|
|
|
lines := strings.Split(str, "\n")
|
|
|
|
for dy, line := range lines {
|
|
|
|
if dy >= b.height {
|
|
|
|
return
|
|
|
|
}
|
2022-03-31 12:37:35 +02:00
|
|
|
WriteString(b, line, x, y+dy)
|
2020-10-01 15:08:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 14:14:43 +02:00
|
|
|
// Fill fills the whole buffer with c
|
2022-03-31 12:37:35 +02:00
|
|
|
func (b *Buffer[T]) Fill(value T) {
|
2020-10-01 14:14:43 +02:00
|
|
|
for _, col := range b.data {
|
|
|
|
for ri := range col {
|
2022-03-31 12:37:35 +02:00
|
|
|
col[ri] = value
|
2020-10-01 14:14:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|