-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.go
More file actions
165 lines (147 loc) · 3.81 KB
/
format.go
File metadata and controls
165 lines (147 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package cmd
import (
"fmt"
"regexp"
"strings"
)
// formatHelp formats the help text for Cmd or Group.
func formatHelp(usage, summary, details string, defs []*definitionList) string {
columns := terminalColumns()
sections := []string{}
sections = append(sections, wrapParagraphs(usage, columns))
if summary != "" {
sections = append(sections, wrapParagraphs(summary, columns))
}
for _, d := range defs {
if len(d.definitions) == 0 {
continue
}
sections = append(sections, d.format(columns))
}
if details != "" {
sections = append(sections, wrapParagraphs(details, columns))
}
return strings.Join(sections, "\n")
}
var whitespaceRe = regexp.MustCompile(`\s+`)
// wrapParagraphs wraps text to fit the given number of columns, preserving paragraphs separated by
// a blank line.
func wrapParagraphs(text string, columns int) string {
input := strings.Split(text, "\n\n")
output := []string{}
for _, p := range input {
b := new(strings.Builder)
lines := wrapText(p, columns)
for _, line := range lines {
fmt.Fprintln(b, line)
}
output = append(output, b.String())
}
return strings.Join(output, "\n")
}
// wrapText splits text into lines that fit the given number of columns.
func wrapText(text string, columns int) []string {
text = strings.TrimSpace(text)
// split text into words and convert to []rune
words := whitespaceRe.Split(text, -1)
runeWords := make([][]rune, len(words))
for i, word := range words {
runeWords[i] = []rune(word)
}
// join words into lines
lines := []string{}
current := []rune{}
firstWord := true
for _, word := range runeWords {
if len(current)+1+len(word) > columns {
lines = append(lines, string(current))
current = word
continue
}
if !firstWord {
current = append(current, ' ')
}
current = append(current, word...)
firstWord = false
}
lines = append(lines, string(current))
return lines
}
// A definitionList is used to represent a list of flags, commands, or groups.
type definitionList struct {
title string
definitions []*definition
}
// format prints the definitionList in a nicely-formatted way that fits in the given number of
// columns.
func (d *definitionList) format(columns int) string {
b := new(strings.Builder)
// set a maximum for left column
maxLeftCols := (columns - 4) / 2
if maxLeftCols > 25 {
maxLeftCols = 25
}
// get text for left column
terms := []termLines{}
for _, def := range d.definitions {
terms = append(terms, def.formatTerms(maxLeftCols))
}
// find out size of left and right column
leftCols := 0
for _, t := range terms {
if t.inline == "" {
continue
}
cols := len([]rune(t.inline))
if cols > leftCols {
leftCols = cols
}
}
rightCols := columns - 4 - leftCols
if rightCols > 80 {
rightCols = 80
}
// print text
fmt.Fprintf(b, "%s:\n", d.title)
for i, def := range d.definitions {
flagDef := terms[i]
for _, line := range flagDef.separate {
fmt.Fprintf(b, " %s\n", line)
}
usageLines := wrapText(def.text, rightCols)
fmt.Fprintf(b, " %-*s %s\n", leftCols, flagDef.inline, usageLines[0])
for _, line := range usageLines[1:] {
fmt.Fprintf(b, "%*s%s\n", 2+leftCols+2, "", line)
}
}
return b.String()
}
// A definition describes one definition.
type definition struct {
terms []string
text string
}
// termLines defines the text printed in the left column for a definition.
type termLines struct {
separate []string
inline string
}
// formatTerms formats the left columns for a definition.
func (d *definition) formatTerms(maxCols int) termLines {
joined := strings.Join(d.terms, ", ")
last := len(d.terms) - 1
if len([]rune(d.terms[last])) > maxCols {
return termLines{
separate: d.terms,
}
}
if len([]rune(joined)) > maxCols {
return termLines{
separate: d.terms[:last],
inline: d.terms[last],
}
}
return termLines{
inline: joined,
}
}