-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
186 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package analyze | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/dundee/gdu/v5/pkg/fs" | ||
) | ||
|
||
// TopList is a list of top largest files | ||
type TopList struct { | ||
Count int | ||
Items fs.Files | ||
MinSize int64 | ||
} | ||
|
||
// NewTopList creates new TopList | ||
func NewTopList(count int) *TopList { | ||
return &TopList{Count: count} | ||
} | ||
|
||
// Add adds file to the list | ||
func (tl *TopList) Add(file fs.Item) { | ||
if len(tl.Items) < tl.Count { | ||
tl.Items = append(tl.Items, file) | ||
if file.GetSize() > tl.MinSize { | ||
tl.MinSize = file.GetSize() | ||
} | ||
} else { | ||
if file.GetSize() > tl.MinSize { | ||
tl.Items = append(tl.Items, file) | ||
tl.MinSize = file.GetSize() | ||
if len(tl.Items) > tl.Count { | ||
sort.Sort(fs.ByApparentSize(tl.Items)) | ||
tl.Items = tl.Items[1:] | ||
} | ||
} | ||
} | ||
} | ||
|
||
func CollectTopFiles(dir fs.Item, count int) fs.Files { | ||
topList := NewTopList(count) | ||
walkDir(dir, topList) | ||
sort.Sort(sort.Reverse(fs.ByApparentSize(topList.Items))) | ||
return topList.Items | ||
} | ||
|
||
func walkDir(dir fs.Item, topList *TopList) { | ||
for _, item := range dir.GetFiles() { | ||
if item.IsDir() { | ||
walkDir(item, topList) | ||
} else { | ||
topList.Add(item) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package analyze | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dundee/gdu/v5/internal/testdir" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCollectTopFiles2(t *testing.T) { | ||
fin := testdir.CreateTestDir() | ||
defer fin() | ||
|
||
dir := CreateAnalyzer().AnalyzeDir( | ||
"test_dir", func(_, _ string) bool { return false }, false, | ||
) | ||
|
||
topFiles := CollectTopFiles(dir, 2) | ||
assert.Equal(t, 2, len(topFiles)) | ||
assert.Equal(t, "file", topFiles[0].GetName()) | ||
assert.Equal(t, int64(5), topFiles[0].GetSize()) | ||
assert.Equal(t, "file2", topFiles[1].GetName()) | ||
assert.Equal(t, int64(2), topFiles[1].GetSize()) | ||
} | ||
|
||
func TestCollectTopFiles1(t *testing.T) { | ||
fin := testdir.CreateTestDir() | ||
defer fin() | ||
|
||
dir := CreateAnalyzer().AnalyzeDir( | ||
"test_dir", func(_, _ string) bool { return false }, false, | ||
) | ||
|
||
topFiles := CollectTopFiles(dir, 1) | ||
assert.Equal(t, 1, len(topFiles)) | ||
assert.Equal(t, "file", topFiles[0].GetName()) | ||
assert.Equal(t, int64(5), topFiles[0].GetSize()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.