DrawStr: made more efficient and renders newline characters

Before, DrawStr would copy all bytes into a []rune slice, and later
copy the rune slice (by accident) in the for i, v := range loop. Now,
it uses a []byte slice and iterates with for i := range slice.
This commit is contained in:
Luke I. Wilson
2021-04-02 13:30:37 -05:00
parent 9192d35b3c
commit f64af6fa29
2 changed files with 148 additions and 3 deletions

View File

@@ -18,9 +18,19 @@ func DrawRect(s tcell.Screen, x, y, width, height int, char rune, style tcell.St
// DrawStr will render each character of a string at `x` and `y`.
func DrawStr(s tcell.Screen, x, y int, str string, style tcell.Style) {
runes := []rune(str)
for idx, r := range runes {
s.SetContent(x+idx, y, r, nil, style)
var col int
bytes := []byte(str)
var i int
for i < len(bytes) {
r, size := utf8.DecodeRune(bytes[i:])
if r == '\n' {
col = 0
y++
} else {
s.SetContent(x+col, y, r, nil, style)
}
i += size
col++
}
}