diff --git a/filter/fuzzy.go b/filter/fuzzy.go index 19f5845..a96d516 100644 --- a/filter/fuzzy.go +++ b/filter/fuzzy.go @@ -9,19 +9,31 @@ import ( "strings" "sync" + "github.com/monochromegane/go-gitignore" "github.com/sahilm/fuzzy" ) -func newFuzzySearch() *fuzzySearch { - return &fuzzySearch{ +func newFuzzySearch(option *SearchOption) *fuzzySearch { + fs := &fuzzySearch{ cachedFile: make(map[string]string), isDuplicate: make(map[string]bool), } + gitIgnorePath := filepath.Join(option.TargetDir, ".gitignore") + if _, err := os.Stat(gitIgnorePath); os.IsExist(err) { + gitIgnore, err := gitignore.NewGitIgnore(gitIgnorePath) + if err == nil { + fs.lookGitIgnore = true + fs.gitIgnore = gitIgnore + } + } + return fs } type fuzzySearch struct { - cachedFile map[string]string - isDuplicate map[string]bool + cachedFile map[string]string + isDuplicate map[string]bool + gitIgnore gitignore.IgnoreMatcher + lookGitIgnore bool } type file struct { @@ -153,6 +165,16 @@ func (f *fuzzySearch) getFileNames(dir string) ([]string, error) { if info.IsDir() { return nil } + if f.lookGitIgnore && f.gitIgnore.Match(path, false) { + return nil + } + // skip hidden directory or file + components := strings.Split(path, "/") + for _, comp := range components { + if strings.HasPrefix(comp, ".") { + return nil + } + } fileNames = append(fileNames, path) return nil }) diff --git a/filter/search.go b/filter/search.go index 1c7ac12..15f10e5 100644 --- a/filter/search.go +++ b/filter/search.go @@ -36,12 +36,12 @@ const ( NoneCommand ) -func NewFilter(cmd SearchCommand) Filter { - switch cmd { +func NewFilter(option *SearchOption) Filter { + switch option.Command { case RipGrep: return newRg() case FuzzySearch: - return newFuzzySearch() + return newFuzzySearch(option) default: return newRg() } diff --git a/go.mod b/go.mod index c4bc6e6..6e1885f 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/gdamore/tcell/v2 v2.2.0 github.com/integrii/flaggy v1.4.4 github.com/kylelemons/godebug v1.1.0 // indirect + github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 github.com/rivo/tview v0.0.0-20210215180505-b1efc6d8c1b8 github.com/sahilm/fuzzy v0.1.0 ) diff --git a/go.sum b/go.sum index 23f6ca5..6d6859a 100644 --- a/go.sum +++ b/go.sum @@ -3,8 +3,6 @@ github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo github.com/gdamore/tcell v1.4.0 h1:vUnHwJRvcPQa3tzi+0QI4U9JINXYJlOz9yiaiPQ2wMU= github.com/gdamore/tcell v1.4.0/go.mod h1:vxEiSDZdW3L+Uhjii9c3375IlDmR05bzxY404ZVSMo0= github.com/gdamore/tcell/v2 v2.0.1-0.20201017141208-acf90d56d591/go.mod h1:vSVL/GV5mCSlPC6thFP5kfOFdM9MGZcalipmpTxTgQA= -github.com/gdamore/tcell/v2 v2.1.0 h1:UnSmozHgBkQi2PGsFr+rpdXuAPRRucMegpQp3Z3kDro= -github.com/gdamore/tcell/v2 v2.1.0/go.mod h1:vSVL/GV5mCSlPC6thFP5kfOFdM9MGZcalipmpTxTgQA= github.com/gdamore/tcell/v2 v2.2.0 h1:vSyEgKwraXPSOkvCk7IwOSyX+Pv3V2cV9CikJMXg4U4= github.com/gdamore/tcell/v2 v2.2.0/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU= github.com/integrii/flaggy v1.4.4 h1:8fGyiC14o0kxhTqm2VBoN19fDKPZsKipP7yggreTMDc= @@ -14,12 +12,10 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+ github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac= github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= -github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRRpdGg= github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= -github.com/rivo/tview v0.0.0-20201204190810-5406288b8e4e h1:eP1XZiExUPO/FjS2q/PBo3CYbEtVvoMi8b7IpCBDWSo= -github.com/rivo/tview v0.0.0-20201204190810-5406288b8e4e/go.mod h1:0ha5CGekam8ZV1kxkBxSlh7gfQ7YolUj2P/VruwH0QY= +github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 h1:n6/2gBQ3RWajuToeY6ZtZTIKv2v7ThUy5KKusIT0yc0= +github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod h1:Pm3mSP3c5uWn86xMLZ5Sa7JB9GsEZySvHYXCTK4E9q4= github.com/rivo/tview v0.0.0-20210215180505-b1efc6d8c1b8 h1:bEm0+lS2TTbv6N32lOq6nPROteRzVjYph0pZhhHo7zk= github.com/rivo/tview v0.0.0-20210215180505-b1efc6d8c1b8/go.mod h1:1QW7hX7RQzOqyGgx8O64bRPQBrFtPflioPPX5gFPV3A= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= @@ -28,15 +24,12 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI= github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7 h1:XtNJkfEjb4zR3q20BBBcYUykVOEMgZeIUOpBPfNYgxg= -golang.org/x/sys v0.0.0-20201017003518-b09fb700fbb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78 h1:nVuTkr9L6Bq62qpUqKo/RnZCFfzDBL0bYo6w9OJUqZY= golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf h1:MZ2shdL+ZM/XzY3ZGOnh4Nlpnxz5GSOhOmtHo3iPU6M= golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/ilse b/ilse new file mode 100755 index 0000000..6acffa0 Binary files /dev/null and b/ilse differ diff --git a/search_bar.go b/search_bar.go index 3c4fdd6..939c599 100644 --- a/search_bar.go +++ b/search_bar.go @@ -90,7 +90,7 @@ func initSearchBar() { clearResult() return } - ftr := filter.NewFilter(app.searchOption.Command) + ftr := filter.NewFilter(app.searchOption) results, err := ftr.Search(text, app.searchOption) if err != nil { logger.Error("search error : %v", err)