fix(ui): panic in rightPad

This commit is contained in:
Rob Watson 2025-03-02 06:44:44 +01:00
parent c4287b75b2
commit 3efd009983
2 changed files with 46 additions and 1 deletions

View File

@ -464,8 +464,11 @@ func (a *Actor) showAbout() {
} }
func rightPad(s string, n int) string { func rightPad(s string, n int) string {
if s == "" { if s == "" || len(s) == n {
return s return s
} }
if len(s) > n {
return s[:n]
}
return s + strings.Repeat(" ", n-len(s)) return s + strings.Repeat(" ", n-len(s))
} }

42
terminal/actor_test.go Normal file
View File

@ -0,0 +1,42 @@
package terminal
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRightPad(t *testing.T) {
testCases := []struct {
name string
input string
want string
}{
{
name: "empty string",
input: "",
want: "",
},
{
name: "short string",
input: "foo",
want: "foo ",
},
{
name: "string with equal lenth to required width",
input: "foobar",
want: "foobar",
},
{
name: "long string",
input: "foobarbaz",
want: "foobar",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, rightPad(tc.input, 6))
})
}
}