From cf2d265d255d6bb42e91452f2528b4a0b7080267 Mon Sep 17 00:00:00 2001 From: bashbunni Date: Mon, 7 Oct 2024 14:39:55 -0700 Subject: [PATCH 1/4] fix(render): strip carriage returns from strings --- style.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/style.go b/style.go index 2aba56b0..19a0f8f2 100644 --- a/style.go +++ b/style.go @@ -353,6 +353,8 @@ func (s Style) Render(strs ...string) string { // Potentially convert tabs to spaces str = s.maybeConvertTabs(str) + // carriage returns can cause strange behaviour when rendering. + str = strings.ReplaceAll(str, "\r", "") // Strip newlines in single line mode if inline { From 041e75095629793e59c58634d7b90ed7cfd39b0b Mon Sep 17 00:00:00 2001 From: bashbunni Date: Mon, 7 Oct 2024 16:38:12 -0700 Subject: [PATCH 2/4] test(render): add test to reproduce carriage return issues --- style_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/style_test.go b/style_test.go index eacaadf5..0db740b4 100644 --- a/style_test.go +++ b/style_test.go @@ -1,6 +1,7 @@ package lipgloss import ( + "fmt" "io" "reflect" "strings" @@ -576,3 +577,16 @@ func requireNotEqual(tb testing.TB, a, b interface{}) { tb.FailNow() } } + +func TestCarriageReturnInRender(t *testing.T) { + out := fmt.Sprintf("%s\r\n%s\r\n", "Super duper california oranges", "Hello world") + testStyle := NewStyle(). + MarginLeft(1) + got := testStyle.Render(string(out)) + want := testStyle.Render(fmt.Sprintf("%s\n%s\n", "Super duper california oranges", "Hello world")) + + if got != want { + t.Logf("got(detailed):\n%q\nwant(detailed):\n%q", got, want) + t.Fatalf("got(string):\n%s\nwant(string):\n%s", got, want) + } +} From 177f51842ba476adea92c31319702c477b9e4ca0 Mon Sep 17 00:00:00 2001 From: bashbunni <15822994+bashbunni@users.noreply.github.com> Date: Wed, 9 Oct 2024 10:02:54 -0700 Subject: [PATCH 3/4] feat: replace \r\n with \n instead of removing \r Co-authored-by: Ayman Bagabas --- style.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/style.go b/style.go index 19a0f8f2..0eb5c016 100644 --- a/style.go +++ b/style.go @@ -354,7 +354,7 @@ func (s Style) Render(strs ...string) string { // Potentially convert tabs to spaces str = s.maybeConvertTabs(str) // carriage returns can cause strange behaviour when rendering. - str = strings.ReplaceAll(str, "\r", "") + str = strings.ReplaceAll(str, "\r\n", "\n") // Strip newlines in single line mode if inline { From 6187f1cd8ff62ecb2b7181ca99ea62190e54a017 Mon Sep 17 00:00:00 2001 From: bashbunni Date: Wed, 9 Oct 2024 10:14:50 -0700 Subject: [PATCH 4/4] test(table): add test for carriage return in cell data --- table/table_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/table/table_test.go b/table/table_test.go index fbe18812..cf737a0d 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -1158,6 +1158,34 @@ func TestClearRows(t *testing.T) { table.String() } +func TestCarriageReturn(t *testing.T) { + data := [][]string{ + {"a0", "b0", "c0", "d0"}, + {"a1", "b1.0\r\nb1.1\r\nb1.2\r\nb1.3\r\nb1.4\r\nb1.5\r\nb1.6", "c1", "d1"}, + {"a2", "b2", "c2", "d2"}, + {"a3", "b3", "c3", "d3"}, + } + table := New().Rows(data...).Border(lipgloss.NormalBorder()) + got := table.String() + want := `┌──┬────┬──┬──┐ +│a0│b0 │c0│d0│ +│a1│b1.0│c1│d1│ +│ │b1.1│ │ │ +│ │b1.2│ │ │ +│ │b1.3│ │ │ +│ │b1.4│ │ │ +│ │b1.5│ │ │ +│ │b1.6│ │ │ +│a2│b2 │c2│d2│ +│a3│b3 │c3│d3│ +└──┴────┴──┴──┘` + + if got != want { + t.Logf("detailed view...\ngot:\n%q\nwant:\n%q", got, want) + t.Fatalf("got:\n%s\nwant:\n%s", got, want) + } +} + func debug(s string) string { return strings.ReplaceAll(s, " ", ".") }