Skip to content

Commit

Permalink
feat: sort items by name if usage/size/count is the same
Browse files Browse the repository at this point in the history
fixes #143
  • Loading branch information
dundee committed May 25, 2022
1 parent 9b346d4 commit 5543a9e
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 31 deletions.
23 changes: 23 additions & 0 deletions pkg/analyze/sort_test.go
Expand Up @@ -29,6 +29,29 @@ func TestSortByUsage(t *testing.T) {
assert.Equal(t, int64(1), files[2].GetUsage())
}

func TestStableSortByUsage(t *testing.T) {
files := fs.Files{
&File{
Name: "aaa",
Usage: 1,
},
&File{
Name: "bbb",
Usage: 1,
},
&File{
Name: "ccc",
Usage: 3,
},
}

sort.Sort(files)

assert.Equal(t, "ccc", files[0].GetName())
assert.Equal(t, "bbb", files[1].GetName())
assert.Equal(t, "aaa", files[2].GetName())
}

func TestSortByUsageAsc(t *testing.T) {
files := fs.Files{
&File{
Expand Down
48 changes: 36 additions & 12 deletions pkg/fs/file.go
Expand Up @@ -71,23 +71,41 @@ func (f Files) RemoveByName(name string) Files {
return append(f[:index], f[index+1:]...)
}

func (f Files) Len() int { return len(f) }
func (f Files) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f Files) Less(i, j int) bool { return f[i].GetUsage() > f[j].GetUsage() }
func (f Files) Len() int { return len(f) }
func (f Files) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f Files) Less(i, j int) bool {
if f[i].GetUsage() != f[j].GetUsage() {
return f[i].GetUsage() > f[j].GetUsage()
}
// if usage is the same, sort by name
return f[i].GetName() > f[j].GetName()
}

// ByApparentSize sorts files by apparent size
type ByApparentSize Files

func (f ByApparentSize) Len() int { return len(f) }
func (f ByApparentSize) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f ByApparentSize) Less(i, j int) bool { return f[i].GetSize() > f[j].GetSize() }
func (f ByApparentSize) Len() int { return len(f) }
func (f ByApparentSize) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f ByApparentSize) Less(i, j int) bool {
if f[i].GetSize() != f[j].GetSize() {
return f[i].GetSize() > f[j].GetSize()
}
// if size is the same, sort by name
return f[i].GetName() > f[j].GetName()
}

// ByItemCount sorts files by item count
type ByItemCount Files

func (f ByItemCount) Len() int { return len(f) }
func (f ByItemCount) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f ByItemCount) Less(i, j int) bool { return f[i].GetItemCount() > f[j].GetItemCount() }
func (f ByItemCount) Len() int { return len(f) }
func (f ByItemCount) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f ByItemCount) Less(i, j int) bool {
if f[i].GetItemCount() != f[j].GetItemCount() {
return f[i].GetItemCount() > f[j].GetItemCount()
}
// if item count is the same, sort by name
return f[i].GetName() > f[j].GetName()
}

// ByName sorts files by name
type ByName Files
Expand All @@ -99,6 +117,12 @@ func (f ByName) Less(i, j int) bool { return f[i].GetName() > f[j].GetName() }
// ByMtime sorts files by name
type ByMtime Files

func (f ByMtime) Len() int { return len(f) }
func (f ByMtime) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f ByMtime) Less(i, j int) bool { return f[i].GetMtime().After(f[j].GetMtime()) }
func (f ByMtime) Len() int { return len(f) }
func (f ByMtime) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f ByMtime) Less(i, j int) bool {
if !f[i].GetMtime().Equal(f[j].GetMtime()) {
return f[i].GetMtime().After(f[j].GetMtime())
}
// if item count is the same, sort by name
return f[i].GetName() > f[j].GetName()
}
8 changes: 4 additions & 4 deletions tui/actions_test.go
Expand Up @@ -79,22 +79,22 @@ func TestDeviceSelected(t *testing.T) {
assert.Equal(t, "test_dir", ui.currentDir.GetName())

assert.Equal(t, 4, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "aaa")
assert.Contains(t, ui.table.GetCell(0, 0).Text, "ccc")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "bbb")
}

func TestAnalyzePath(t *testing.T) {
ui := getAnalyzedPathMockedApp(t, true, true, true)

assert.Equal(t, 4, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "aaa")
assert.Contains(t, ui.table.GetCell(0, 0).Text, "ccc")
}

