diff --git a/complete.go b/complete.go index d00a66de..02dfd7f6 100644 --- a/complete.go +++ b/complete.go @@ -139,6 +139,7 @@ var ( "ratios", "info", "shellopts", + "hiddenfiles", } ) diff --git a/doc.go b/doc.go index e34292e0..4d45c5d1 100644 --- a/doc.go +++ b/doc.go @@ -134,6 +134,7 @@ The following options can be used to customize the behavior of lf: ratios string (default '1:2:3') info string (default '') shellopts string (default '') + hiddenfiles string (default '.*') The following variables are exported for shell commands: diff --git a/eval.go b/eval.go index 9a4bdf5e..9a46fd20 100644 --- a/eval.go +++ b/eval.go @@ -296,6 +296,8 @@ func (e *setExpr) eval(app *app, args []string) { gOpts.info = toks case "shellopts": gOpts.shellopts = strings.Split(e.val, ":") + case "hiddenfiles": + gOpts.hiddenfiles = strings.Split(e.val, ":") default: app.ui.echoerrf("unknown option: %s", e.opt) return diff --git a/nav.go b/nav.go index f5abf812..898d6a4c 100644 --- a/nav.go +++ b/nav.go @@ -204,13 +204,13 @@ func (dir *dir) sort() { // files to the first non-hidden file in the list if gOpts.sortType.option&hiddenSort == 0 { sort.SliceStable(dir.files, func(i, j int) bool { - if isHidden(dir.files[i]) && isHidden(dir.files[j]) { + if isHidden(dir.files[i], dir.path) && isHidden(dir.files[j], dir.path) { return i < j } - return isHidden(dir.files[i]) + return isHidden(dir.files[i], dir.path) }) for i, f := range dir.files { - if !isHidden(f) { + if !isHidden(f, dir.path) { dir.files = dir.files[i:] return } diff --git a/opts.go b/opts.go index 25cdc1c6..7a686f50 100644 --- a/opts.go +++ b/opts.go @@ -58,6 +58,7 @@ var gOpts struct { ratios []int info []string shellopts []string + hiddenfiles []string keys map[string]expr cmdkeys map[string]expr cmds map[string]expr @@ -95,6 +96,7 @@ func init() { gOpts.ratios = []int{1, 2, 3} gOpts.info = nil gOpts.shellopts = nil + gOpts.hiddenfiles = []string{".*"} gOpts.sortType = sortType{naturalSort, dirfirstSort} gOpts.keys = make(map[string]expr) diff --git a/os.go b/os.go index 1a268575..56bf0617 100644 --- a/os.go +++ b/os.go @@ -133,8 +133,38 @@ func isExecutable(f os.FileInfo) bool { return f.Mode()&0111 != 0 } -func isHidden(f os.FileInfo) bool { - return f.Name()[0] == '.' +func isHidden(f os.FileInfo, path string) (hidden bool) { + for _, pattern := range gOpts.hiddenfiles { + if len(pattern) == 0 { + continue + } + + matched := matchPattern(strings.TrimPrefix(pattern, "!"), f.Name(), path) + if pattern[0] == '!' && matched { + hidden = false + } else if matched { + hidden = true + } + } + return hidden +} + +func matchPattern(pattern, name, path string) bool { + matchStr := name + + if pattern[0] == '~' { + pattern = gUser.HomeDir + strings.TrimPrefix(pattern, "~") + } + if pattern[0] == filepath.Separator { + matchStr = filepath.Join(path, name) + } + + isMatch, err := filepath.Match(pattern, matchStr) + if err != nil { + log.Printf("pattern %s caused error with %s", pattern, matchStr) + return false + } + return isMatch } func exportFiles(f string, fs []string) { diff --git a/os_windows.go b/os_windows.go index 8f2de156..dcd82514 100644 --- a/os_windows.go +++ b/os_windows.go @@ -114,9 +114,9 @@ func isExecutable(f os.FileInfo) bool { return false } -func isHidden(f os.FileInfo) bool { +func isHidden(f os.FileInfo, path string) (hidden bool) { // TODO: implement - return false + return hidden } func exportFiles(f string, fs []string) {