-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚸 Improved formatting of flags help menu (#1259)
- Loading branch information
1 parent
0d55c68
commit 5732e38
Showing
4 changed files
with
179 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package utils | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
func WordWrap(s string, width int, indent string) string { | ||
var buffer bytes.Buffer | ||
|
||
lines := strings.Split(s, "\n") | ||
|
||
for _, line := range lines { | ||
wrapLine(line, width, indent, &buffer) | ||
} | ||
|
||
return buffer.String() | ||
} | ||
|
||
func wrapLine(line string, width int, indent string, buffer *bytes.Buffer) { | ||
if len(line) == 0 { | ||
buffer.WriteString("\n") | ||
return | ||
} | ||
|
||
var units []unit | ||
curUnit := unit{ | ||
isWhitespace: unicode.IsSpace(rune(line[0])), | ||
} | ||
startIdx := 0 | ||
for idx := 0; idx < len(line); idx++ { | ||
if unicode.IsSpace(rune(line[idx])) != curUnit.isWhitespace { | ||
curUnit.s = line[startIdx:idx] | ||
units = append(units, curUnit) | ||
curUnit = unit{isWhitespace: !curUnit.isWhitespace} | ||
startIdx = idx | ||
} | ||
} | ||
curUnit.s = line[startIdx:] | ||
units = append(units, curUnit) | ||
|
||
curLineLength := 0 | ||
var curLine []unit | ||
for idx, unit := range units { | ||
if curLineLength+len(unit.s)+len(indent) > width { | ||
writeUnits(curLine, width, indent, buffer) | ||
curLineLength, curLine = 0, nil | ||
} | ||
if !unit.isWhitespace || idx == 0 || curLineLength != 0 { | ||
curLine = append(curLine, unit) | ||
curLineLength += len(unit.s) | ||
} | ||
} | ||
writeUnits(curLine, width, indent, buffer) | ||
} | ||
|
||
func writeUnits(units []unit, width int, indent string, buffer *bytes.Buffer) { | ||
if units[len(units)-1].isWhitespace { | ||
units = units[:len(units)-1] | ||
} | ||
|
||
w := 0 | ||
for _, u := range units { | ||
w += len(u.s) | ||
} | ||
missing := width - w - len(indent) | ||
|
||
// Justify line | ||
if missing < 10 { | ||
idx := 0 | ||
for missing > 0 { | ||
if !units[idx].isWhitespace && idx != len(units)-1 { | ||
units[idx].s += " " | ||
missing-- | ||
} | ||
idx = (idx + 1) % len(units) | ||
} | ||
} | ||
|
||
buffer.WriteString(indent) | ||
for _, u := range units { | ||
buffer.WriteString(u.s) | ||
} | ||
buffer.WriteString("\n") | ||
} | ||
|
||
type unit struct { | ||
s string | ||
isWhitespace bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_WordWrap(t *testing.T) { | ||
//nolint:lll | ||
s := `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. | ||
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. | ||
Here are some bullet points: | ||
- point 1: hej | ||
- point 2: hej again | ||
- point 3: word word word word word word word word word word word word word word word word word word word word word` | ||
|
||
expected := `Lorem Ipsum is simply dummy text of the printing and | ||
typesetting industry. Lorem Ipsum has been the industry's | ||
standard dummy text ever since the 1500s, when an unknown | ||
printer took a galley of type and scrambled it to make a | ||
type specimen book. | ||
It has survived not only five centuries, but also the leap | ||
into electronic typesetting, remaining essentially | ||
unchanged. It was popularised in the 1960s with the release | ||
of Letraset sheets containing Lorem Ipsum passages, and more | ||
recently with desktop publishing software like Aldus | ||
PageMaker including versions of Lorem Ipsum. | ||
Here are some bullet points: | ||
- point 1: hej | ||
- point 2: hej again | ||
- point 3: word word word word word word word word word | ||
word word word word word word word word word word word word | ||
` | ||
|
||
wrapped := WordWrap(s, 60, "") | ||
require.Equal(t, expected, wrapped) | ||
} |