pax_global_header 0000666 0000000 0000000 00000000064 14435376215 0014524 g ustar 00root root 0000000 0000000 52 comment=c48e322e2a8f19bbd962faf82eee4cf11dbf81ba
fuzzy-0.1.1/ 0000775 0000000 0000000 00000000000 14435376215 0012712 5 ustar 00root root 0000000 0000000 fuzzy-0.1.1/.editorconfig 0000664 0000000 0000000 00000000370 14435376215 0015367 0 ustar 00root root 0000000 0000000 [*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
[*.sh]
indent_size = 4
[Makefile]
indent_style = tab
indent_size = 4
[*.go]
indent_style = tab
indent_size = 4
fuzzy-0.1.1/.gitignore 0000664 0000000 0000000 00000000022 14435376215 0014674 0 ustar 00root root 0000000 0000000 vendor/
coverage/
fuzzy-0.1.1/.travis.yml 0000664 0000000 0000000 00000000106 14435376215 0015020 0 ustar 00root root 0000000 0000000 arch:
- amd64
- ppc64le
language: go
go:
- 1.x
script:
- make
fuzzy-0.1.1/CONTRIBUTING.md 0000664 0000000 0000000 00000000162 14435376215 0015142 0 ustar 00root root 0000000 0000000 Everyone is welcome to contribute. Please send me a pull request or file an issue. I promise to respond promptly.
fuzzy-0.1.1/Gopkg.lock 0000664 0000000 0000000 00000001025 14435376215 0014631 0 ustar 00root root 0000000 0000000 # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
branch = "master"
digest = "1:ee97ec8a00b2424570c1ce53d7b410e96fbd4c241b29df134276ff6aa3750335"
name = "github.com/kylelemons/godebug"
packages = [
"diff",
"pretty",
]
pruneopts = ""
revision = "d65d576e9348f5982d7f6d83682b694e731a45c6"
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
input-imports = ["github.com/kylelemons/godebug/pretty"]
solver-name = "gps-cdcl"
solver-version = 1
fuzzy-0.1.1/Gopkg.toml 0000664 0000000 0000000 00000000136 14435376215 0014656 0 ustar 00root root 0000000 0000000 # Test dependency
[[constraint]]
branch = "master"
name = "github.com/kylelemons/godebug"
fuzzy-0.1.1/LICENSE 0000664 0000000 0000000 00000002067 14435376215 0013724 0 ustar 00root root 0000000 0000000 The MIT License (MIT)
Copyright (c) 2017 Sahil Muthoo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
fuzzy-0.1.1/Makefile 0000664 0000000 0000000 00000002106 14435376215 0014351 0 ustar 00root root 0000000 0000000 .PHONY: all
all: setup lint test
.PHONY: test
test: setup
go test -bench ./...
.PHONY: cover
cover: setup
mkdir -p coverage
gocov test ./... | gocov-html > coverage/coverage.html
sources = $(shell find . -name '*.go' -not -path './vendor/*')
.PHONY: goimports
goimports: setup
goimports -w $(sources)
.PHONY: lint
lint: setup
gometalinter ./... --enable=goimports --disable=gocyclo --vendor -t
.PHONY: install
install: setup
go install
BIN_DIR := $(GOPATH)/bin
GOIMPORTS := $(BIN_DIR)/goimports
GOMETALINTER := $(BIN_DIR)/gometalinter
DEP := $(BIN_DIR)/dep
GOCOV := $(BIN_DIR)/gocov
GOCOV_HTML := $(BIN_DIR)/gocov-html
$(GOIMPORTS):
go get -u golang.org/x/tools/cmd/goimports
$(GOMETALINTER):
go get -u github.com/alecthomas/gometalinter
gometalinter --install &> /dev/null
$(GOCOV):
go get -u github.com/axw/gocov/gocov
$(GOCOV_HTML):
go get -u gopkg.in/matm/v1/gocov-html
$(DEP):
go get -u github.com/golang/dep/cmd/dep
tools: $(GOIMPORTS) $(GOMETALINTER) $(GOCOV) $(GOCOV_HTML) $(DEP)
vendor: $(DEP)
dep ensure
setup: tools vendor
updatedeps:
dep ensure -update
fuzzy-0.1.1/README.md 0000664 0000000 0000000 00000012240 14435376215 0014170 0 ustar 00root root 0000000 0000000
# fuzzy
[](https://travis-ci.org/sahilm/fuzzy)
[](https://godoc.org/github.com/sahilm/fuzzy)
Go library that provides fuzzy string matching optimized for filenames and code symbols in the style of Sublime Text,
VSCode, IntelliJ IDEA et al. This library is external dependency-free. It only depends on the Go standard library.
## Features
- Intuitive matching. Results are returned in descending order of match quality. Quality is determined by:
- The first character in the pattern matches the first character in the match string.
- The matched character is camel cased.
- The matched character follows a separator such as an underscore character.
- The matched character is adjacent to a previous match.
- Speed. Matches are returned in milliseconds. It's perfect for interactive search boxes.
- The positions of matches are returned. Allows you to highlight matching characters.
- Unicode aware.
## Demo
Here is a [demo](_example/main.go) of matching various patterns against ~16K files from the Unreal Engine 4 codebase.

You can run the demo yourself like so:
```
cd _example/
go get github.com/jroimartin/gocui
go run main.go
```
## Usage
The following example prints out matches with the matched chars in bold.
```go
package main
import (
"fmt"
"github.com/sahilm/fuzzy"
)
func main() {
const bold = "\033[1m%s\033[0m"
pattern := "mnr"
data := []string{"game.cpp", "moduleNameResolver.ts", "my name is_Ramsey"}
matches := fuzzy.Find(pattern, data)
for _, match := range matches {
for i := 0; i < len(match.Str); i++ {
if contains(i, match.MatchedIndexes) {
fmt.Print(fmt.Sprintf(bold, string(match.Str[i])))
} else {
fmt.Print(string(match.Str[i]))
}
}
fmt.Println()
}
}
func contains(needle int, haystack []int) bool {
for _, i := range haystack {
if needle == i {
return true
}
}
return false
}
```
If the data you want to match isn't a slice of strings, you can use `FindFrom` by implementing
the provided `Source` interface. Here's an example:
```go
package main
import (
"fmt"
"github.com/sahilm/fuzzy"
)
type employee struct {
name string
age int
}
type employees []employee
func (e employees) String(i int) string {
return e[i].name
}
func (e employees) Len() int {
return len(e)
}
func main() {
emps := employees{
{
name: "Alice",
age: 45,
},
{
name: "Bob",
age: 35,
},
{
name: "Allie",
age: 35,
},
}
results := fuzzy.FindFrom("al", emps)
for _, r := range results {
fmt.Println(emps[r.Index])
}
}
```
Check out the [godoc](https://godoc.org/github.com/sahilm/fuzzy) for detailed documentation.
## Installation
`go get github.com/sahilm/fuzzy` or use your favorite dependency management tool.
## Speed
Here are a few benchmark results on a normal laptop.
```
BenchmarkFind/with_unreal_4_(~16K_files)-4 100 12915315 ns/op
BenchmarkFind/with_linux_kernel_(~60K_files)-4 50 30885038 ns/op
```
Matching a pattern against ~60K files from the Linux kernel takes about 30ms.
## Contributing
Everyone is welcome to contribute. Please send me a pull request or file an issue. I promise
to respond promptly.
## Credits
* [@ericpauley](https://github.com/ericpauley) & [@lunixbochs](https://github.com/lunixbochs) contributed Unicode awareness and various performance optimisations.
* The algorithm is based of the awesome work of [forrestthewoods](https://github.com/forrestthewoods/lib_fts/blob/master/code/fts_fuzzy_match.js).
See [this](https://blog.forrestthewoods.com/reverse-engineering-sublime-text-s-fuzzy-match-4cffeed33fdb#.d05n81yjy)
blog post for details of the algorithm.
* The artwork is by my lovely wife Sanah. It's based on the Go Gopher.
* The Go gopher was designed by Renee French (http://reneefrench.blogspot.com/).
The design is licensed under the Creative Commons 3.0 Attributions license.
## License
The MIT License (MIT)
Copyright (c) 2017 Sahil Muthoo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
fuzzy-0.1.1/_example/ 0000775 0000000 0000000 00000000000 14435376215 0014504 5 ustar 00root root 0000000 0000000 fuzzy-0.1.1/_example/main.go 0000664 0000000 0000000 00000012602 14435376215 0015760 0 ustar 00root root 0000000 0000000 package main
import (
"fmt"
"log"
"io/ioutil"
"strings"
"time"
"github.com/jroimartin/gocui"
"github.com/sahilm/fuzzy"
)
var filenamesBytes []byte
var err error
var filenames []string
var g *gocui.Gui
func main() {
filenamesBytes, err = ioutil.ReadFile("../testdata/ue4_filenames.txt")
if err != nil {
panic(err)
}
filenames = strings.Split(string(filenamesBytes), "\n")
g, err = gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Panicln(err)
}
defer g.Close()
g.Cursor = true
g.Mouse = false
g.SetManagerFunc(layout)
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("finder", gocui.KeyArrowRight, gocui.ModNone, switchToMainView); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("main", gocui.KeyArrowLeft, gocui.ModNone, switchToSideView); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil {
log.Panicln(err)
}
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
}
func cursorDown(g *gocui.Gui, v *gocui.View) error {
if v != nil {
cx, cy := v.Cursor()
if err := v.SetCursor(cx, cy+1); err != nil {
ox, oy := v.Origin()
if err := v.SetOrigin(ox, oy+1); err != nil {
return err
}
}
}
return nil
}
func cursorUp(g *gocui.Gui, v *gocui.View) error {
if v != nil {
ox, oy := v.Origin()
cx, cy := v.Cursor()
if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
if err := v.SetOrigin(ox, oy-1); err != nil {
return err
}
}
}
return nil
}
func switchToSideView(g *gocui.Gui, view *gocui.View) error {
if _, err := g.SetCurrentView("finder"); err != nil {
return err
}
return nil
}
func switchToMainView(g *gocui.Gui, view *gocui.View) error {
if _, err := g.SetCurrentView("main"); err != nil {
return err
}
return nil
}
func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView("finder", -1, 0, 80, 10); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Wrap = true
v.Editable = true
v.Frame = true
v.Title = "Type pattern here. Press -> or <- to switch between panes"
if _, err := g.SetCurrentView("finder"); err != nil {
return err
}
v.Editor = gocui.EditorFunc(finder)
}
if v, err := g.SetView("main", 79, 0, maxX, maxY-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
fmt.Fprintf(v, "%s", filenamesBytes)
v.Editable = false
v.Wrap = true
v.Frame = true
v.Title = "list of all files"
}
if v, err := g.SetView("results", -1, 3, 79, maxY-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Editable = false
v.Wrap = true
v.Frame = true
v.Title = "Search Results"
}
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func finder(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
switch {
case ch != 0 && mod == 0:
v.EditWrite(ch)
g.Update(func(gui *gocui.Gui) error {
results, err := g.View("results")
if err != nil {
// handle error
}
results.Clear()
t := time.Now()
matches := fuzzy.Find(strings.TrimSpace(v.ViewBuffer()), filenames)
elapsed := time.Since(t)
fmt.Fprintf(results, "found %v matches in %v\n", len(matches), elapsed)
for _, match := range matches {
for i := 0; i < len(match.Str); i++ {
if contains(i, match.MatchedIndexes) {
fmt.Fprintf(results, fmt.Sprintf("\033[1m%s\033[0m", string(match.Str[i])))
} else {
fmt.Fprintf(results, string(match.Str[i]))
}
}
fmt.Fprintln(results, "")
}
return nil
})
case key == gocui.KeySpace:
v.EditWrite(' ')
case key == gocui.KeyBackspace || key == gocui.KeyBackspace2:
v.EditDelete(true)
g.Update(func(gui *gocui.Gui) error {
results, err := g.View("results")
if err != nil {
// handle error
}
results.Clear()
t := time.Now()
matches := fuzzy.Find(strings.TrimSpace(v.ViewBuffer()), filenames)
elapsed := time.Since(t)
fmt.Fprintf(results, "found %v matches in %v\n", len(matches), elapsed)
for _, match := range matches {
for i := 0; i < len(match.Str); i++ {
if contains(i, match.MatchedIndexes) {
fmt.Fprintf(results, fmt.Sprintf("\033[1m%s\033[0m", string(match.Str[i])))
} else {
fmt.Fprintf(results, string(match.Str[i]))
}
}
fmt.Fprintln(results, "")
}
return nil
})
case key == gocui.KeyDelete:
v.EditDelete(false)
g.Update(func(gui *gocui.Gui) error {
results, err := g.View("results")
if err != nil {
// handle error
}
results.Clear()
t := time.Now()
matches := fuzzy.Find(strings.TrimSpace(v.ViewBuffer()), filenames)
elapsed := time.Since(t)
fmt.Fprintf(results, "found %v matches in %v\n", len(matches), elapsed)
for _, match := range matches {
for i := 0; i < len(match.Str); i++ {
if contains(i, match.MatchedIndexes) {
fmt.Fprintf(results, fmt.Sprintf("\033[1m%s\033[0m", string(match.Str[i])))
} else {
fmt.Fprintf(results, string(match.Str[i]))
}
}
fmt.Fprintln(results, "")
}
return nil
})
case key == gocui.KeyInsert:
v.Overwrite = !v.Overwrite
}
}
func contains(needle int, haystack []int) bool {
for _, i := range haystack {
if needle == i {
return true
}
}
return false
}
fuzzy-0.1.1/assets/ 0000775 0000000 0000000 00000000000 14435376215 0014214 5 ustar 00root root 0000000 0000000 fuzzy-0.1.1/assets/demo.gif 0000664 0000000 0000000 00013365642 14435376215 0015653 0 ustar 00root root 0000000 0000000 GIF89a X ŗ!""./0ABC$$%PQQ456YYZghhkllzzz89:;<