diff --git a/doc.md b/doc.md index 61ddd74e..a050bb13 100644 --- a/doc.md +++ b/doc.md @@ -759,6 +759,7 @@ This option only has an effect when `info` has a `size` field and the pane is wi ## dirfirst (bool) (default true) Show directories first above regular files. +With `dircounts` enabled, sorting by `size` always separates directories and files, regardless of `dirfirst`. ## dironly (bool) (default false) diff --git a/doc.txt b/doc.txt index 43177061..8f43efcf 100644 --- a/doc.txt +++ b/doc.txt @@ -800,7 +800,9 @@ directory at most, and bigger directories are shown as 999+. dirfirst (bool) (default true) -Show directories first above regular files. +Show directories first above regular files. With dircounts enabled, +sorting by size always separates directories and files, regardless of +dirfirst. dironly (bool) (default false) diff --git a/lf.1 b/lf.1 index b5c5a5a3..52a7b6d3 100644 --- a/lf.1 +++ b/lf.1 @@ -707,6 +707,8 @@ information. shown as \f[CR]999+\f[R]. .SS dirfirst (bool) (default true) Show directories first above regular files. +With \f[CR]dircounts\f[R] enabled, sorting by \f[CR]size\f[R] always +separates directories and files, regardless of \f[CR]dirfirst\f[R]. .SS dironly (bool) (default false) Show only directories. .SS dirpreviews (bool) (default false) diff --git a/nav.go b/nav.go index f5b3c8f9..3dc582e4 100644 --- a/nav.go +++ b/nav.go @@ -167,6 +167,7 @@ type dir struct { files []*file // displayed files in directory including or excluding hidden ones allFiles []*file // all files in directory including hidden ones (same array as files) sortby sortMethod // sortby value from last sort + dircounts bool // dircounts value from last sort dirfirst bool // dirfirst value from last sort dironly bool // dironly value from last sort hidden bool // hidden value from last sort @@ -212,6 +213,7 @@ func normalize(s1, s2 string, ignorecase, ignoredia bool) (string, string) { func (dir *dir) sort() { dir.sortby = getSortBy(dir.path) + dir.dircounts = getDirCounts(dir.path) dir.dirfirst = getDirFirst(dir.path) dir.dironly = getDirOnly(dir.path) dir.hidden = getHidden(dir.path) @@ -290,11 +292,18 @@ func (dir *dir) sort() { }) } case sizeSort: + sizeVal := func(f *file) int64 { + if f.IsDir() && dir.dircounts { + return int64(f.dirCount) + } + return f.TotalSize() + } sort.SliceStable(dir.files, func(i, j int) bool { + s1, s2 := sizeVal(dir.files[i]), sizeVal(dir.files[j]) if !dir.reverse { - return dir.files[i].TotalSize() < dir.files[j].TotalSize() + return s1 < s2 } else { - return dir.files[j].TotalSize() < dir.files[i].TotalSize() + return s2 < s1 } }) case timeSort: @@ -333,7 +342,9 @@ func (dir *dir) sort() { }) } - if dir.dirfirst { + // when sorting by size while also showing dircounts, we always display files + // and directories separately to avoid mixing file sizes and file counts + if dir.dirfirst || (dir.sortby == sizeSort && dir.dircounts) { sort.SliceStable(dir.files, func(i, j int) bool { if dir.files[i].IsDir() == dir.files[j].IsDir() { return i < j @@ -538,6 +549,7 @@ func (nav *nav) loadDirInternal(path string) *dir { loadTime: time.Now(), path: path, sortby: getSortBy(path), + dircounts: getDirCounts(path), dirfirst: getDirFirst(path), dironly: getDirOnly(path), hidden: getHidden(path), @@ -597,6 +609,7 @@ func (nav *nav) checkDir(dir *dir) { nav.dirChan <- nd }() case dir.sortby != getSortBy(dir.path) || + dir.dircounts != getDirCounts(dir.path) || dir.dirfirst != getDirFirst(dir.path) || dir.dironly != getDirOnly(dir.path) || dir.hidden != getHidden(dir.path) ||