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 gOpts.smartdia = !gOpts.smartdia
case "sortby": case "sortby":
switch e.val { method := sortMethod(e.val)
case "natural": if !isValidSortMethod(method) {
gOpts.sortType.method = naturalSort app.ui.echoerr(invalidSortErrorMessage)
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'")
return return
} }
gOpts.sortType.method = method
app.nav.sort() app.nav.sort()
app.ui.sort() app.ui.sort()
case "statfmt": case "statfmt":
@@ -1104,25 +1091,12 @@ func (e *setLocalExpr) eval(app *app, args []string) {
app.nav.sort() app.nav.sort()
app.ui.sort() app.ui.sort()
case "sortby": case "sortby":
switch e.val { method := sortMethod(e.val)
case "natural": if !isValidSortMethod(method) {
gLocalOpts.sortMethods[path] = naturalSort app.ui.echoerr(invalidSortErrorMessage)
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'")
return return
} }
gLocalOpts.sortMethods[path] = method
app.nav.sort() app.nav.sort()
app.ui.sort() app.ui.sort()
default: default:
+3 -23
View File
@@ -13,9 +13,9 @@ import (
"runtime/pprof" "runtime/pprof"
"strconv" "strconv"
"strings" "strings"
)
import _ "embed" _ "embed"
)
//go:embed doc.txt //go:embed doc.txt
var genDocString string var genDocString string
@@ -137,28 +137,8 @@ func getOptsMap() map[string]string {
continue continue
} }
// Get string representation of the value
if name == "lf_sortType" { if name == "lf_sortType" {
var sortby string opts["lf_sortby"] = string(gOpts.sortType.method)
switch gOpts.sortType.method {
case naturalSort:
sortby = "natural"
case nameSort:
sortby = "name"
case sizeSort:
sortby = "size"
case timeSort:
sortby = "time"
case ctimeSort:
sortby = "ctime"
case atimeSort:
sortby = "atime"
case extSort:
sortby = "ext"
}
opts["lf_sortby"] = sortby
opts["lf_reverse"] = strconv.FormatBool(gOpts.sortType.option&reverseSort != 0) opts["lf_reverse"] = strconv.FormatBool(gOpts.sortType.option&reverseSort != 0)
opts["lf_hidden"] = strconv.FormatBool(gOpts.sortType.option&hiddenSort != 0) opts["lf_hidden"] = strconv.FormatBool(gOpts.sortType.option&hiddenSort != 0)
opts["lf_dirfirst"] = strconv.FormatBool(gOpts.sortType.option&dirfirstSort != 0) opts["lf_dirfirst"] = strconv.FormatBool(gOpts.sortType.option&dirfirstSort != 0)
+20 -8
View File
@@ -5,18 +5,30 @@ import (
"time" "time"
) )
type sortMethod byte // String values match the sortby string sent by the user at startup
type sortMethod string
const ( const (
naturalSort sortMethod = iota naturalSort sortMethod = "natural"
nameSort nameSort sortMethod = "name"
sizeSort sizeSort sortMethod = "size"
timeSort timeSort sortMethod = "time"
atimeSort atimeSort sortMethod = "atime"
ctimeSort ctimeSort sortMethod = "ctime"
extSort extSort sortMethod = "ext"
) )
func isValidSortMethod(method sortMethod) bool {
for _, validMethod := range []sortMethod{naturalSort, nameSort, sizeSort, timeSort, atimeSort, ctimeSort, extSort} {
if method == validMethod {
return true
}
}
return false
}
const invalidSortErrorMessage = `sortby: value should either be 'natural', 'name', 'size', 'time', 'atime', 'ctime' or 'ext'`
type sortOption byte type sortOption byte
const ( const (