From fa2fa19ab05b8b4bc8251aa82133643045f1ff1f Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Mon, 4 Nov 2024 05:51:38 +0700 Subject: [PATCH] Small optimizations for opts.go (#1831) * Small optimizations for opts.go * Add back all opts --- opts.go | 240 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 120 insertions(+), 120 deletions(-) diff --git a/opts.go b/opts.go index 3431a338..a0ec15ec 100644 --- a/opts.go +++ b/opts.go @@ -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'` @@ -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)) } @@ -258,120 +258,120 @@ func init() { gOpts.numberfmt = "\033[33m" gOpts.tagfmt = "\033[31m" - gOpts.keys = make(map[string]expr) - - gOpts.keys["k"] = &callExpr{"up", nil, 1} - gOpts.keys[""] = &callExpr{"up", nil, 1} - gOpts.keys[""] = &callExpr{"up", nil, 1} - gOpts.keys[""] = &callExpr{"half-up", nil, 1} - gOpts.keys[""] = &callExpr{"page-up", nil, 1} - gOpts.keys[""] = &callExpr{"page-up", nil, 1} - gOpts.keys[""] = &callExpr{"scroll-up", nil, 1} - gOpts.keys[""] = &callExpr{"scroll-up", nil, 1} - gOpts.keys["j"] = &callExpr{"down", nil, 1} - gOpts.keys[""] = &callExpr{"down", nil, 1} - gOpts.keys[""] = &callExpr{"down", nil, 1} - gOpts.keys[""] = &callExpr{"half-down", nil, 1} - gOpts.keys[""] = &callExpr{"page-down", nil, 1} - gOpts.keys[""] = &callExpr{"page-down", nil, 1} - gOpts.keys[""] = &callExpr{"scroll-down", nil, 1} - gOpts.keys[""] = &callExpr{"scroll-down", nil, 1} - gOpts.keys["h"] = &callExpr{"updir", nil, 1} - gOpts.keys[""] = &callExpr{"updir", nil, 1} - gOpts.keys["l"] = &callExpr{"open", nil, 1} - gOpts.keys[""] = &callExpr{"open", nil, 1} - gOpts.keys["q"] = &callExpr{"quit", nil, 1} - gOpts.keys["gg"] = &callExpr{"top", nil, 1} - gOpts.keys[""] = &callExpr{"top", nil, 1} - gOpts.keys["G"] = &callExpr{"bottom", nil, 1} - gOpts.keys[""] = &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[""] = &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[""] = &callExpr{"redraw", nil, 1} - gOpts.keys[""] = &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[""] = &callExpr{"cmd-history-next", nil, 1} - gOpts.keys[""] = &callExpr{"cmd-history-prev", nil, 1} + gOpts.keys = map[string]expr{ + "k": &callExpr{"up", nil, 1}, + "": &callExpr{"up", nil, 1}, + "": &callExpr{"up", nil, 1}, + "": &callExpr{"half-up", nil, 1}, + "": &callExpr{"page-up", nil, 1}, + "": &callExpr{"page-up", nil, 1}, + "": &callExpr{"scroll-up", nil, 1}, + "": &callExpr{"scroll-up", nil, 1}, + "j": &callExpr{"down", nil, 1}, + "": &callExpr{"down", nil, 1}, + "": &callExpr{"down", nil, 1}, + "": &callExpr{"half-down", nil, 1}, + "": &callExpr{"page-down", nil, 1}, + "": &callExpr{"page-down", nil, 1}, + "": &callExpr{"scroll-down", nil, 1}, + "": &callExpr{"scroll-down", nil, 1}, + "h": &callExpr{"updir", nil, 1}, + "": &callExpr{"updir", nil, 1}, + "l": &callExpr{"open", nil, 1}, + "": &callExpr{"open", nil, 1}, + "q": &callExpr{"quit", nil, 1}, + "gg": &callExpr{"top", nil, 1}, + "": &callExpr{"top", nil, 1}, + "G": &callExpr{"bottom", nil, 1}, + "": &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}, + "": &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}, + "": &callExpr{"redraw", nil, 1}, + "": &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}, + "": &callExpr{"cmd-history-next", nil, 1}, + "": &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[""] = &callExpr{"cmd-insert", []string{" "}, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-escape", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-complete", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-enter", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-enter", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-history-next", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-history-next", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-history-prev", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-history-prev", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-delete", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-delete", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-delete-back", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-delete-back", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-left", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-left", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-right", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-right", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-home", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-home", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-end", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-end", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-delete-home", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-delete-end", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-delete-unix-word", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-yank", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-transpose", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-interrupt", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-word", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-word-back", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-capitalize-word", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-delete-word", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-delete-word-back", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-delete-word-back", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-uppercase-word", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-lowercase-word", nil, 1} - gOpts.cmdkeys[""] = &callExpr{"cmd-transpose-word", nil, 1} + gOpts.cmdkeys = map[string]expr{ + "": &callExpr{"cmd-insert", []string{" "}, 1}, + "": &callExpr{"cmd-escape", nil, 1}, + "": &callExpr{"cmd-complete", nil, 1}, + "": &callExpr{"cmd-enter", nil, 1}, + "": &callExpr{"cmd-enter", nil, 1}, + "": &callExpr{"cmd-history-next", nil, 1}, + "": &callExpr{"cmd-history-next", nil, 1}, + "": &callExpr{"cmd-history-prev", nil, 1}, + "": &callExpr{"cmd-history-prev", nil, 1}, + "": &callExpr{"cmd-delete", nil, 1}, + "": &callExpr{"cmd-delete", nil, 1}, + "": &callExpr{"cmd-delete-back", nil, 1}, + "": &callExpr{"cmd-delete-back", nil, 1}, + "": &callExpr{"cmd-left", nil, 1}, + "": &callExpr{"cmd-left", nil, 1}, + "": &callExpr{"cmd-right", nil, 1}, + "": &callExpr{"cmd-right", nil, 1}, + "": &callExpr{"cmd-home", nil, 1}, + "": &callExpr{"cmd-home", nil, 1}, + "": &callExpr{"cmd-end", nil, 1}, + "": &callExpr{"cmd-end", nil, 1}, + "": &callExpr{"cmd-delete-home", nil, 1}, + "": &callExpr{"cmd-delete-end", nil, 1}, + "": &callExpr{"cmd-delete-unix-word", nil, 1}, + "": &callExpr{"cmd-yank", nil, 1}, + "": &callExpr{"cmd-transpose", nil, 1}, + "": &callExpr{"cmd-interrupt", nil, 1}, + "": &callExpr{"cmd-word", nil, 1}, + "": &callExpr{"cmd-word-back", nil, 1}, + "": &callExpr{"cmd-capitalize-word", nil, 1}, + "": &callExpr{"cmd-delete-word", nil, 1}, + "": &callExpr{"cmd-delete-word-back", nil, 1}, + "": &callExpr{"cmd-delete-word-back", nil, 1}, + "": &callExpr{"cmd-uppercase-word", nil, 1}, + "": &callExpr{"cmd-lowercase-word", nil, 1}, + "": &callExpr{"cmd-transpose-word", nil, 1}, + } gOpts.cmds = make(map[string]expr) gOpts.user = make(map[string]string)