Skip to content

Commit

Permalink
feat: improved algorithm to search notes
Browse files Browse the repository at this point in the history
  • Loading branch information
luisnquin committed Feb 3, 2024
1 parent b594659 commit a5cff0d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 42 deletions.
6 changes: 3 additions & 3 deletions internal/cmd/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ func (c CatCmd) Main() cobra.PositionalArgs {
for i, arg := range args {
c.log.Trace().Msgf("searching key or tag '%s', %d/%d", arg, i+1, nbOfArgs)

key, err := note.SearchByPrefix(arg, c.data)
nt, err := note.Search(c.data, arg)
if err != nil {
c.log.Err(err).Msgf("an error occurred while searching key/tag '%s", arg)

return err
}

note := c.data.Notes[key]
note := c.data.Notes[nt.Key]

c.log.Trace().Str("key", key).Str("tag", note.Tag).Send()
c.log.Trace().Str("key", nt.Key).Str("tag", note.Tag).Send()
c.log.Trace().Msg("sending note content to stdout...")

fmt.Fprintln(os.Stdout, note.Content)
Expand Down
13 changes: 3 additions & 10 deletions internal/cmd/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,11 @@ func (c *ModCmd) Main() cobra.PositionalArgs {
case len(args) == 1:
c.log.Trace().Str("key/tag provided", args[0]).Send()

key, err := note.SearchByPrefix(args[0], c.data)
if err != nil {
c.log.Err(err).Str("arg", args[0]).Msg("error with the argument supplied")

return err
}

c.log.Trace().Str("key found", key).Send()
var err error

nt, err = notesRepo.Get(key)
nt, err = note.Search(c.data, args[0])
if err != nil {
c.log.Err(err).Msg("unexpected error trying to get a previously found note")
c.log.Err(err).Str("arg", args[0]).Msg("error with the argument supplied")

return err
}
Expand Down
7 changes: 1 addition & 6 deletions internal/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,7 @@ func (c *NewCmd) Main() cobra.PositionalArgs {
defer os.Remove(path)

if c.from != "" {
key, err := note.SearchByPrefix(c.from, c.data)
if err != nil {
return err
}

note, err := notesRepo.Get(key)
note, err := note.Search(c.data, c.from)
if err != nil {
return err
}
Expand Down
9 changes: 2 additions & 7 deletions internal/cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,15 @@ func (c *RmCmd) Main() cobra.PositionalArgs {
maxSize := 0

for _, arg := range args {
key, err := note.SearchByPrefix(arg, c.data)
if err != nil {
return err
}

note, err := repo.Get(key)
note, err := note.Search(c.data, arg)
if err != nil {
return err
}

maxSize += note.Size()

tags = append(tags, note.Tag)
keys = append(keys, key)
keys = append(keys, note.Key)
}

if !c.yes {
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func (c *TagCmd) Main() cobra.PositionalArgs {
return fmt.Errorf("tag %s is not valid: %w", args[1], err)
}

key, err := note.SearchByPrefix(args[0], c.data)
nt, err := note.Search(c.data, args[0])
if err != nil {
return err
}

return notesRepo.Update(key, note.WithTag(args[1]))
return notesRepo.Update(nt.Key, note.WithTag(args[1]))
}
}
30 changes: 16 additions & 14 deletions internal/note/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/luisnquin/nao/v3/internal/data"
"github.com/luisnquin/nao/v3/internal/models"
"github.com/luisnquin/nao/v3/internal/utils"
)

Expand All @@ -17,7 +18,7 @@ func SearchKeyTagsByPrefix(prefix string, data *data.Buffer) []string {
}

if strings.HasPrefix(key, prefix) {
if len(key) >= 10 {
if len(key) >= 10 { //nolint:gomnd
results = append(results, key[:10])
} else {
results = append(results, key)
Expand All @@ -28,23 +29,24 @@ func SearchKeyTagsByPrefix(prefix string, data *data.Buffer) []string {
return results
}

func SearchByPrefix(prefix string, data *data.Buffer) (string, error) {
var result string
func Search(data *data.Buffer, searchTerm string) (models.Note, error) {
var result models.Note

// We look for the pattern most similar to the available keys/tags
for key, note := range data.Notes {
if strings.HasPrefix(note.Tag, prefix) && len(note.Tag) > len(result) ||
strings.HasPrefix(key, prefix) && len(key) > len(result) {
result = key
if note.Tag == searchTerm {
return note, nil
}

if note.Tag == prefix || key == prefix {
break
}
tagLike := strings.HasPrefix(note.Tag, searchTerm) && len(note.Tag) > len(result.Key)
keyLike := strings.HasPrefix(key, searchTerm) && len(key) > len(result.Key)

if tagLike || keyLike {
result = note
}
}

// Your last bullet, I think
if result != "" {
if result.Key != "" {
return result, nil
}

Expand All @@ -54,10 +56,10 @@ func SearchByPrefix(prefix string, data *data.Buffer) (string, error) {
opts = append(opts, n.Tag)
}

bestMatch := utils.CalculateNearestString(opts, prefix)
bestMatch := utils.CalculateNearestString(opts, searchTerm)
if bestMatch != "" {
return "", fmt.Errorf("key not found, did you mean '%s'?", bestMatch)
return models.Note{}, fmt.Errorf("key not found, did you mean '%s'?", bestMatch)
}

return "", ErrNoteNotFound
return models.Note{}, ErrNoteNotFound
}

0 comments on commit a5cff0d

Please sign in to comment.