Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove extra newline when message contains trailing newlines #387

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (k *Kong) applyHookToDefaultFlags(ctx *Context, node *Node, name string) er
}

func formatMultilineMessage(w io.Writer, leaders []string, format string, args ...interface{}) {
lines := strings.Split(fmt.Sprintf(format, args...), "\n")
lines := strings.Split(strings.TrimRight(fmt.Sprintf(format, args...), "\n"), "\n")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be changed to TrimSuffix if we want to remove one and only one newline at the end.

leader := ""
for _, l := range leaders {
if l == "" {
Expand Down
23 changes: 18 additions & 5 deletions kong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,11 +582,24 @@ func TestSliceWithDisabledSeparator(t *testing.T) {
}

func TestMultilineMessage(t *testing.T) {
w := &bytes.Buffer{}
var cli struct{}
p := mustNew(t, &cli, kong.Writers(w, w))
p.Printf("hello\nworld")
assert.Equal(t, "test: hello\n world\n", w.String())
tests := []struct {
name string
text string
want string
}{
{"Simple", "hello\nworld", "test: hello\n world\n"},
{"WithNewline", "hello\nworld\n", "test: hello\n world\n"},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
w := &bytes.Buffer{}
var cli struct{}
p := mustNew(t, &cli, kong.Writers(w, w))
p.Printf(test.text)
assert.Equal(t, test.want, w.String())
})
}
}

type cmdWithRun struct {
Expand Down
Loading