From 3f7774aaa3e16bf4ae622327de0bbf6f68970c6a Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Mon, 3 Oct 2022 18:53:26 +0300 Subject: [PATCH 1/5] wrap: Avoid trailing whitespace for empty lines --- help.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/help.go b/help.go index ba5f803..77be2b0 100644 --- a/help.go +++ b/help.go @@ -476,7 +476,9 @@ func wrap(input string, offset int, wrapAt int) string { for i, line := range lines { if i != 0 { - sb.WriteString(padding) + if len(line) > 0 { + sb.WriteString(padding) + } } sb.WriteString(wrapLine(line, offset, wrapAt, padding)) From 59d580978ed5e3dd54d2cb7d20d8bcb5f3cf585a Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Mon, 3 Oct 2022 19:05:46 +0300 Subject: [PATCH 2/5] Fix test for removed trailing whitespace --- help_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/help_test.go b/help_test.go index 5a50586..9c99b98 100644 --- a/help_test.go +++ b/help_test.go @@ -1276,7 +1276,7 @@ DESCRIPTION: App.Description string long enough that it should be wrapped in this test - + with a newline and an indented line @@ -1294,8 +1294,8 @@ COPYRIGHT: that it should be wrapped. Including newlines. And also indented lines. - - + + And then another long line. Blah blah blah does anybody ever read these things? From 1d2696072fcccd9f00aa8b1d07976139da3496ec Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Tue, 4 Oct 2022 10:30:31 +0300 Subject: [PATCH 3/5] wrap: Simplify loop logic Suggested by @julian7 --- help.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/help.go b/help.go index 77be2b0..697f0e1 100644 --- a/help.go +++ b/help.go @@ -475,10 +475,14 @@ func wrap(input string, offset int, wrapAt int) string { padding := strings.Repeat(" ", offset) for i, line := range lines { + if line == "" { + sb.WriteString("\n") + continue + } + + // the first line is not indented if i != 0 { - if len(line) > 0 { - sb.WriteString(padding) - } + sb.WriteString(padding) } sb.WriteString(wrapLine(line, offset, wrapAt, padding)) From 359e5a8d84019cc1942f4c72e7afdd40fa3dfc87 Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Tue, 4 Oct 2022 10:40:11 +0300 Subject: [PATCH 4/5] Run `go fmt` --- help.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/help.go b/help.go index 697f0e1..5db6696 100644 --- a/help.go +++ b/help.go @@ -479,7 +479,7 @@ func wrap(input string, offset int, wrapAt int) string { sb.WriteString("\n") continue } - + // the first line is not indented if i != 0 { sb.WriteString(padding) From 15b278907eb545b7ddd9cf60214202ed1f6dd950 Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Tue, 4 Oct 2022 13:13:52 +0300 Subject: [PATCH 5/5] Refactor `wrap()` and add test for empty line --- help.go | 23 ++++++++++------------- help_test.go | 7 +++++++ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/help.go b/help.go index 5db6696..6929a22 100644 --- a/help.go +++ b/help.go @@ -468,7 +468,7 @@ func nindent(spaces int, v string) string { } func wrap(input string, offset int, wrapAt int) string { - var sb strings.Builder + var ss []string lines := strings.Split(input, "\n") @@ -476,23 +476,20 @@ func wrap(input string, offset int, wrapAt int) string { for i, line := range lines { if line == "" { - sb.WriteString("\n") - continue - } - - // the first line is not indented - if i != 0 { - sb.WriteString(padding) - } + ss = append(ss, line) + } else { + wrapped := wrapLine(line, offset, wrapAt, padding) + if i == 0 { + ss = append(ss, wrapped) + } else { + ss = append(ss, padding+wrapped) - sb.WriteString(wrapLine(line, offset, wrapAt, padding)) + } - if i != len(lines)-1 { - sb.WriteString("\n") } } - return sb.String() + return strings.Join(ss, "\n") } func wrapLine(input string, offset int, wrapAt int, padding string) string { diff --git a/help_test.go b/help_test.go index 9c99b98..d7cdfa2 100644 --- a/help_test.go +++ b/help_test.go @@ -1213,6 +1213,13 @@ func TestDefaultCompleteWithFlags(t *testing.T) { } } +func TestWrap(t *testing.T) { + emptywrap := wrap("", 4, 16) + if emptywrap != "" { + t.Errorf("Wrapping empty line should return empty line. Got '%s'.", emptywrap) + } +} + func TestWrappedHelp(t *testing.T) { // Reset HelpPrinter after this test.