Fix TextArea SetContent method

SetContent should overwrite the model, not append to it.

Add a test to check model's width and height after SetContent calls.
This commit is contained in:
VÖRÖSKŐI András 2020-04-26 20:36:01 +02:00 committed by Garrett D'Amore
parent 1f1f979c1b
commit e277d9c03b
2 changed files with 19 additions and 1 deletions

View File

@ -90,10 +90,11 @@ func (m *linesModel) GetCursor() (int, int, bool, bool) {
func (ta *TextArea) SetLines(lines []string) {
ta.Init()
m := ta.model
m.width =0
// extend slice before using m.runes[row] to avoid panic
slice := make([][]rune, len(lines))
m.runes = append(m.runes, slice...)
m.runes = slice
for row, line := range lines {
for _, ch := range line {

17
views/textarea_test.go Normal file
View File

@ -0,0 +1,17 @@
package views
import "testing"
func TestSetContent(t *testing.T) {
ta := &TextArea{}
ta.SetContent("This is a quite long line.") // This line is longer than 11.
ta.SetContent("Four.\nFive...\n...and Six.") //"...and Six." should be 11 long.
if ta.model.height != 3 {
t.Errorf("Incorrect height: %d, expected: %d", ta.model.height, 3)
}
if ta.model.width != 11 {
t.Errorf("Incorrect width: %d, expected: %d", ta.model.width, 11)
}
}