-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstring_aligner.go
80 lines (71 loc) · 2.22 KB
/
string_aligner.go
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
// -----------------------------------------------------------------------------
// ZR Library zr/[string_aligner.go]
// (c) balarabe@protonmail.com License: MIT
// -----------------------------------------------------------------------------
package zr
// StringAligner struct
// (ob *StringAligner) Clear()
// (ob *StringAligner) Write(row ...string)
// (ob *StringAligner) String() string
import (
"bytes"
"strings"
)
// StringAligner left-aligns columns of strings.
type StringAligner struct {
Values [][]string
Width map[int]int
Padding int
Prefix string
Suffix string
} // StringAligner
// Clear erases the content of the object.
func (ob *StringAligner) Clear() {
ob.Values = [][]string{}
} // Clear
// Write writes a row of strings to the object.
func (ob *StringAligner) Write(row ...string) {
if ob.Width == nil {
ob.Width = make(map[int]int, 10)
}
ob.Values = append(ob.Values, row)
for i, col := range row {
if ob.Width[i] < len(col) {
ob.Width[i] = len(col)
}
}
} // Write
// WriteBreak writes a row of strings to the object.
func (ob *StringAligner) WriteBreak() {
ob.Values = append(ob.Values, []string{})
} // WriteBreak
// String outputs the previously-written rows with columns aligned by spaces
// and implements the fmt.Stringer interface.
func (ob *StringAligner) String() string {
var (
retBuf = bytes.NewBuffer(make([]byte, 0, 1024))
ws = retBuf.WriteString
)
for i, row := range ob.Values {
if i > 0 {
ws("\r\n")
}
if ob.Prefix != "" && len(row) > 0 {
ws(ob.Prefix)
}
for i, col := range row {
ws(col)
if i < len(row)-1 {
spaces := ob.Width[i] - len(col) + ob.Padding
if spaces > 0 {
ws(strings.Repeat(" ", spaces))
}
}
}
if ob.Suffix != "" && len(row) > 0 {
ws(ob.Suffix)
}
}
return retBuf.String()
} // String
// end