Skip to content

Commit

Permalink
skip hidden file and look gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
tjmtmmnk committed Feb 24, 2021
1 parent b1998be commit 5e97901
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 18 deletions.
30 changes: 26 additions & 4 deletions filter/fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
})
Expand Down
6 changes: 3 additions & 3 deletions filter/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
13 changes: 3 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Binary file added ilse
Binary file not shown.
2 changes: 1 addition & 1 deletion search_bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5e97901

Please # to comment.