Skip to content

Commit

Permalink
fixup! Add word-wrap support, with wrap length provided by the user
Browse files Browse the repository at this point in the history
only calculate the wrapped line padding once per wrap call
  • Loading branch information
mostynb committed Nov 7, 2020
1 parent a096039 commit 5b5e431
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions help.go
Expand Up @@ -386,12 +386,14 @@ func wrap(input string, offset int, wrapAt int) string {

lines := strings.Split(input, "\n")

padding := strings.Repeat(" ", offset)

for i, line := range lines {
if i != 0 {
sb.WriteString(strings.Repeat(" ", offset))
sb.WriteString(padding)
}

sb.WriteString(wrapLine(line, offset, wrapAt))
sb.WriteString(wrapLine(line, offset, wrapAt, padding))

if i != len(lines)-1 {
sb.WriteString("\n")
Expand All @@ -401,7 +403,7 @@ func wrap(input string, offset int, wrapAt int) string {
return sb.String()
}

func wrapLine(input string, offset int, wrapAt int) string {
func wrapLine(input string, offset int, wrapAt int, padding string) string {
if wrapAt <= offset || len(input) <= wrapAt-offset {
return input
}
Expand All @@ -416,7 +418,7 @@ func wrapLine(input string, offset int, wrapAt int) string {
spaceLeft := lineWidth - len(wrapped)
for _, word := range words[1:] {
if len(word)+1 > spaceLeft {
wrapped += "\n" + strings.Repeat(" ", offset) + word
wrapped += "\n" + padding + word
spaceLeft = lineWidth - len(word)
} else {
wrapped += " " + word
Expand Down

0 comments on commit 5b5e431

Please sign in to comment.