From db8e9992a153bca245fb94d1765a5560a9839fd3 Mon Sep 17 00:00:00 2001 From: Michael Lorant Date: Fri, 26 Jan 2024 13:22:12 +1100 Subject: [PATCH] Fix adding right padding to empty string An empty string could not have padding applied on the right side. Issue: #252 Signed-off-by: Michael Lorant --- style.go | 2 +- style_test.go | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/style.go b/style.go index 72be9ef3..ad96f79c 100644 --- a/style.go +++ b/style.go @@ -489,7 +489,7 @@ func padLeft(str string, n int, style *termenv.Style) string { // Apply right padding. func padRight(str string, n int, style *termenv.Style) string { - if n == 0 || str == "" { + if n == 0 { return str } diff --git a/style_test.go b/style_test.go index a5148db0..a8c799fb 100644 --- a/style_test.go +++ b/style_test.go @@ -329,33 +329,62 @@ func TestStyleValue(t *testing.T) { tt := []struct { name string + text string style Style expected string }{ { name: "empty", + text: "foo", style: NewStyle(), expected: "foo", }, { name: "set string", + text: "foo", style: NewStyle().SetString("bar"), expected: "bar foo", }, { name: "set string with bold", + text: "foo", style: NewStyle().SetString("bar").Bold(true), expected: "\x1b[1mbar foo\x1b[0m", }, { name: "new style with string", + text: "foo", style: NewStyle().SetString("bar", "foobar"), expected: "bar foobar foo", }, + { + name: "margin right", + text: "foo", + style: NewStyle().MarginRight(1), + expected: "foo ", + }, + { + name: "margin left", + text: "foo", + style: NewStyle().MarginLeft(1), + expected: " foo", + }, + { + name: "empty text margin right", + text: "", + style: NewStyle().MarginRight(1), + expected: " ", + }, + { + name: "empty text margin left", + text: "", + style: NewStyle().MarginLeft(1), + expected: " ", + }, } for i, tc := range tt { - res := tc.style.Render("foo") + res := tc.style.Render(tc.text) if res != tc.expected { t.Errorf("Test %d, expected:\n\n`%s`\n`%s`\n\nActual output:\n\n`%s`\n`%s`\n\n", i, tc.expected, formatEscapes(tc.expected),