Add sorting by custom column (#2019)

This commit is contained in:
CatsDeservePets authored and GitHub committed 2025-06-26 11:12:58 +10:00
1 parent 017951fd6b
commit e47600e428
6 files changed
+35 -7

No files matched your search

+1 -1
View File
@@ -974,7 +974,7 @@ This option has no effect when `ignoredia` is disabled.
## sortby (string) (default `natural`)
Sort type for directories.
Currently supported sort types are `natural`, `name`, `size`, `time`, `ctime`, `atime`, and `ext`.
Currently supported sort types are `natural`, `name`, `size`, `time`, `ctime`, `atime`, `ext` and `custom`.
## statfmt (string) (default `\033[36m%p\033[0m| %c| %u| %g| %S| %t| -> %l`)
+1 -1
View File
@@ -1061,7 +1061,7 @@ diacritic. This option has no effect when ignoredia is disabled.
sortby (string) (default natural)
Sort type for directories. Currently supported sort types are natural,
name, size, time, ctime, atime, and ext.
name, size, time, ctime, atime, ext and custom.
statfmt (string) (default \033[36m%p\033[0m| %c| %u| %g| %S| %t| -> %l)
+7 -2
View File
@@ -2288,9 +2288,14 @@ func (e *callExpr) eval(app *app, args []string) {
}
if len(strings.Trim(v, " ")) == 0 {
f.customInfo = ""
} else {
v = ""
}
if f.customInfo != v {
f.customInfo = v
// only sort when order changes
if getSortBy(dir) == customSort {
d.sort()
}
}
default:
cmd, ok := gOpts.cmds[e.name]
+1 -1
View File
@@ -928,7 +928,7 @@ This option has no effect when \f[CR]ignoredia\f[R] is disabled.
Sort type for directories.
Currently supported sort types are \f[CR]natural\f[R], \f[CR]name\f[R],
\f[CR]size\f[R], \f[CR]time\f[R], \f[CR]ctime\f[R], \f[CR]atime\f[R],
and \f[CR]ext\f[R].
\f[CR]ext\f[R] and \f[CR]custom\f[R].
.SS statfmt (string) (default \f[CR]\(rs033[36m%p\(rs033[0m| %c| %u| %g| %S| %t| \-> %l\f[R])
Format string of the file info shown in the bottom left corner.
Special expansions are provided, \f[CR]%p\f[R] as the file permissions,
+21
View File
@@ -245,6 +245,27 @@ func (dir *dir) sort() {
}
})
}
case customSort:
if collator, err := makeCollator(dir.locale, collate.Numeric); err == nil {
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)
result := collator.CompareString(s1, s2)
if !dir.reverse {
return result < 0
} else {
return result > 0
}
})
} else {
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)
}
})
}
case nameSort:
if collator, err := makeCollator(dir.locale); err == nil {
sort.SliceStable(dir.files, func(i, j int) bool {
+4 -2
View File
@@ -16,6 +16,7 @@ const (
atimeSort sortMethod = "atime"
ctimeSort sortMethod = "ctime"
extSort sortMethod = "ext"
customSort sortMethod = "custom"
)
func isValidSortMethod(method sortMethod) bool {
@@ -25,10 +26,11 @@ func isValidSortMethod(method sortMethod) bool {
method == timeSort ||
method == atimeSort ||
method == ctimeSort ||
method == extSort
method == extSort ||
method == customSort
}
const invalidSortErrorMessage = `sortby: value should either be 'natural', 'name', 'size', 'time', 'atime', 'ctime' or 'ext'`
const invalidSortErrorMessage = `sortby: value should either be 'natural', 'name', 'size', 'time', 'atime', 'ctime', 'ext' or 'custom'`
var gOpts struct {
anchorfind bool