-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.go
More file actions
58 lines (48 loc) · 1.31 KB
/
utils.go
File metadata and controls
58 lines (48 loc) · 1.31 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
package argparse
import (
"os"
"strings"
"golang.org/x/term"
)
func decideTerminalWidth() int {
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
return 80
}
return width
}
func formatHelpRow(head, content string, bareHeadLength, maxHeadLength, terminalWidth int, withBreak bool) string {
content = strings.Replace(content, "\n", "", -1)
head = " " + head + " " // head content
bareHeadLength += 3 // head length without control chars
// length of a single content row, constant
contentRowLen := terminalWidth - maxHeadLength
min := func(x, y int) int {
if x < y {
return x
} else {
return y
}
}
var rows []string
if withBreak && maxHeadLength < bareHeadLength {
rows = append(rows, head)
} else {
// no break -> head is on the same row
// as first content line
var headRowPadding string
if maxHeadLength > bareHeadLength {
headRowPadding = strings.Repeat(" ", maxHeadLength-bareHeadLength)
}
rowLen := min(contentRowLen, len(content))
rows = append(rows, head+headRowPadding+content[:rowLen])
content = content[rowLen:]
}
rowPadding := strings.Repeat(" ", maxHeadLength)
for content != "" {
rowLen := min(contentRowLen, len(content))
rows = append(rows, rowPadding+content[:rowLen])
content = content[rowLen:]
}
return strings.Join(rows, "\n")
}