diff --git a/doc.md b/doc.md index fb3e4cc7..b9cbeecc 100644 --- a/doc.md +++ b/doc.md @@ -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`) diff --git a/doc.txt b/doc.txt index e839fd17..441d9180 100644 --- a/doc.txt +++ b/doc.txt @@ -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) diff --git a/eval.go b/eval.go index 487301c3..d113e1ee 100644 --- a/eval.go +++ b/eval.go @@ -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] diff --git a/lf.1 b/lf.1 index 7373abf6..d339eee9 100644 --- a/lf.1 +++ b/lf.1 @@ -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, diff --git a/nav.go b/nav.go index 861b83ec..9e9e0f59 100644 --- a/nav.go +++ b/nav.go @@ -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 { diff --git a/opts.go b/opts.go index 32b36b19..287c130f 100644 --- a/opts.go +++ b/opts.go @@ -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