fork/gdu

git clone https://git.julia.blue/fork/gdu

Quet Zalfeat(analyze): add SQLite analyzer and some assorted changes (#484)da638da

ignore
993 B48 linesraw
1package analyze
2
3import (
4	"sort"
5
6	"github.com/dundee/gdu/v5/pkg/fs"
7)
8
9// TopList is a list of top largest files
10type TopList struct {
11	Items   fs.Files
12	Count   int
13	MinSize int64
14}
15
16// NewTopList creates new TopList
17func NewTopList(count int) *TopList {
18	return &TopList{Count: count}
19}
20
21// Add adds file to the list
22func (tl *TopList) Add(file fs.Item) {
23	if file.GetSize() > tl.MinSize || len(tl.Items) < tl.Count {
24		tl.Items = append(tl.Items, file)
25		sort.Sort(fs.ByApparentSize(tl.Items))
26		if len(tl.Items) > tl.Count {
27			tl.Items = tl.Items[1:]
28		}
29		tl.MinSize = tl.Items[0].GetSize()
30	}
31}
32
33func CollectTopFiles(dir fs.Item, count int) fs.Files {
34	topList := NewTopList(count)
35	walkDir(dir, topList)
36	sort.Sort(sort.Reverse(fs.ByApparentSize(topList.Items)))
37	return topList.Items
38}
39
40func walkDir(dir fs.Item, topList *TopList) {
41	for item := range dir.GetFiles(fs.SortBySize, fs.SortDesc) {
42		if item.IsDir() {
43			walkDir(item, topList)
44		} else {
45			topList.Add(item)
46		}
47	}
48}