Skip to content

Commit

Permalink
Move text helpers into textutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Dec 28, 2024
1 parent 12c9a03 commit 093890f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
38 changes: 4 additions & 34 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/mfridman/cli/pkg/suggest"
"github.com/mfridman/cli/pkg/textutil"
)

// NoExecError is returned when a command has no execution function.
Expand Down Expand Up @@ -126,7 +127,7 @@ func defaultUsage(c *Command) string {

// Short help section
if c.ShortHelp != "" {
for _, line := range wrapText(c.ShortHelp, 80) {
for _, line := range textutil.Wrap(c.ShortHelp, 80) {
b.WriteString(line)
b.WriteRune('\n')
}
Expand Down Expand Up @@ -178,7 +179,7 @@ func defaultUsage(c *Command) string {
nameWidth := maxLen + 4
wrapWidth := 80 - nameWidth

lines := wrapText(sub.ShortHelp, wrapWidth)
lines := textutil.Wrap(sub.ShortHelp, wrapWidth)
padding := strings.Repeat(" ", maxLen-len(sub.Name)+4)
fmt.Fprintf(&b, " %s%s%s\n", sub.Name, padding, lines[0])

Expand Down Expand Up @@ -265,7 +266,7 @@ func writeFlagSection(b *strings.Builder, flags []flagInfo, maxLen int, global b
usageText += fmt.Sprintf(" (default %s)", f.defval)
}

lines := wrapText(usageText, wrapWidth)
lines := textutil.Wrap(usageText, wrapWidth)
padding := strings.Repeat(" ", maxLen-len(f.name)+4)
fmt.Fprintf(b, " %s%s%s\n", f.name, padding, lines[0])

Expand All @@ -284,37 +285,6 @@ type flagInfo struct {
global bool
}

func wrapText(text string, width int) []string {
words := strings.Fields(text)
var (
lines []string
currentLine []string
currentLength int
)
for _, word := range words {
if currentLength+len(word)+1 > width {
if len(currentLine) > 0 {
lines = append(lines, strings.Join(currentLine, " "))
currentLine = []string{word}
currentLength = len(word)
} else {
lines = append(lines, word)
}
} else {
currentLine = append(currentLine, word)
if currentLength == 0 {
currentLength = len(word)
} else {
currentLength += len(word) + 1
}
}
}
if len(currentLine) > 0 {
lines = append(lines, strings.Join(currentLine, " "))
}
return lines
}

func formatFlagName(name string) string {
return "-" + name
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/textutil/textutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package textutil

import "strings"

func Wrap(text string, width int) []string {
words := strings.Fields(text)
var (
lines []string
currentLine []string
currentLength int
)
for _, word := range words {
if currentLength+len(word)+1 > width {
if len(currentLine) > 0 {
lines = append(lines, strings.Join(currentLine, " "))
currentLine = []string{word}
currentLength = len(word)
} else {
lines = append(lines, word)
}
} else {
currentLine = append(currentLine, word)
if currentLength == 0 {
currentLength = len(word)
} else {
currentLength += len(word) + 1
}
}
}
if len(currentLine) > 0 {
lines = append(lines, strings.Join(currentLine, " "))
}
return lines
}
4 changes: 2 additions & 2 deletions command_test.go → pkg/textutil/textutil_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cli
package textutil

import (
"testing"
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestWrapText(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := wrapText(tt.text, tt.width)
result := Wrap(tt.text, tt.width)
assert.EqualValues(t, tt.expected, result, "wrapped text mismatch for input %q with width %d", tt.text, tt.width)
})
}
Expand Down

0 comments on commit 093890f

Please sign in to comment.