func TestAnalyzePathBW(t *testing.T) {
ui := getAnalyzedPathMockedApp(t, false, true, true)

assert.Equal(t, 4, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "aaa")
assert.Contains(t, ui.table.GetCell(0, 0).Text, "ccc")
}

func TestAnalyzePathWithParentDir(t *testing.T) {
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestAnalyzePathWithParentDir(t *testing.T) {

assert.Equal(t, 5, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "/..")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "aaa")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "ccc")
}

func TestReadAnalysis(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions tui/filter_test.go
Expand Up @@ -33,17 +33,17 @@ func TestFiltering(t *testing.T) {
ui.filterValue = ""
ui.showDir()

assert.Contains(t, ui.table.GetCell(0, 0).Text, "aaa") // nothing is filtered
assert.Contains(t, ui.table.GetCell(0, 0).Text, "ccc") // nothing is filtered

ui.filterValue = "cc"
ui.filterValue = "aa"
ui.showDir()

assert.Contains(t, ui.table.GetCell(0, 0).Text, "ccc") // shows only cccc
assert.Contains(t, ui.table.GetCell(0, 0).Text, "aaa") // shows only cccc

ui.hideFilterInput()
ui.showDir()

assert.Contains(t, ui.table.GetCell(0, 0).Text, "aaa") // filtering reset
assert.Contains(t, ui.table.GetCell(0, 0).Text, "ccc") // filtering reset
}

func TestFilteringWithoutCurrentDir(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion tui/keys_test.go
Expand Up @@ -670,7 +670,7 @@ func TestRescan(t *testing.T) {

assert.Equal(t, 5, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "/..")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "aaa")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "ccc")
}

func TestSorting(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions tui/sort_test.go
Expand Up @@ -13,9 +13,9 @@ func TestAnalyzeByApparentSize(t *testing.T) {
ui := getAnalyzedPathWithSorting("size", "desc", true)

assert.Equal(t, 4, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "aaa")
assert.Contains(t, ui.table.GetCell(0, 0).Text, "ccc")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "bbb")
assert.Contains(t, ui.table.GetCell(2, 0).Text, "ccc")
assert.Contains(t, ui.table.GetCell(2, 0).Text, "aaa")
assert.Contains(t, ui.table.GetCell(3, 0).Text, "ddd")
}

Expand All @@ -33,9 +33,9 @@ func TestAnalyzeBySize(t *testing.T) {
ui := getAnalyzedPathWithSorting("size", "desc", false)

assert.Equal(t, 4, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "aaa")
assert.Contains(t, ui.table.GetCell(0, 0).Text, "ccc")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "bbb")
assert.Contains(t, ui.table.GetCell(2, 0).Text, "ccc")
assert.Contains(t, ui.table.GetCell(2, 0).Text, "aaa")
assert.Contains(t, ui.table.GetCell(3, 0).Text, "ddd")
}

Expand Down Expand Up @@ -73,10 +73,10 @@ func TestAnalyzeByItemCount(t *testing.T) {
ui := getAnalyzedPathWithSorting("itemCount", "desc", false)

assert.Equal(t, 4, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "aaa")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "bbb")
assert.Contains(t, ui.table.GetCell(2, 0).Text, "ccc")
assert.Contains(t, ui.table.GetCell(3, 0).Text, "ddd")
assert.Contains(t, ui.table.GetCell(0, 0).Text, "ddd")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "ccc")
assert.Contains(t, ui.table.GetCell(2, 0).Text, "bbb")
assert.Contains(t, ui.table.GetCell(3, 0).Text, "aaa")
}

func TestAnalyzeByItemCountAsc(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions tui/tui_test.go
Expand Up @@ -179,7 +179,7 @@ func TestRescanDir(t *testing.T) {

assert.Equal(t, 5, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "/..")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "aaa")
assert.Contains(t, ui.table.GetCell(1, 0).Text, "ccc")
}

func TestDirSelected(t *testing.T) {
Expand All @@ -202,7 +202,7 @@ func TestFileSelected(t *testing.T) {
ui.fileItemSelected(3, 0)

assert.Equal(t, 4, ui.table.GetRowCount())
assert.Contains(t, ui.table.GetCell(0, 0).Text, "aaa")
assert.Contains(t, ui.table.GetCell(0, 0).Text, "ccc")
}

func TestSelectedWithoutCurrentDir(t *testing.T) {
Expand Down

0 comments on commit 5543a9e

Please sign in to comment.