-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.go
179 lines (148 loc) · 3.34 KB
/
tree.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
package cmd
import (
"fmt"
"os"
"path"
"sort"
"strings"
"github.com/logrusorgru/aurora"
"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(treeCmd)
}
const (
SPACE = " "
STEM = "│ "
BRANCH = "├──"
CORNER = "└──"
)
type Node struct {
Level int
Children []*Node
Parent *Node
Name string
Path string
IsLast bool
}
func genNodes(root *Node, filename string, level int, filepath string) []*Node {
children := []*Node{}
files, err := os.ReadDir(filepath)
if err != nil {
return children
}
// sort by [..folder, ...file]
sort.Slice(files, func(i, j int) bool {
iIsDir := files[i].IsDir()
jIsDir := files[j].IsDir()
if !iIsDir && jIsDir {
return false
} else {
return true
}
})
for idx, f := range files {
isLast := idx == len(files)-1
child := &Node{
Level: level + 1,
Parent: root,
Name: f.Name(),
Path: path.Join(filepath, f.Name()),
IsLast: isLast,
}
child.Children = genNodes(child, f.Name(), level+1, path.Join(filepath, f.Name()))
children = append(children, child)
}
return children
}
var treeCmd = &cobra.Command{
Use: `tree`,
Short: `List contents of directories in a tree-like format`,
Long: `List contents of directories in a tree-like format`,
Run: func(treeCmd *cobra.Command, args []string) {
prompt := promptui.Select{
Label: "Select Traversal Strategy",
Items: []string{"Breadth First", "Depth First"},
}
_, result, err := prompt.Run()
if err != nil {
fmt.Println(aurora.Red(err.Error()), " Exiting...")
return
}
fmt.Println(aurora.Green(result))
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
// Default to current working directory
dir := cwd
// Or use path via 1st argument
if len(args) >= 1 {
dir = args[0]
}
// Generate in-memory tree
root := &Node{
Name: dir,
Level: 0,
Path: dir,
Parent: nil,
Children: nil,
IsLast: true,
}
root.Children = genNodes(root, dir, 0, dir)
switch result {
case "Breadth First":
bfs(root)
case "Depth First":
dfs(root, true)
}
},
}
// Not really search, just traversal
func bfs(root *Node) {
// Instantiate queue
queue := []*Node{}
// Enqueue root node
queue = append(queue, root)
// While the queue isn't empty
for len(queue) > 0 {
// Dequeue first element
el := queue[0]
queue = queue[1:]
// Enqueue its children
queue = append(queue, el.Children...)
fmt.Println(strings.Repeat(STEM, el.Level)+fmt.Sprintf("[%v]", el.Level)+">>", aurora.Blue(el.Name))
}
}
// Also not search, just traversal
func dfs(root *Node, isLast bool) {
// The symbol, preceding the filename
tip := BRANCH
if isLast {
tip = CORNER
}
symbols := []string{}
ptr := root
// Traverse backwards to draw all preceding branches
for ptr.Parent != nil {
// Draw leftwards
if ptr.Parent.IsLast {
// If a parent is the last node out of its siblings,
// draw empty space
symbols = append([]string{SPACE}, symbols...)
} else {
// else draw a stem
symbols = append([]string{STEM}, symbols...)
}
ptr = ptr.Parent
}
branch := strings.Join(symbols, "") + tip
name := aurora.Yellow(root.Name)
fmt.Println(branch, name)
for i, next := range root.Children {
isLast := i == len(root.Children)-1
dfs(next, isLast)
}
}