-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.go
80 lines (66 loc) · 1.35 KB
/
printer.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
package prettycov
import (
"fmt"
"io"
"strings"
)
func DisplayTree(w io.Writer, tree *PathTree, depth uint) {
displayTree(w, tree, depth, " ", true, "")
}
func displayTree(w io.Writer, tree *PathTree, depth uint, padding string, root bool, key string) {
curDepth := strings.Count(key, "/")
if uint(curDepth) > depth {
return
}
if tree == nil {
return
}
index := 0
for k, v := range tree.Children {
_, _ = fmt.Fprintf(w, "%s%s - %.2f\n",
padding+symbol(root, getBoxType(index, len(tree.Children))), k, v.Coverage.Ratio)
displayTree(w, v, depth, padding+symbol(root, childSymbol(index, len(tree.Children))), false, key+"/"+k)
index++
}
}
type BoxType int
const (
Regular BoxType = iota
Last
AfterLast
Between
)
func (boxType BoxType) String() string {
switch boxType {
case Regular:
return "\u251c" // ├
case Last:
return "\u2514" // └
case AfterLast:
return " "
case Between:
return "\u2502" // │
default:
panic("invalid box type")
}
}
func getBoxType(index int, length int) BoxType {
if index+1 == length {
return Last
} else if index+1 > length {
return AfterLast
}
return Regular
}
func childSymbol(index int, length int) BoxType {
if index+1 == length {
return AfterLast
}
return Between
}
func symbol(root bool, boxType BoxType) string {
if root {
return ""
}
return boxType.String() + " "
}