Merge pull request #1513 from abitrolly/patch-1
wrap: Avoid trailing whitespace for empty lines
This commit is contained in:
commit
8335f5435f
19
help.go
19
help.go
@ -468,25 +468,28 @@ 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")
|
||||
|
||||
padding := strings.Repeat(" ", offset)
|
||||
|
||||
for i, line := range lines {
|
||||
if i != 0 {
|
||||
sb.WriteString(padding)
|
||||
}
|
||||
if line == "" {
|
||||
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 {
|
||||
|
13
help_test.go
13
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.
|
||||
@ -1276,7 +1283,7 @@ DESCRIPTION:
|
||||
App.Description string long
|
||||
enough that it should be
|
||||
wrapped in this test
|
||||
|
||||
|
||||
with a newline
|
||||
and an indented line
|
||||
|
||||
@ -1294,8 +1301,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?
|
||||
|
Loading…
Reference in New Issue
Block a user