From 7d9548919e9b1bb05bb95225275e34213a747cde Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 26 Nov 2024 17:26:16 +0900 Subject: [PATCH] Extend --walker-skip to support multi-component patterns fzf --walker-skip 'foo/bar' Close #4107 --- src/reader.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/reader.go b/src/reader.go index 80387ba..d78f10f 100644 --- a/src/reader.go +++ b/src/reader.go @@ -7,6 +7,7 @@ import ( "io/fs" "os" "path/filepath" + "strings" "sync" "sync/atomic" "time" @@ -272,6 +273,26 @@ func (r *Reader) readFiles(roots []string, opts walkerOpts, ignores []string) bo ToSlash: fastwalk.DefaultToSlash(), Sort: fastwalk.SortFilesFirst, } + ignoresBase := []string{} + ignoresFull := []string{} + ignoresSuffix := []string{} + sep := string(os.PathSeparator) + for _, ignore := range ignores { + if strings.ContainsRune(ignore, os.PathSeparator) { + if strings.HasPrefix(ignore, sep) { + ignoresSuffix = append(ignoresSuffix, ignore) + } else { + // 'foo/bar' should match match + // * 'foo/bar' + // * 'baz/foo/bar' + // * but NOT 'bazfoo/bar' + ignoresFull = append(ignoresFull, ignore) + ignoresSuffix = append(ignoresSuffix, sep+ignore) + } + } else { + ignoresBase = append(ignoresBase, ignore) + } + } fn := func(path string, de os.DirEntry, err error) error { if err != nil { return nil @@ -284,11 +305,21 @@ func (r *Reader) readFiles(roots []string, opts walkerOpts, ignores []string) bo if !opts.hidden && base[0] == '.' && base != ".." { return filepath.SkipDir } - for _, ignore := range ignores { + for _, ignore := range ignoresBase { if ignore == base { return filepath.SkipDir } } + for _, ignore := range ignoresFull { + if ignore == path { + return filepath.SkipDir + } + } + for _, ignore := range ignoresSuffix { + if strings.HasSuffix(path, ignore) { + return filepath.SkipDir + } + } } if ((opts.file && !isDir) || (opts.dir && isDir)) && r.pusher(stringBytes(path)) { atomic.StoreInt32(&r.event, int32(EvtReadNew))