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

Small optimizations for opts.go #1831

Merged
merged 2 commits into from
Nov 3, 2024
Merged
Changes from 1 commit
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
268 changes: 121 additions & 147 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ const (
)

func isValidSortMethod(method sortMethod) bool {
for _, validMethod := range []sortMethod{naturalSort, nameSort, sizeSort, timeSort, atimeSort, ctimeSort, extSort} {
if method == validMethod {
return true
}
}
return false
return method == naturalSort ||
method == nameSort ||
method == sizeSort ||
method == timeSort ||
method == atimeSort ||
method == ctimeSort ||
method == extSort
}

const invalidSortErrorMessage = `sortby: value should either be 'natural', 'name', 'size', 'time', 'atime', 'ctime' or 'ext'`
Expand Down Expand Up @@ -116,8 +117,7 @@ var gLocalOpts struct {
}

func localOptPaths(path string) []string {
var list []string
list = append(list, path)
list := []string{path}
for curr := path; !isRoot(curr); curr = filepath.Dir(curr) {
list = append(list, curr+string(filepath.Separator))
}
Expand Down Expand Up @@ -188,55 +188,31 @@ func getLocale(path string) string {
}

func init() {
// Values missing from here relies on their Go default values
Limero marked this conversation as resolved.
Show resolved Hide resolved
gOpts.anchorfind = true
gOpts.autoquit = false
gOpts.dircache = true
gOpts.dircounts = false
gOpts.dirfirst = true
gOpts.dironly = false
gOpts.dirpreviews = false
gOpts.drawbox = false
gOpts.dupfilefmt = "%f.~%n~"
gOpts.borderfmt = "\033[0m"
gOpts.copyfmt = "\033[7;33m"
gOpts.cursoractivefmt = "\033[7m"
gOpts.cursorparentfmt = "\033[7m"
gOpts.cursorpreviewfmt = "\033[4m"
gOpts.cutfmt = "\033[7;31m"
gOpts.globfilter = false
gOpts.globsearch = false
gOpts.hidden = false
gOpts.icons = false
gOpts.ignorecase = true
gOpts.ignoredia = true
gOpts.incfilter = false
gOpts.incsearch = false
gOpts.locale = localeStrDisable
gOpts.mouse = false
gOpts.number = false
gOpts.preview = true
gOpts.relativenumber = false
gOpts.reverse = false
gOpts.roundbox = false
gOpts.selectfmt = "\033[7;35m"
gOpts.showbinds = true
gOpts.sixel = false
gOpts.sortby = naturalSort
gOpts.smartcase = true
gOpts.smartdia = false
gOpts.waitmsg = "Press any key to continue"
gOpts.watch = false
gOpts.wrapscan = true
gOpts.wrapscroll = false
gOpts.findlen = 1
gOpts.period = 0
gOpts.scrolloff = 0
gOpts.tabstop = 8
gOpts.errorfmt = "\033[7;31;47m"
gOpts.filesep = "\n"
gOpts.ifs = ""
gOpts.previewer = ""
gOpts.cleaner = ""
gOpts.promptfmt = "\033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m"
gOpts.selmode = "all"
gOpts.shell = gDefaultShell
Expand All @@ -250,128 +226,126 @@ func init() {
gOpts.ratios = []int{1, 2, 3}
gOpts.hiddenfiles = gDefaultHiddenFiles
gOpts.history = true
gOpts.info = nil
gOpts.rulerfmt = " %a| %p| \033[7;31m %m \033[0m| \033[7;33m %c \033[0m| \033[7;35m %s \033[0m| \033[7;34m %f \033[0m| %i/%t"
gOpts.preserve = []string{"mode"}
gOpts.shellopts = nil
gOpts.tempmarks = "'"
gOpts.numberfmt = "\033[33m"
gOpts.tagfmt = "\033[31m"

gOpts.keys = make(map[string]expr)

gOpts.keys["k"] = &callExpr{"up", nil, 1}
gOpts.keys["<up>"] = &callExpr{"up", nil, 1}
gOpts.keys["<m-up>"] = &callExpr{"up", nil, 1}
gOpts.keys["<c-u>"] = &callExpr{"half-up", nil, 1}
gOpts.keys["<c-b>"] = &callExpr{"page-up", nil, 1}
gOpts.keys["<pgup>"] = &callExpr{"page-up", nil, 1}
gOpts.keys["<c-y>"] = &callExpr{"scroll-up", nil, 1}
gOpts.keys["<c-m-up>"] = &callExpr{"scroll-up", nil, 1}
gOpts.keys["j"] = &callExpr{"down", nil, 1}
gOpts.keys["<down>"] = &callExpr{"down", nil, 1}
gOpts.keys["<m-down>"] = &callExpr{"down", nil, 1}
gOpts.keys["<c-d>"] = &callExpr{"half-down", nil, 1}
gOpts.keys["<c-f>"] = &callExpr{"page-down", nil, 1}
gOpts.keys["<pgdn>"] = &callExpr{"page-down", nil, 1}
gOpts.keys["<c-e>"] = &callExpr{"scroll-down", nil, 1}
gOpts.keys["<c-m-down>"] = &callExpr{"scroll-down", nil, 1}
gOpts.keys["h"] = &callExpr{"updir", nil, 1}
gOpts.keys["<left>"] = &callExpr{"updir", nil, 1}
gOpts.keys["l"] = &callExpr{"open", nil, 1}
gOpts.keys["<right>"] = &callExpr{"open", nil, 1}
gOpts.keys["q"] = &callExpr{"quit", nil, 1}
gOpts.keys["gg"] = &callExpr{"top", nil, 1}
gOpts.keys["<home>"] = &callExpr{"top", nil, 1}
gOpts.keys["G"] = &callExpr{"bottom", nil, 1}
gOpts.keys["<end>"] = &callExpr{"bottom", nil, 1}
gOpts.keys["H"] = &callExpr{"high", nil, 1}
gOpts.keys["M"] = &callExpr{"middle", nil, 1}
gOpts.keys["L"] = &callExpr{"low", nil, 1}
gOpts.keys["["] = &callExpr{"jump-prev", nil, 1}
gOpts.keys["]"] = &callExpr{"jump-next", nil, 1}
gOpts.keys["<space>"] = &listExpr{[]expr{&callExpr{"toggle", nil, 1}, &callExpr{"down", nil, 1}}, 1}
gOpts.keys["t"] = &callExpr{"tag-toggle", nil, 1}
gOpts.keys["v"] = &callExpr{"invert", nil, 1}
gOpts.keys["u"] = &callExpr{"unselect", nil, 1}
gOpts.keys["y"] = &callExpr{"copy", nil, 1}
gOpts.keys["d"] = &callExpr{"cut", nil, 1}
gOpts.keys["c"] = &callExpr{"clear", nil, 1}
gOpts.keys["p"] = &callExpr{"paste", nil, 1}
gOpts.keys["<c-l>"] = &callExpr{"redraw", nil, 1}
gOpts.keys["<c-r>"] = &callExpr{"reload", nil, 1}
gOpts.keys[":"] = &callExpr{"read", nil, 1}
gOpts.keys["$"] = &callExpr{"shell", nil, 1}
gOpts.keys["%"] = &callExpr{"shell-pipe", nil, 1}
gOpts.keys["!"] = &callExpr{"shell-wait", nil, 1}
gOpts.keys["&"] = &callExpr{"shell-async", nil, 1}
gOpts.keys["f"] = &callExpr{"find", nil, 1}
gOpts.keys["F"] = &callExpr{"find-back", nil, 1}
gOpts.keys[";"] = &callExpr{"find-next", nil, 1}
gOpts.keys[","] = &callExpr{"find-prev", nil, 1}
gOpts.keys["/"] = &callExpr{"search", nil, 1}
gOpts.keys["?"] = &callExpr{"search-back", nil, 1}
gOpts.keys["n"] = &callExpr{"search-next", nil, 1}
gOpts.keys["N"] = &callExpr{"search-prev", nil, 1}
gOpts.keys["m"] = &callExpr{"mark-save", nil, 1}
gOpts.keys["'"] = &callExpr{"mark-load", nil, 1}
gOpts.keys[`"`] = &callExpr{"mark-remove", nil, 1}
gOpts.keys[`r`] = &callExpr{"rename", nil, 1}
gOpts.keys["<c-n>"] = &callExpr{"cmd-history-next", nil, 1}
gOpts.keys["<c-p>"] = &callExpr{"cmd-history-prev", nil, 1}
gOpts.keys = map[string]expr{
"k": &callExpr{"up", nil, 1},
"<up>": &callExpr{"up", nil, 1},
"<m-up>": &callExpr{"up", nil, 1},
"<c-u>": &callExpr{"half-up", nil, 1},
"<c-b>": &callExpr{"page-up", nil, 1},
"<pgup>": &callExpr{"page-up", nil, 1},
"<c-y>": &callExpr{"scroll-up", nil, 1},
"<c-m-up>": &callExpr{"scroll-up", nil, 1},
"j": &callExpr{"down", nil, 1},
"<down>": &callExpr{"down", nil, 1},
"<m-down>": &callExpr{"down", nil, 1},
"<c-d>": &callExpr{"half-down", nil, 1},
"<c-f>": &callExpr{"page-down", nil, 1},
"<pgdn>": &callExpr{"page-down", nil, 1},
"<c-e>": &callExpr{"scroll-down", nil, 1},
"<c-m-down>": &callExpr{"scroll-down", nil, 1},
"h": &callExpr{"updir", nil, 1},
"<left>": &callExpr{"updir", nil, 1},
"l": &callExpr{"open", nil, 1},
"<right>": &callExpr{"open", nil, 1},
"q": &callExpr{"quit", nil, 1},
"gg": &callExpr{"top", nil, 1},
"<home>": &callExpr{"top", nil, 1},
"G": &callExpr{"bottom", nil, 1},
"<end>": &callExpr{"bottom", nil, 1},
"H": &callExpr{"high", nil, 1},
"M": &callExpr{"middle", nil, 1},
"L": &callExpr{"low", nil, 1},
"[": &callExpr{"jump-prev", nil, 1},
"]": &callExpr{"jump-next", nil, 1},
"<space>": &listExpr{[]expr{&callExpr{"toggle", nil, 1}, &callExpr{"down", nil, 1}}, 1},
"t": &callExpr{"tag-toggle", nil, 1},
"v": &callExpr{"invert", nil, 1},
"u": &callExpr{"unselect", nil, 1},
"y": &callExpr{"copy", nil, 1},
"d": &callExpr{"cut", nil, 1},
"c": &callExpr{"clear", nil, 1},
"p": &callExpr{"paste", nil, 1},
"<c-l>": &callExpr{"redraw", nil, 1},
"<c-r>": &callExpr{"reload", nil, 1},
":": &callExpr{"read", nil, 1},
"$": &callExpr{"shell", nil, 1},
"%": &callExpr{"shell-pipe", nil, 1},
"!": &callExpr{"shell-wait", nil, 1},
"&": &callExpr{"shell-async", nil, 1},
"f": &callExpr{"find", nil, 1},
"F": &callExpr{"find-back", nil, 1},
";": &callExpr{"find-next", nil, 1},
",": &callExpr{"find-prev", nil, 1},
"/": &callExpr{"search", nil, 1},
"?": &callExpr{"search-back", nil, 1},
"n": &callExpr{"search-next", nil, 1},
"N": &callExpr{"search-prev", nil, 1},
"m": &callExpr{"mark-save", nil, 1},
"'": &callExpr{"mark-load", nil, 1},
`"`: &callExpr{"mark-remove", nil, 1},
`r`: &callExpr{"rename", nil, 1},
"<c-n>": &callExpr{"cmd-history-next", nil, 1},
"<c-p>": &callExpr{"cmd-history-prev", nil, 1},

gOpts.keys["zh"] = &setExpr{"hidden!", ""}
gOpts.keys["zr"] = &setExpr{"reverse!", ""}
gOpts.keys["zn"] = &setExpr{"info", ""}
gOpts.keys["zs"] = &setExpr{"info", "size"}
gOpts.keys["zt"] = &setExpr{"info", "time"}
gOpts.keys["za"] = &setExpr{"info", "size:time"}
gOpts.keys["sn"] = &listExpr{[]expr{&setExpr{"sortby", "natural"}, &setExpr{"info", ""}}, 1}
gOpts.keys["ss"] = &listExpr{[]expr{&setExpr{"sortby", "size"}, &setExpr{"info", "size"}}, 1}
gOpts.keys["st"] = &listExpr{[]expr{&setExpr{"sortby", "time"}, &setExpr{"info", "time"}}, 1}
gOpts.keys["sa"] = &listExpr{[]expr{&setExpr{"sortby", "atime"}, &setExpr{"info", "atime"}}, 1}
gOpts.keys["sc"] = &listExpr{[]expr{&setExpr{"sortby", "ctime"}, &setExpr{"info", "ctime"}}, 1}
gOpts.keys["se"] = &listExpr{[]expr{&setExpr{"sortby", "ext"}, &setExpr{"info", ""}}, 1}
gOpts.keys["gh"] = &callExpr{"cd", []string{"~"}, 1}

gOpts.cmdkeys = make(map[string]expr)
"zh": &setExpr{"hidden!", ""},
"zr": &setExpr{"reverse!", ""},
"zn": &setExpr{"info", ""},
"zs": &setExpr{"info", "size"},
"zt": &setExpr{"info", "time"},
"za": &setExpr{"info", "size:time"},
"sn": &listExpr{[]expr{&setExpr{"sortby", "natural"}, &setExpr{"info", ""}}, 1},
"ss": &listExpr{[]expr{&setExpr{"sortby", "size"}, &setExpr{"info", "size"}}, 1},
"st": &listExpr{[]expr{&setExpr{"sortby", "time"}, &setExpr{"info", "time"}}, 1},
"sa": &listExpr{[]expr{&setExpr{"sortby", "atime"}, &setExpr{"info", "atime"}}, 1},
"sc": &listExpr{[]expr{&setExpr{"sortby", "ctime"}, &setExpr{"info", "ctime"}}, 1},
"se": &listExpr{[]expr{&setExpr{"sortby", "ext"}, &setExpr{"info", ""}}, 1},
"gh": &callExpr{"cd", []string{"~"}, 1},
}

gOpts.cmdkeys["<space>"] = &callExpr{"cmd-insert", []string{" "}, 1}
gOpts.cmdkeys["<esc>"] = &callExpr{"cmd-escape", nil, 1}
gOpts.cmdkeys["<tab>"] = &callExpr{"cmd-complete", nil, 1}
gOpts.cmdkeys["<enter>"] = &callExpr{"cmd-enter", nil, 1}
gOpts.cmdkeys["<c-j>"] = &callExpr{"cmd-enter", nil, 1}
gOpts.cmdkeys["<down>"] = &callExpr{"cmd-history-next", nil, 1}
gOpts.cmdkeys["<c-n>"] = &callExpr{"cmd-history-next", nil, 1}
gOpts.cmdkeys["<up>"] = &callExpr{"cmd-history-prev", nil, 1}
gOpts.cmdkeys["<c-p>"] = &callExpr{"cmd-history-prev", nil, 1}
gOpts.cmdkeys["<delete>"] = &callExpr{"cmd-delete", nil, 1}
gOpts.cmdkeys["<c-d>"] = &callExpr{"cmd-delete", nil, 1}
gOpts.cmdkeys["<backspace>"] = &callExpr{"cmd-delete-back", nil, 1}
gOpts.cmdkeys["<backspace2>"] = &callExpr{"cmd-delete-back", nil, 1}
gOpts.cmdkeys["<left>"] = &callExpr{"cmd-left", nil, 1}
gOpts.cmdkeys["<c-b>"] = &callExpr{"cmd-left", nil, 1}
gOpts.cmdkeys["<right>"] = &callExpr{"cmd-right", nil, 1}
gOpts.cmdkeys["<c-f>"] = &callExpr{"cmd-right", nil, 1}
gOpts.cmdkeys["<home>"] = &callExpr{"cmd-home", nil, 1}
gOpts.cmdkeys["<c-a>"] = &callExpr{"cmd-home", nil, 1}
gOpts.cmdkeys["<end>"] = &callExpr{"cmd-end", nil, 1}
gOpts.cmdkeys["<c-e>"] = &callExpr{"cmd-end", nil, 1}
gOpts.cmdkeys["<c-u>"] = &callExpr{"cmd-delete-home", nil, 1}
gOpts.cmdkeys["<c-k>"] = &callExpr{"cmd-delete-end", nil, 1}
gOpts.cmdkeys["<c-w>"] = &callExpr{"cmd-delete-unix-word", nil, 1}
gOpts.cmdkeys["<c-y>"] = &callExpr{"cmd-yank", nil, 1}
gOpts.cmdkeys["<c-t>"] = &callExpr{"cmd-transpose", nil, 1}
gOpts.cmdkeys["<c-c>"] = &callExpr{"cmd-interrupt", nil, 1}
gOpts.cmdkeys["<a-f>"] = &callExpr{"cmd-word", nil, 1}
gOpts.cmdkeys["<a-b>"] = &callExpr{"cmd-word-back", nil, 1}
gOpts.cmdkeys["<a-c>"] = &callExpr{"cmd-capitalize-word", nil, 1}
gOpts.cmdkeys["<a-d>"] = &callExpr{"cmd-delete-word", nil, 1}
gOpts.cmdkeys["<a-backspace>"] = &callExpr{"cmd-delete-word-back", nil, 1}
gOpts.cmdkeys["<a-backspace2>"] = &callExpr{"cmd-delete-word-back", nil, 1}
gOpts.cmdkeys["<a-u>"] = &callExpr{"cmd-uppercase-word", nil, 1}
gOpts.cmdkeys["<a-l>"] = &callExpr{"cmd-lowercase-word", nil, 1}
gOpts.cmdkeys["<a-t>"] = &callExpr{"cmd-transpose-word", nil, 1}
gOpts.cmdkeys = map[string]expr{
"<space>": &callExpr{"cmd-insert", []string{" "}, 1},
"<esc>": &callExpr{"cmd-escape", nil, 1},
"<tab>": &callExpr{"cmd-complete", nil, 1},
"<enter>": &callExpr{"cmd-enter", nil, 1},
"<c-j>": &callExpr{"cmd-enter", nil, 1},
"<down>": &callExpr{"cmd-history-next", nil, 1},
"<c-n>": &callExpr{"cmd-history-next", nil, 1},
"<up>": &callExpr{"cmd-history-prev", nil, 1},
"<c-p>": &callExpr{"cmd-history-prev", nil, 1},
"<delete>": &callExpr{"cmd-delete", nil, 1},
"<c-d>": &callExpr{"cmd-delete", nil, 1},
"<backspace>": &callExpr{"cmd-delete-back", nil, 1},
"<backspace2>": &callExpr{"cmd-delete-back", nil, 1},
"<left>": &callExpr{"cmd-left", nil, 1},
"<c-b>": &callExpr{"cmd-left", nil, 1},
"<right>": &callExpr{"cmd-right", nil, 1},
"<c-f>": &callExpr{"cmd-right", nil, 1},
"<home>": &callExpr{"cmd-home", nil, 1},
"<c-a>": &callExpr{"cmd-home", nil, 1},
"<end>": &callExpr{"cmd-end", nil, 1},
"<c-e>": &callExpr{"cmd-end", nil, 1},
"<c-u>": &callExpr{"cmd-delete-home", nil, 1},
"<c-k>": &callExpr{"cmd-delete-end", nil, 1},
"<c-w>": &callExpr{"cmd-delete-unix-word", nil, 1},
"<c-y>": &callExpr{"cmd-yank", nil, 1},
"<c-t>": &callExpr{"cmd-transpose", nil, 1},
"<c-c>": &callExpr{"cmd-interrupt", nil, 1},
"<a-f>": &callExpr{"cmd-word", nil, 1},
"<a-b>": &callExpr{"cmd-word-back", nil, 1},
"<a-c>": &callExpr{"cmd-capitalize-word", nil, 1},
"<a-d>": &callExpr{"cmd-delete-word", nil, 1},
"<a-backspace>": &callExpr{"cmd-delete-word-back", nil, 1},
"<a-backspace2>": &callExpr{"cmd-delete-word-back", nil, 1},
"<a-u>": &callExpr{"cmd-uppercase-word", nil, 1},
"<a-l>": &callExpr{"cmd-lowercase-word", nil, 1},
"<a-t>": &callExpr{"cmd-transpose-word", nil, 1},
}

gOpts.cmds = make(map[string]expr)
gOpts.user = make(map[string]string)
Expand Down