Refactor dir.sort function (#2115)

* Refactor filtering code in `dir.sort` function

* Refactor sorting code in `dir.sort` function

* Fix bug
This commit is contained in:
Joe Lim authored and GitHub committed 2025-08-26 14:16:54 +10:00
1 parent 4384e520b5
commit 04afd78e65
1 file changed
+64 -116
+64 -116
View File
@@ -12,6 +12,7 @@ import (
"path/filepath"
"reflect"
"regexp"
"slices"
"sort"
"strconv"
"strings"
@@ -231,35 +232,60 @@ func (dir *dir) sort() {
dir.files = dir.allFiles
// When applying a filter, move all files not satisfying the predicate to
// the beginning, then take the subslice starting from the first file that
// does satisfy the predicate
applyFilter := func(fn func(f *file) bool) {
slices.SortStableFunc(dir.files, func(i, j *file) int {
switch {
case fn(i) == fn(j):
return 0
case !fn(i) && fn(j):
return -1
default:
return 1
}
})
i := slices.IndexFunc(dir.files, fn)
if i == -1 {
i = len(dir.files)
}
dir.files = dir.files[i:]
}
if dir.dironly {
applyFilter(func(f *file) bool { return f.IsDir() })
}
if !dir.hidden {
applyFilter(func(f *file) bool { return !isHidden(f, dir.path, dir.hiddenfiles) })
}
if len(dir.filter) != 0 {
applyFilter(func(f *file) bool { return !isFiltered(f, dir.filter) })
}
// reverse order cannot be applied after stable sorting, otherwise the order
// of equivalent elements will be reversed
applySort := func(fn func(i, j int) bool) {
if !dir.reverse {
sort.SliceStable(dir.files, fn)
} else {
sort.SliceStable(dir.files, func(i, j int) bool { return fn(j, i) })
}
}
switch dir.sortby {
case naturalSort:
sort.SliceStable(dir.files, func(i, j int) bool {
applySort(func(i, j int) bool {
s1, s2 := normalize(dir.files[i].Name(), dir.files[j].Name(), dir.ignorecase, dir.ignoredia)
if !dir.reverse {
return naturalLess(s1, s2)
} else {
return naturalLess(s2, s1)
}
})
case customSort:
sort.SliceStable(dir.files, func(i, j int) bool {
s1, s2 := normalize(stripAnsi(dir.files[i].customInfo), stripAnsi(dir.files[j].customInfo), dir.ignorecase, dir.ignoredia)
if !dir.reverse {
return naturalLess(s1, s2)
} else {
return naturalLess(s2, s1)
}
return naturalLess(s1, s2)
})
case nameSort:
sort.SliceStable(dir.files, func(i, j int) bool {
applySort(func(i, j int) bool {
s1, s2 := normalize(dir.files[i].Name(), dir.files[j].Name(), dir.ignorecase, dir.ignoredia)
if !dir.reverse {
return s1 < s2
} else {
return s2 < s1
}
return s1 < s2
})
case sizeSort:
sizeVal := func(f *file) int64 {
@@ -268,55 +294,36 @@ func (dir *dir) sort() {
}
return f.TotalSize()
}
sort.SliceStable(dir.files, func(i, j int) bool {
applySort(func(i, j int) bool {
s1, s2 := sizeVal(dir.files[i]), sizeVal(dir.files[j])
if !dir.reverse {
return s1 < s2
} else {
return s2 < s1
}
return s1 < s2
})
case timeSort:
sort.SliceStable(dir.files, func(i, j int) bool {
if !dir.reverse {
return dir.files[i].ModTime().Before(dir.files[j].ModTime())
} else {
return dir.files[j].ModTime().Before(dir.files[i].ModTime())
}
applySort(func(i, j int) bool {
return dir.files[i].ModTime().Before(dir.files[j].ModTime())
})
case atimeSort:
sort.SliceStable(dir.files, func(i, j int) bool {
if !dir.reverse {
return dir.files[i].accessTime.Before(dir.files[j].accessTime)
} else {
return dir.files[j].accessTime.Before(dir.files[i].accessTime)
}
applySort(func(i, j int) bool {
return dir.files[i].accessTime.Before(dir.files[j].accessTime)
})
case btimeSort:
sort.SliceStable(dir.files, func(i, j int) bool {
if !dir.reverse {
return dir.files[i].birthTime.Before(dir.files[j].birthTime)
} else {
return dir.files[j].birthTime.Before(dir.files[i].birthTime)
}
applySort(func(i, j int) bool {
return dir.files[i].birthTime.Before(dir.files[j].birthTime)
})
case ctimeSort:
sort.SliceStable(dir.files, func(i, j int) bool {
if !dir.reverse {
return dir.files[i].changeTime.Before(dir.files[j].changeTime)
} else {
return dir.files[j].changeTime.Before(dir.files[i].changeTime)
}
applySort(func(i, j int) bool {
return dir.files[i].changeTime.Before(dir.files[j].changeTime)
})
case extSort:
sort.SliceStable(dir.files, func(i, j int) bool {
applySort(func(i, j int) bool {
ext1, ext2 := normalize(dir.files[i].ext, dir.files[j].ext, dir.ignorecase, dir.ignoredia)
name1, name2 := normalize(dir.files[i].Name(), dir.files[j].Name(), dir.ignorecase, dir.ignoredia)
if !dir.reverse {
return ext1 < ext2 || ext1 == ext2 && name1 < name2
} else {
return ext2 < ext1 || ext2 == ext1 && name2 < name1
}
return ext1 < ext2 || ext1 == ext2 && name1 < name2
})
case customSort:
applySort(func(i, j int) bool {
s1, s2 := normalize(stripAnsi(dir.files[i].customInfo), stripAnsi(dir.files[j].customInfo), dir.ignorecase, dir.ignoredia)
return naturalLess(s1, s2)
})
}
@@ -331,65 +338,6 @@ func (dir *dir) sort() {
})
}
// when dironly option is enabled, we move files to the beginning of our file
// list and then set the beginning of displayed files to the first directory
// in the list
if dir.dironly {
sort.SliceStable(dir.files, func(i, j int) bool {
if !dir.files[i].IsDir() && !dir.files[j].IsDir() {
return i < j
}
return !dir.files[i].IsDir()
})
dir.files = func() []*file {
for i, f := range dir.files {
if f.IsDir() {
return dir.files[i:]
}
}
return dir.files[len(dir.files):]
}()
}
// when hidden option is disabled, we move hidden files to the
// beginning of our file list and then set the beginning of displayed
// files to the first non-hidden file in the list
if !dir.hidden {
sort.SliceStable(dir.files, func(i, j int) bool {
if isHidden(dir.files[i], dir.path, dir.hiddenfiles) && isHidden(dir.files[j], dir.path, dir.hiddenfiles) {
return i < j
}
return isHidden(dir.files[i], dir.path, dir.hiddenfiles)
})
for i, f := range dir.files {
if !isHidden(f, dir.path, dir.hiddenfiles) {
dir.files = dir.files[i:]
break
}
}
if len(dir.files) > 0 && isHidden(dir.files[len(dir.files)-1], dir.path, dir.hiddenfiles) {
dir.files = dir.files[len(dir.files):]
}
}
if len(dir.filter) != 0 {
sort.SliceStable(dir.files, func(i, j int) bool {
if isFiltered(dir.files[i], dir.filter) && isFiltered(dir.files[j], dir.filter) {
return i < j
}
return isFiltered(dir.files[i], dir.filter)
})
for i, f := range dir.files {
if !isFiltered(f, dir.filter) {
dir.files = dir.files[i:]
break
}
}
if len(dir.files) > 0 && isFiltered(dir.files[len(dir.files)-1], dir.filter) {
dir.files = dir.files[len(dir.files):]
}
}
dir.ind = max(dir.ind, 0)
dir.ind = min(dir.ind, len(dir.files)-1)
}