Refactor wrap()
and add test for empty line
This commit is contained in:
parent
359e5a8d84
commit
15b278907e
23
help.go
23
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
|
||||
}
|
||||
ss = append(ss, line)
|
||||
} else {
|
||||
wrapped := wrapLine(line, offset, wrapAt, padding)
|
||||
if i == 0 {
|
||||
ss = append(ss, wrapped)
|
||||
} else {
|
||||
ss = append(ss, padding+wrapped)
|
||||
|
||||
// the first line is not indented
|
||||
if i != 0 {
|
||||
sb.WriteString(padding)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user