diff --git a/doc.go b/doc.go index afe817f..a85de96 100644 --- a/doc.go +++ b/doc.go @@ -54,7 +54,7 @@ The following options can be used to customize the behavior of lf. previewer string (default "") (not filtered if empty) shell string (default "$SHELL") showinfo string (default "none") - sortby string (default "name") + sortby string (default "natural") timefmt string (default "Mon Jan _2 15:04:05 2006") ratios string (default "1:2:3") diff --git a/docstring.go b/docstring.go index 9cf8c49..15abf9b 100644 --- a/docstring.go +++ b/docstring.go @@ -58,7 +58,7 @@ The following options can be used to customize the behavior of lf. previewer string (default "") (not filtered if empty) shell string (default "$SHELL") showinfo string (default "none") - sortby string (default "name") + sortby string (default "natural") timefmt string (default "Mon Jan _2 15:04:05 2006") ratios string (default "1:2:3") diff --git a/eval.go b/eval.go index 599fd6f..b389eed 100644 --- a/eval.go +++ b/eval.go @@ -84,8 +84,8 @@ func (e *SetExpr) eval(app *App, args []string) { } gOpts.showinfo = e.val case "sortby": - if e.val != "name" && e.val != "size" && e.val != "time" { - msg := "sortby should either be 'name', 'size' or 'time'" + if e.val != "natural" && e.val != "name" && e.val != "size" && e.val != "time" { + msg := "sortby should either be 'natural', 'name', 'size' or 'time'" app.ui.message = msg log.Print(msg) return diff --git a/nav.go b/nav.go index bcaaed7..3de8ab1 100644 --- a/nav.go +++ b/nav.go @@ -46,6 +46,34 @@ func getFilesSorted(path string) []*File { } switch gOpts.sortby { + case "natural": + sortFilesStable(fi, func(i, j int) bool { + nums1, rest1, numFirst1 := extractNums(fi[i].Name()) + nums2, rest2, numFirst2 := extractNums(fi[j].Name()) + + if numFirst1 != numFirst2 { + return strings.ToLower(fi[i].Name()) < strings.ToLower(fi[j].Name()) + } + + if numFirst1 { + if nums1[0] != nums2[0] { + return nums1[0] < nums2[0] + } + nums1 = nums1[1:] + nums2 = nums2[1:] + } + + for k := 0; k < len(nums1) && k < len(nums2); k++ { + if rest1[k] != rest2[k] { + return strings.ToLower(fi[i].Name()) < strings.ToLower(fi[j].Name()) + } + if nums1[k] != nums2[k] { + return nums1[k] < nums2[k] + } + } + + return strings.ToLower(fi[i].Name()) < strings.ToLower(fi[j].Name()) + }) case "name": sortFilesStable(fi, func(i, j int) bool { return strings.ToLower(fi[i].Name()) < strings.ToLower(fi[j].Name()) @@ -70,34 +98,6 @@ func getFilesSorted(path string) []*File { return fi[i].IsDir() }) } - //TODO this should be optional - sortFilesStable(fi, func(i, j int) bool { - nums1, rest1, numFirst1 := extractNums(fi[i].Name()) - nums2, rest2, numFirst2 := extractNums(fi[j].Name()) - - if numFirst1 != numFirst2 { - return i < j - } - - if numFirst1 { - if nums1[0] != nums2[0] { - return nums1[0] < nums2[0] - } - nums1 = nums1[1:] - nums2 = nums2[1:] - } - - for k := 0; k < len(nums1) && k < len(nums2); k++ { - if rest1[k] != rest2[k] { - return i < j - } - if nums1[k] != nums2[k] { - return nums1[k] < nums2[k] - } - } - - return i < j - }) return fi } diff --git a/opts.go b/opts.go index 2fc0f0f..0328c7d 100644 --- a/opts.go +++ b/opts.go @@ -27,7 +27,7 @@ func init() { gOpts.tabstop = 8 gOpts.shell = envShell gOpts.showinfo = "none" - gOpts.sortby = "name" + gOpts.sortby = "natural" gOpts.timefmt = time.ANSIC gOpts.ratios = []int{1, 2, 3}