From a5cff0d2f60779d42b9d250c67c0d7d364d956d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Qui=C3=B1ones?= Date: Fri, 2 Feb 2024 23:50:52 -0500 Subject: [PATCH] feat: improved algorithm to search notes --- internal/cmd/cat.go | 6 +++--- internal/cmd/mod.go | 13 +++---------- internal/cmd/new.go | 7 +------ internal/cmd/rm.go | 9 ++------- internal/cmd/tag.go | 4 ++-- internal/note/util.go | 30 ++++++++++++++++-------------- 6 files changed, 27 insertions(+), 42 deletions(-) diff --git a/internal/cmd/cat.go b/internal/cmd/cat.go index 81964cf..1d06888 100644 --- a/internal/cmd/cat.go +++ b/internal/cmd/cat.go @@ -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) diff --git a/internal/cmd/mod.go b/internal/cmd/mod.go index a284d3a..0306cff 100644 --- a/internal/cmd/mod.go +++ b/internal/cmd/mod.go @@ -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 } diff --git a/internal/cmd/new.go b/internal/cmd/new.go index b7cc979..43dff75 100644 --- a/internal/cmd/new.go +++ b/internal/cmd/new.go @@ -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 } diff --git a/internal/cmd/rm.go b/internal/cmd/rm.go index 605a558..b5d30ec 100644 --- a/internal/cmd/rm.go +++ b/internal/cmd/rm.go @@ -56,12 +56,7 @@ 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 } @@ -69,7 +64,7 @@ func (c *RmCmd) Main() cobra.PositionalArgs { maxSize += note.Size() tags = append(tags, note.Tag) - keys = append(keys, key) + keys = append(keys, note.Key) } if !c.yes { diff --git a/internal/cmd/tag.go b/internal/cmd/tag.go index af16a9b..133fc51 100644 --- a/internal/cmd/tag.go +++ b/internal/cmd/tag.go @@ -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])) } } diff --git a/internal/note/util.go b/internal/note/util.go index ca1932d..715bbc8 100644 --- a/internal/note/util.go +++ b/internal/note/util.go @@ -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" ) @@ -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) @@ -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 } @@ -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 }