-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
60 lines (53 loc) · 1.72 KB
/
graph.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
package ui
import (
"fmt"
"github.com/zwoo-hq/zwooc/pkg/model"
"github.com/zwoo-hq/zwooc/pkg/tasks"
)
func GraphDependencies(collection tasks.Collection) {
for _, tasks := range collection {
fmt.Printf("task %s ", graphHeaderStyle.Render(tasks.Name))
fmt.Println(graphInfoStyle.Render(fmt.Sprintf("(%d total stages)", tasks.CountStages())))
tasks.RemoveEmptyNodes()
printNode(tasks, "", true)
}
}
func printNode(node *tasks.TaskTreeNode, prefix string, isLast bool) {
connector := "┬"
if len(node.Pre) == 0 && len(node.Post) == 0 {
connector = "─"
}
if isLast {
fmt.Printf("%s└─%s%s %s\n", prefix, connector, graphMainStyle.Render(node.Name), graphInfoStyle.Render(node.Main.Name()))
} else {
fmt.Printf("%s├─%s%s %s\n", prefix, connector, graphMainStyle.Render(node.Name), graphInfoStyle.Render(node.Main.Name()))
}
if len(node.Pre) > 0 {
newPrefix := "│ "
prePrefix := "│ "
if isLast {
prePrefix = " "
}
name := graphPreStyle.Render(model.KeyPre)
info := graphInfoStyle.Render(fmt.Sprintf("(%d tasks)", len(node.Pre)))
if len(node.Post) == 0 {
newPrefix = " "
fmt.Printf("%s%s└─┬%s %s\n", prefix, prePrefix, name, info)
} else {
fmt.Printf("%s%s├─┬%s %s\n", prefix, prePrefix, name, info)
}
for i, child := range node.Pre {
printNode(child, prefix+prePrefix+newPrefix, i == len(node.Pre)-1)
}
}
if len(node.Post) > 0 {
postPrefix := "│ "
if isLast {
postPrefix = " "
}
fmt.Printf("%s%s└─┬%s %s\n", prefix, postPrefix, graphPostStyle.Render(model.KeyPost), graphInfoStyle.Render(fmt.Sprintf("(%d tasks)", len(node.Post))))
for i, child := range node.Post {
printNode(child, prefix+postPrefix+" ", i == len(node.Post)-1)
}
}
}