-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutliner.go
106 lines (86 loc) · 2.14 KB
/
outliner.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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
type Options struct {
maxDepth int
showHidden bool
excludeFilter string
showTimer bool
}
func main() {
maxDepth := flag.Int("depth", -1, "Maximum depth to traverse (-1 for unlimited)")
showHidden := flag.Bool("hidden", false, "Show hidden files and directories")
excludeFilter := flag.String("exclude", "", "Comma-separated list of names to exclude")
showTimer := flag.Bool("timer", false, "Show execution time")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTIONS] [DIRECTORY]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Options:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nIf no directory is specified, the current directory will be used.\n")
}
flag.Parse()
root := "."
if args := flag.Args(); len(args) > 0 {
root = args[0]
}
opts := Options{
maxDepth: *maxDepth,
showHidden: *showHidden,
excludeFilter: *excludeFilter,
showTimer: *showTimer,
}
startTime := time.Now()
fmt.Println(root)
printTree(root, "", 0, opts)
if opts.showTimer {
duration := time.Since(startTime)
fmt.Printf("\nExecution time: %v\n", duration)
}
}
// Basic DFS traversal of the file system
func printTree(path string, prefix string, depth int, opts Options) error {
if opts.maxDepth != -1 && depth >= opts.maxDepth {
return nil
}
entries, err := os.ReadDir(path)
if err != nil {
return err
}
excludeList := strings.Split(opts.excludeFilter, ",")
for i, entry := range entries {
name := entry.Name()
if !opts.showHidden && strings.HasPrefix(name, ".") {
continue
}
// Skip excluded files/directories
excluded := false
for _, exclude := range excludeList {
if exclude != "" && name == exclude {
excluded = true
break
}
}
if excluded {
continue
}
isLast := i == len(entries)-1
newPrefix := prefix
if isLast {
fmt.Printf("%s└── %s\n", prefix, name)
newPrefix += " "
} else {
fmt.Printf("%s├── %s\n", prefix, name)
newPrefix += "│ "
}
if entry.IsDir() {
printTree(filepath.Join(path, name), newPrefix, depth+1, opts)
}
}
return nil
}