From b08e7e4762ecc3956a7065ce2d78f6fb8dfdf0ee Mon Sep 17 00:00:00 2001 From: bashbunni <15822994+bashbunni@users.noreply.github.com> Date: Thu, 3 Oct 2024 10:05:17 -0700 Subject: [PATCH] fix(table): unset data rows without causing nil pointer err (#372) * fix(table): unset data rows without causing nil pointer err * test(table): add ClearRows test to repro nil ptr err --- table/table.go | 2 +- table/table_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/table/table.go b/table/table.go index 8ad93053..1bef6717 100644 --- a/table/table.go +++ b/table/table.go @@ -85,7 +85,7 @@ func New() *Table { // ClearRows clears the table rows. func (t *Table) ClearRows() *Table { - t.data = nil + t.data = NewStringData() return t } diff --git a/table/table_test.go b/table/table_test.go index 0138d238..fbe18812 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -1140,6 +1140,24 @@ func TestTableHeightWithOffset(t *testing.T) { } } +func TestClearRows(t *testing.T) { + defer func() { + if r := recover(); r != nil { + t.Fatalf("had to recover: %v", r) + } + }() + + table := New(). + Border(lipgloss.NormalBorder()). + Headers("LANGUAGE", "FORMAL", "INFORMAL"). + Row("Chinese", "Nǐn hǎo", "Nǐ hǎo") + table.ClearRows() + table.Row("French", "Bonjour", "Salut") + + // String() will try to get the rows from table.data + table.String() +} + func debug(s string) string { return strings.ReplaceAll(s, " ", ".") }