-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.go
187 lines (157 loc) · 3.65 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
180
181
182
183
184
185
186
187
package cmd
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/logrusorgru/aurora"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(treeCmd)
}
const (
SPACE = " "
BRANCH = "│ "
STEM = "├──"
CORNER = "└──"
)
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) {
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 = path.Join(dir, args[0])
}
fmt.Println(aurora.Blue(dir))
fileCt, folderCt := dfs(dir, dir, map[string]string{})
fmt.Println("Files:", fileCt, "Folders:", folderCt)
},
}
// GIVEN a path like '/Users/kevin/repos/tk'
//
// RETURN true|false, if the path is the last entry amongst its siblings
func getIsLast(path string) bool {
dir := filepath.Dir(path) // Users/kevin/repos
base := filepath.Base(path) // tk
// TODO handle errors
files, _ := os.ReadDir(dir)
isLast := false
for i, file := range files {
if base == file.Name() {
isLast = i == len(files)-1
break
}
}
return isLast
}
func dfs(dir string, root string, cache map[string]string) (int, int) {
fileCt := 0
folderCt := 0
files, err := os.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
filePath := path.Join(dir, f.Name())
// TODO make ignore-cases configurable
switch name := f.Name(); {
case
strings.Contains(name, ".git"), // dot files
strings.Contains(name, "node_modules"): // node modules
continue
}
relativePath := strings.TrimPrefix(filePath, root)
relativePath = strings.Trim(relativePath, string(os.PathSeparator))
parts := strings.Split(relativePath, string(os.PathSeparator))
res := ""
abs := root
for i, part := range parts {
abs = path.Join(abs, part)
if cache[abs] != "" {
res = cache[abs]
} else {
isLast := getIsLast(abs)
if i == len(parts)-1 {
if isLast {
res += CORNER
} else {
res += STEM
}
} else {
if isLast {
res += SPACE
} else {
res += BRANCH
}
cache[abs] = res
}
}
}
fmt.Println(aurora.Gray(12, res), f.Name())
if f.IsDir() {
_fileCt, _folderCt := dfs(filePath, root, cache)
fileCt += _fileCt
folderCt += 1 + _folderCt
} else {
fileCt += 1
}
}
return fileCt, folderCt
}
// This func has the same output as `dfs` but relies on
// filepath.Walkdir.
// This func is only used in benchmark tests.
func dfs_walk(dir string, root string) (int, int) {
fileCt := 0
folderCt := 0
filepath.WalkDir(dir, func(_path string, d os.DirEntry, err error) error {
switch p := _path; {
case
p == root, // avoid infinite loop on root
strings.Contains(p, ".git"), // dot files
strings.Contains(p, "node_modules"): // node modules
return err
}
if d.IsDir() {
folderCt += 1
} else {
fileCt += 1
}
res := ""
relativePath := strings.TrimPrefix(_path, root)
relativePath = strings.Trim(relativePath, string(os.PathSeparator))
parts := strings.Split(relativePath, string(os.PathSeparator))
abs := root
for i, part := range parts {
abs = path.Join(abs, part)
isLast := getIsLast(abs)
if i == len(parts)-1 {
if isLast {
res += CORNER
} else {
res += STEM
}
} else {
if isLast {
res += SPACE
} else {
res += BRANCH
}
}
}
fmt.Println(res, d.Name())
return err
})
return fileCt, folderCt
}