Skip to content

Commit

Permalink
Allow to search by substrings of description
Browse files Browse the repository at this point in the history
  • Loading branch information
baopham committed May 23, 2017
1 parent 2af9df9 commit a5462b3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 31 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ USAGE:
snip [global options] command [command options] [arguments...]
VERSION:
1.0.1
1.0.2
COMMANDS:
add, a snip add -k="port" -c="lsof -i :{p}" -desc="List processes listening on a particular port"
Expand Down Expand Up @@ -63,7 +63,10 @@ you should see:
### Search

```bash
# by substring of keyword
snip search port
# by substrings of description
snip search "list process"
```

you should see:
Expand Down
6 changes: 3 additions & 3 deletions cli/cli_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

func Search(c *cli.Context) error {
keyword := strings.TrimSpace(c.Args().First())
searchTerm := strings.TrimSpace(c.Args().First())

if keyword == "" {
if searchTerm == "" {
return MissingInfoError{Message: "Please specify your keyword"}
}

Expand All @@ -21,7 +21,7 @@ func Search(c *cli.Context) error {
return err
}

snippets, err := s.Search(keyword, filePath)
snippets, err := s.Search(searchTerm, filePath)

if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Action(fn func(c *cli.Context) error) func(c *cli.Context) error {

func main() {
app := cli.NewApp()
app.Version = "1.0.1"
app.Version = "1.0.2"
app.Usage = "Save snippets: commands, texts, emoji, etc."
app.EnableBashCompletion = true
app.Commands = []cli.Command{
Expand All @@ -50,7 +50,7 @@ func main() {
{
Name: "search",
Aliases: []string{"s"},
Usage: "search for snippets by keyword: snip search port",
Usage: "search for snippets: snip search port",
Action: Action(snippetCli.Search),
},
{
Expand Down
25 changes: 11 additions & 14 deletions snippet/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/baopham/snip/util"
"github.com/renstrom/fuzzysearch/fuzzy"
)

// EOL end of line
Expand Down Expand Up @@ -107,12 +108,12 @@ func (s *Snippet) Remove(filePath string) error {

// Get all saved snippets
func GetAll(filePath string) ([]*Snippet, error) {
return searchByKeyword("", filePath, SEARCH_MATCH_ANY)
return searchSnippets("", filePath, SEARCH_MATCH_ANY)
}

// SearchExact exact search by keyword
func SearchExact(keyword string, filePath string) (*Snippet, error) {
snippets, err := searchByKeyword(keyword, filePath, SEARCH_EXACT)
snippets, err := searchSnippets(keyword, filePath, SEARCH_EXACT)

if err != nil {
return nil, err
Expand All @@ -125,9 +126,9 @@ func SearchExact(keyword string, filePath string) (*Snippet, error) {
return snippets[0], nil
}

// Search fuzzy search by keyword
func Search(keyword string, filePath string) ([]*Snippet, error) {
return searchByKeyword(keyword, filePath, SEARCH_FUZZY)
// Search fuzzy search by given search term
func Search(searchTerm string, filePath string) ([]*Snippet, error) {
return searchSnippets(searchTerm, filePath, SEARCH_FUZZY)
}

func (s *Snippet) String() string {
Expand Down Expand Up @@ -224,7 +225,7 @@ func getScanner(file *os.File) *bufio.Scanner {
return scanner
}

func searchByKeyword(keyword string, filePath string, exact SearchCode) ([]*Snippet, error) {
func searchSnippets(searchTerm string, filePath string, exact SearchCode) ([]*Snippet, error) {
var snippets []*Snippet

file, err := os.Open(filePath)
Expand All @@ -235,7 +236,7 @@ func searchByKeyword(keyword string, filePath string, exact SearchCode) ([]*Snip

scanner := getScanner(file)

matcher := fuzzyMatcher
matcher := fuzzy.MatchFold

if exact == SEARCH_EXACT {
matcher = exactMatcher
Expand All @@ -246,7 +247,7 @@ func searchByKeyword(keyword string, filePath string, exact SearchCode) ([]*Snip
for line := 1; scanner.Scan(); line++ {
lineContent := scanner.Text()

if !matcher(keyword, lineContent) {
if !matcher(searchTerm, lineContent) {
continue
}

Expand All @@ -268,12 +269,8 @@ func searchByKeyword(keyword string, filePath string, exact SearchCode) ([]*Snip
return snippets, nil
}

func fuzzyMatcher(keyword string, content string) bool {
return regexp.MustCompile(fmt.Sprintf(`^.*%s.*\|`, keyword)).MatchString(content)
}

func exactMatcher(keyword string, content string) bool {
return regexp.MustCompile(fmt.Sprintf(`^%s\|`, keyword)).MatchString(content)
func exactMatcher(source string, target string) bool {
return regexp.MustCompile(fmt.Sprintf(`^%s\|`, source)).MatchString(target)
}

func panicIfError(e error) {
Expand Down
31 changes: 20 additions & 11 deletions snippet/snippet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,34 +121,43 @@ var _ = Describe("Snippet", func() {
})
})

Context("when calling Search() by a keyword", func() {
It("should return all the found snippets that fuzzy match the keyword", func() {
snippet1 := Snippet{
Context("when calling Search()", func() {
var snippet1, snippet2, snippet3 Snippet

BeforeEach(func() {
snippet1 = Snippet{
Keyword: "port",
Description: "Find processes using a certain port",
Content: "lsof -i :{p}",
}

saveSnippet(snippet1, fakeFilePath)

snippet2 := Snippet{
snippet2 = Snippet{
Keyword: "port2",
Description: "Find processes using a certain port",
Content: "lsof -i :{p}",
}

saveSnippet(snippet2, fakeFilePath)

snippet3 := seedSnippet()

snippet3 = seedSnippet()
saveSnippet(snippet3, fakeFilePath)
})

found, err := Search("port", fakeFilePath)
assertSearchResult := func(searchTerm string) {
found, err := Search(searchTerm, fakeFilePath)
Expect(err).To(BeNil())

Expect(found).To(HaveLen(2))
Expect(*found[0]).To(Equal(snippet1))
Expect(*found[1]).To(Equal(snippet2))
}

It("should return all the found snippets that fuzzy match the search term", func() {
By("searching by substring of keyword")

assertSearchResult("port")

By("searching by substring of description")

assertSearchResult("find process")
})
})

Expand Down

0 comments on commit a5462b3

Please sign in to comment.