-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (148 loc) · 3.32 KB
/
main.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
package main
import (
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"sort"
"time"
"github.com/atomicgo/cursor"
)
type fileInfo struct {
Name string
Size int64
Path string
}
type topLevel struct {
Name string
NumFiles int64
Bytes int64
}
var fileChan = make(chan fileInfo, 1000)
func main() {
fmt.Println()
targetPath := "."
if len(os.Args) > 1 {
targetPath = os.Args[1]
}
fileMap := make(map[string]*topLevel)
// Files in top level of directory:
files, err := dirsInDir(targetPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
type done struct{}
dirsOpenChan := make(chan done, len(files))
for _, file := range files {
go func(file string) {
collectDirStats(targetPath + "/" + file)
dirsOpenChan <- done{}
}(file.Name())
}
// goroutine that waits for all dir listings to be finished:
go func() {
for range files {
<-dirsOpenChan
}
close(fileChan)
}()
// ... consume and report results:
prevHeight := 0
t0 := time.Now()
for {
file, ok := <-fileChan
if ok {
topOfPath := topOfPath(targetPath, file.Path)
tl := fileMap[topOfPath]
if tl == nil {
tl = &topLevel{
Name: topOfPath,
Bytes: file.Size,
NumFiles: 1,
}
} else {
tl.Bytes += file.Size
tl.NumFiles++
}
fileMap[topOfPath] = tl
}
t1 := time.Now()
if !ok || t1.Sub(t0) > 100*time.Millisecond {
cursor.Up(prevHeight)
prevHeight = showTable(makeTable(sortedTopLevels(fileMap), 20))
t0 = t1
}
if !ok {
break
}
}
fmt.Println()
}
func dirsInDir(dir string) ([]fs.FileInfo, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("can't read dir: " + dir)
}
ret := []fs.FileInfo{}
for _, file := range files {
if file.IsDir() {
ret = append(ret, file)
}
}
return ret, nil
}
// sortedTopLevels returns a slice of topLevels sorted by name.
func sortedTopLevels(fileMap map[string]*topLevel) []*topLevel {
// sort by total number of bytes in top level directory:
var topLevels []*topLevel
for _, tl := range fileMap {
topLevels = append(topLevels, tl)
}
sort.Slice(topLevels, func(i, j int) bool {
return topLevels[i].Bytes > topLevels[j].Bytes ||
topLevels[i].Bytes == topLevels[j].Bytes && topLevels[i].Name < topLevels[j].Name
})
return topLevels
}
func makeTable(topLevels []*topLevel, maxTopLevels int) [][]string {
// Fixme: Generalize, and move into `table.go`?
table := [][]string{}
i, bytes, files := 0, 0, 0
for _, tl := range topLevels {
if i < maxTopLevels {
table = append(table, []string{
fmt.Sprintf("%s B ", commafiedInt(int(tl.Bytes))),
fmt.Sprintf("%s: ", tl.Name),
fmt.Sprintf("%d files%30s", tl.NumFiles, ""),
})
}
i++
bytes += int(tl.Bytes)
files += int(tl.NumFiles)
}
table = append(table, []string{"", "", ""})
table = append(table, []string{
fmt.Sprintf("%s B ", commafiedInt(bytes)),
"TOTAL ",
fmt.Sprintf("%s files%30s", commafiedInt(files), ""),
})
return table
}
func collectDirStats(path string) {
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
// Skip read errors (generally, permissions) for now; typically,
// these are permissions issues; handle them later?
// Present statistics?
return nil
}
fileChan <- fileInfo{
Name: info.Name(),
Size: info.Size(),
Path: path,
}
return nil
})
}