-
Notifications
You must be signed in to change notification settings - Fork 6
/
formatter_markdown.go
49 lines (41 loc) · 987 Bytes
/
formatter_markdown.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
package main
import (
"strings"
"fmt"
)
func printMarkdownTable(header []string, content [][]string) {
var lengths = make([]int, len(header))
for i, h := range header {
lengths[i] = len([]rune(h))
}
for _, row := range content {
for i, value := range row {
lengths[i] = max(lengths[i], len([]rune(value)))
}
}
line := strings.Builder{}
line.WriteString("|")
for i, h := range header {
line.WriteString(" ")
line.WriteString(suffixToLength(h, lengths[i]))
line.WriteString(" |")
}
fmt.Println(line.String())
line.Reset()
line.WriteString("|")
for _, l := range lengths {
line.WriteString(strings.Repeat("-", l+2)) // a single space for one suffix and one prefix
line.WriteString("|")
}
println(line.String())
line.Reset()
for _, row := range content {
for i, value := range row {
line.WriteString("| ")
line.WriteString(suffixToLength(value, lengths[i]+1))
}
line.WriteString("|")
fmt.Println(line.String())
line.Reset()
}
}