Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sortMethod simplification. #1535

Merged
merged 4 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 8 additions & 34 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,25 +789,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":
Expand Down Expand Up @@ -1083,25 +1070,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:
Expand Down
26 changes: 3 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"runtime/pprof"
"strconv"
"strings"
)

import _ "embed"
_ "embed"
)

//go:embed doc.txt
var genDocString string
Expand Down Expand Up @@ -136,28 +136,8 @@ func getOptsMap() map[string]string {
continue
}

// Get string representation of the value
if name == "lf_sortType" {
var sortby string

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_sortby"] = string(gOpts.sortType.method)
opts["lf_reverse"] = strconv.FormatBool(gOpts.sortType.option&reverseSort != 0)
opts["lf_hidden"] = strconv.FormatBool(gOpts.sortType.option&hiddenSort != 0)
opts["lf_dirfirst"] = strconv.FormatBool(gOpts.sortType.option&dirfirstSort != 0)
Expand Down
28 changes: 20 additions & 8 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@ import (
"time"
)

type sortMethod byte
// String values match the sortby string sent by the user at startup
type sortMethod string

const (
naturalSort sortMethod = iota
nameSort
sizeSort
timeSort
atimeSort
ctimeSort
extSort
naturalSort sortMethod = "natural"
nameSort sortMethod = "name"
sizeSort sortMethod = "size"
timeSort sortMethod = "time"
atimeSort sortMethod = "atime"
ctimeSort sortMethod = "ctime"
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

const (
Expand Down