-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgmtree.go
281 lines (208 loc) · 5.27 KB
/
gmtree.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package main
import (
"bufio"
"errors"
"fmt"
"os"
"regexp"
"strings"
"time"
"github.com/urfave/cli/v2"
)
const (
// Box Drawings
lightHorizontal = "\u2500" // ─
lightVertical = "\u2502" // │
lightUpAndRight = "\u2514" // └
lightVerticalAndRight = "\u251C" // ├
// Basic Latin
space = "\u0020"
)
type Node struct {
Name string
Parent *Node
Next *Node
Children []*Node
}
func checkStdin() error {
fileInfo, err := os.Stdin.Stat()
if err != nil {
return err
}
if (fileInfo.Mode() & os.ModeNamedPipe) != os.ModeNamedPipe {
return errors.New("invalid stdin")
}
return nil
}
func getStdinSync() ([]string, error) {
var requirements []string
if err := checkStdin(); err != nil {
return requirements, err
}
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
requirements = append(requirements, s.Text())
}
return requirements, nil
}
func getStdinAsync() error {
if err := checkStdin(); err != nil {
return err
}
go func() {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
input = append(input, s.Text())
}
}()
return nil
}
func checkRequirements(requirements []string) error {
reg := regexp.MustCompile(`^.+\s.+$`)
for index, requirement := range requirements {
if !reg.MatchString(requirement) {
return fmt.Errorf("invalid requirement: \"%s\" on line %d", requirement, index+1)
}
}
return nil
}
func newNode(name string) *Node {
var children []*Node
return &Node{Name: name, Children: children}
}
func parseRequirements(requirements []string) *Node {
var tree *Node
for index, requirement := range requirements {
requirePair := strings.Split(requirement, " ")
if index == 0 {
tree = newNode(requirePair[0])
child := newNode(requirePair[1])
child.Parent = tree
tree.Children = append(tree.Children, child)
nodeMap[tree.Name] = tree
nodeMap[requirePair[1]] = child
} else {
var parent *Node
var child *Node
var ok bool
if child, ok = nodeMap[requirePair[1]]; !ok {
child = newNode(requirePair[1])
nodeMap[requirePair[1]] = child
}
// fmt.Printf("child: %s\n", child.Name)
if parent, ok = nodeMap[requirePair[0]]; !ok {
parent = newNode(requirePair[0])
nodeMap[requirePair[0]] = parent
}
if child.Parent != nil {
child = newNode(requirePair[1])
}
child.Parent = parent
// fmt.Printf("parent: %s\n", parent.Name)
if len(parent.Children) > 0 {
parent.Children[len(parent.Children)-1].Next = child
}
parent.Children = append(parent.Children, child)
}
}
return tree
}
func getPrefix(node *Node) string {
if node.Parent == nil || (node.Parent != nil && node.Parent.Parent == nil) {
return ""
}
prefix := strings.Repeat(space, indent)
if node.Parent.Next != nil {
prefix = lightVertical + prefix
} else {
prefix = space + prefix
}
prefix = getPrefix(node.Parent) + prefix
return prefix
}
func printTree(node *Node, offset int) {
// fmt.Printf("node: %s\n", node.Name)
if offset > 0 {
prefix := getPrefix(node)
if node.Next == nil {
prefix += lightUpAndRight
} else {
prefix += lightVerticalAndRight
}
fmt.Print(prefix + strings.Repeat(lightHorizontal, indent) + " " + node.Name + "\n")
} else {
fmt.Println(" " + node.Name)
}
// fmt.Printf("%s has %d child\n", node.Name, len(node.Children))
for _, child := range node.Children {
// fmt.Printf("child: %s\n", child.Name)
printTree(child, offset+indent)
}
}
func showHelp(c *cli.Context) {
if buildTime != "" && goVersion != "" {
fmt.Printf("%-15s%-s \n", "Built:", buildTime)
fmt.Printf("%-15s%-s \n\n", "Go version:", strings.Split(goVersion, " ")[2])
}
cli.ShowAppHelpAndExit(c, 0)
}
var (
indent int
input []string
nodeMap map[string]*Node
buildTime string
goVersion string
)
func main() {
app := &cli.App{
Name: "gmtree",
Authors: []*cli.Author{{Name: "Rujax Chen"}},
Version: "0.0.2",
Usage: "Convert `go mod graph` to treeview",
UsageText: "go mod graph | gmtree (> tree_file_path)",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "indent",
Aliases: []string{"i"},
Value: 2,
Usage: "Requirement's Indent",
Destination: &indent,
},
},
Action: func(c *cli.Context) error {
var requirements []string
if strings.Contains(strings.ToLower(os.Getenv("MSYSTEM")), "mingw") {
// Receive empty data from Pipe synchronously will block main goroutine if you are on MinGW.
fmt.Printf("Detected that you are executing this program on MinGW.\nPlease wait 2 seconds to receive Stdin from Pipe.\n\n")
if err := getStdinAsync(); err != nil {
fmt.Printf("Get Stdin error: %+v\n\n", err)
showHelp(c)
return nil
}
time.Sleep(time.Second * 2)
requirements = input
} else {
var err error
if requirements, err = getStdinSync(); err != nil {
fmt.Printf("Get Stdin error: %+v\n\n", err)
showHelp(c)
return nil
}
}
if len(requirements) == 0 {
return errors.New("invalid graph")
}
// fmt.Println(requirements)
if err := checkRequirements(requirements); err != nil {
return err
}
nodeMap = make(map[string]*Node)
tree := parseRequirements(requirements)
printTree(tree, 0)
return nil
},
}
if err := app.Run(os.Args); err != nil {
fmt.Printf("Run error: %+v", err)
}
}