Skip to content

Commit

Permalink
Merge pull request #334 from a2xdeveloper/feature/tag-filter
Browse files Browse the repository at this point in the history
Filter snippets by tag -> pet list -t $tag #329
  • Loading branch information
RamiAwar authored Dec 7, 2024
2 parents 19068f8 + 4e0e0c1 commit 321f94e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func list(cmd *cobra.Command, args []string) error {
return err
}

if config.Flag.FilterTag != "" {
snippets.Snippets = snippets.FilterByTags(strings.Split(config.Flag.FilterTag, ","))
}

col := config.Conf.General.Column
if col == 0 {
col = column
Expand Down Expand Up @@ -82,4 +86,5 @@ func init() {
RootCmd.AddCommand(listCmd)
listCmd.Flags().BoolVarP(&config.Flag.OneLine, "oneline", "", false,
`Display snippets in one line`)
listCmd.Flags().StringVar(&config.Flag.FilterTag, "t", "", "list by specified tags as comma separated values")
}
19 changes: 19 additions & 0 deletions snippet/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os"
"slices"

Check failure on line 7 in snippet/snippet.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.14/x64/src/slices)
"sort"

"github.com/knqyf263/pet/config"
Expand Down Expand Up @@ -164,6 +165,24 @@ func (snippets *Snippets) Order() {
}
}

// FilterByTags filters snippets by tags.
func (snippets *Snippets) FilterByTags(tags []string) (filteredSnippets []SnippetInfo) {
for _, snippet := range snippets.Snippets {
if len(snippet.Tag) == 0 {
continue
}

for _, tag := range tags {
if slices.Contains(snippet.Tag, tag) {
filteredSnippets = append(filteredSnippets, snippet)
break
}
}
}

return filteredSnippets
}

func (snippets *Snippets) reverse() {
for i, j := 0, len(snippets.Snippets)-1; i < j; i, j = i+1, j-1 {
snippets.Snippets[i], snippets.Snippets[j] = snippets.Snippets[j], snippets.Snippets[i]
Expand Down
38 changes: 38 additions & 0 deletions snippet/snippet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,41 @@ func TestSaveWithMultipleSnippetFiles(t *testing.T) {
`
assert.Equal(t, want, string(data))
}

func TestFilterByTags(t *testing.T) {
snippets := &Snippets{
Snippets: []SnippetInfo{
{
Description: "Test snippet",
Command: "echo 'Hello, World!'",
Tag: []string{"test"},
Output: "Hello, World!",
},
{
Description: "Test snippet 2",
Command: "echo 'Hello, World 2!'",
Tag: []string{"test", "test2"},
Output: "Hello, World 2!",
},
{
Description: "Test snippet 3",
Command: "echo 'Hello, World 3!'",
Tag: []string{"test", "test3"},
Output: "Hello, World 3!",
},
},
}

// Filter by a single tag
filteredSnippets := snippets.FilterByTags([]string{"test"})
assert.Len(t, filteredSnippets, 3)
assert.Equal(t, "Test snippet", filteredSnippets[0].Description)
assert.Equal(t, "Test snippet 2", filteredSnippets[1].Description)

// Filter by multiple tags
filteredSnippets = snippets.FilterByTags([]string{"test", "test2"})
assert.Len(t, filteredSnippets, 3)
assert.Equal(t, "Test snippet", filteredSnippets[0].Description)
assert.Equal(t, "Test snippet 2", filteredSnippets[1].Description)
assert.Equal(t, "Test snippet 3", filteredSnippets[2].Description)
}

0 comments on commit 321f94e

Please sign in to comment.