-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
141 lines (127 loc) · 3.19 KB
/
main.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
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
package table
import (
"bytes"
"github.com/fatih/color"
"strings"
)
var MaxCellWidth = 40
var LineEndTag = "+"
var LineBody = "-"
var LineDivider = "|"
func trimTableCell(headers []string, content [][]string) {
for i, header := range headers {
headers[i] = strings.TrimSpace(header)
}
for i, c := range content {
for j, s := range c {
content[i][j] = strings.TrimSpace(s)
}
}
}
func Show(headers []string, content [][]string) {
trimTableCell(headers, content)
// 寻找每列最长字符
maxWidths := make([]int, len(headers), len(headers))
for i, header := range headers {
maxWidths[i] = len(header)
}
for _, row := range content {
for i, col := range row {
if i < len(maxWidths) {
max := maxWidths[i]
current := len(col)
if current > max {
maxWidths[i] = current
}
}
}
}
for i, width := range maxWidths {
if width > MaxCellWidth {
maxWidths[i] = MaxCellWidth
}
}
headerWriter := color.New(color.FgBlue, color.Bold)
// 输出header
headerWriter.Println(tableBorder(maxWidths))
headerWriter.Printf(row(headers, maxWidths))
headerWriter.Println(tableBorder(maxWidths))
contentWriter := color.New(color.FgBlue)
for _, c := range content {
contentWriter.Printf(row(c, maxWidths))
}
headerWriter.Println(tableBorder(maxWidths))
}
func tableBorder(maxWidths []int) string {
bufferString := bytes.NewBufferString("")
for i, width := range maxWidths {
width += 2
if i == len(maxWidths)-1 {
bufferString.WriteString(borderWithEnd(width, true))
} else {
bufferString.WriteString(borderWithEnd(width, false))
}
}
return bufferString.String()
}
func row(row []string, maxWidth []int) string {
maxLine := 0
for i, cell := range row {
if i >= len(maxWidth) {
break
}
lines := howManyLine(cell, maxWidth[i])
if lines > maxLine {
maxLine = lines
}
}
bufferString := bytes.NewBufferString("")
for i := 0; i < maxLine; i++ {
bufferString.WriteString(LineDivider)
for j, s := range row {
if j < len(maxWidth) {
bufferString.WriteString(" ")
bufferString.WriteString(fixStr(s, i, maxWidth[j]))
bufferString.WriteString(" ")
bufferString.WriteString(LineDivider)
}
}
bufferString.WriteString("\n")
}
return bufferString.String()
}
func borderWithEnd(length int, end bool) string {
bufferString := bytes.NewBufferString(LineEndTag)
for i := 0; i < length; i++ {
bufferString.WriteString(LineBody)
}
if end {
bufferString.WriteString(LineEndTag)
}
return bufferString.String()
}
func fixStr(str string, line int, size int) string {
currentLine := line * size
overLine := (line + 1) * size
if len(str) < currentLine {
bufferString := bytes.NewBufferString("")
for i := 0; i < size; i++ {
bufferString.WriteByte(' ')
}
return bufferString.String()
} else if len(str) > overLine { // 可以直接截断
return string([]rune(str)[currentLine:overLine])
} else {
endLength := len([]rune(str))
content := string([]rune(str)[currentLine:endLength])
bufferString := bytes.NewBufferString(content)
for i := 0; i < overLine-endLength; i++ {
bufferString.WriteByte(' ')
}
return bufferString.String()
}
}
func howManyLine(str string, cut int) int {
line := len(str) / (cut + 1)
return line + 1
}