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 <michael.lorant@nine.com.au>
This commit is contained in:
Michael Lorant 2024-01-31 09:38:39 +11:00 committed by GitHub
parent fb3000d55f
commit 04def9df3e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View File

@ -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
}

View File

@ -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),