sortMethod simplification. (#1535)

* made sortMethod a string enum to reduce comparisons and repetition

* main uses sortmethod directly instead of converting it into a string

* updated sortmethod validity check to no longer require slices package

* Update opts.go

Co-authored-by: Joe Lim <50560759+joelim-work@users.noreply.github.com>

---------

Co-authored-by: Joe Lim <50560759+joelim-work@users.noreply.github.com>
This commit is contained in:
Michael GalloandJoe Lim authored and GitHub committed 2023-12-26 15:55:42 +01:00
1 parent f13325f78d
commit e1ab72516d
3 files changed
+31 -65

No files matched your search

+8 -34
View File
@@ -810,25 +810,12 @@ func (e *setExpr) eval(app *app, args []string) {
}
gOpts.smartdia = !gOpts.smartdia
case "sortby":
switch e.val {
case "natural":
gOpts.sortType.method = naturalSort
case "name":
gOpts.sortType.method = nameSort
case "size":
gOpts.sortType.method = sizeSort
case "time":
gOpts.sortType.method = timeSort
case "ctime":
gOpts.sortType.method = ctimeSort
case "atime":
gOpts.sortType.method = atimeSort
case "ext":
gOpts.sortType.method = extSort
default:
app.ui.echoerr("sortby: value should either be 'natural', 'name', 'size', 'time', 'atime', 'ctime' or 'ext'")
method := sortMethod(e.val)
if !isValidSortMethod(method) {
app.ui.echoerr(invalidSortErrorMessage)
return
}
gOpts.sortType.method = method
app.nav.sort()
app.ui.sort()
case "statfmt":
@@ -1104,25 +1091,12 @@ func (e *setLocalExpr) eval(app *app, args []string) {
app.nav.sort()
app.ui.sort()
case "sortby":
switch e.val {
case "natural":
gLocalOpts.sortMethods[path] = naturalSort
case "name":
gLocalOpts.sortMethods[path] = nameSort
case "size":
gLocalOpts.sortMethods[path] = sizeSort
case "time":
gLocalOpts.sortMethods[path] = timeSort
case "ctime":
gLocalOpts.sortMethods[path] = ctimeSort
case "atime":
gLocalOpts.sortMethods[path] = atimeSort
case "ext":
gLocalOpts.sortMethods[path] = extSort
default:
app.ui.echoerr("sortby: value should either be 'natural', 'name', 'size', 'time', 'atime', 'ctime' or 'ext'")
method := sortMethod(e.val)
if !isValidSortMethod(method) {
app.ui.echoerr(invalidSortErrorMessage)
return
}
gLocalOpts.sortMethods[path] = method
app.nav.sort()
app.ui.sort()
default: