From 04def9df3e907a8ca95a0b037a2a04fc680e1d31 Mon Sep 17 00:00:00 2001 From: Michael Lorant Date: Wed, 31 Jan 2024 09:38:39 +1100 Subject: [PATCH] fix: the empty string can have padding on the right (#253) 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 72be9ef..ad96f79 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 a5148db..a8c799f 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),