From 3efd00998357890035f0d96a8153f3fe13384b5e Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Sun, 2 Mar 2025 06:44:44 +0100 Subject: [PATCH] fix(ui): panic in rightPad --- terminal/actor.go | 5 ++++- terminal/actor_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 terminal/actor_test.go diff --git a/terminal/actor.go b/terminal/actor.go index 37f60c4..187621e 100644 --- a/terminal/actor.go +++ b/terminal/actor.go @@ -464,8 +464,11 @@ func (a *Actor) showAbout() { } func rightPad(s string, n int) string { - if s == "" { + if s == "" || len(s) == n { return s } + if len(s) > n { + return s[:n] + } return s + strings.Repeat(" ", n-len(s)) } diff --git a/terminal/actor_test.go b/terminal/actor_test.go new file mode 100644 index 0000000..1720218 --- /dev/null +++ b/terminal/actor_test.go @@ -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)) + }) + } +}