Skip to content

Commit

Permalink
feat filter tag (#172)
Browse files Browse the repository at this point in the history
* feat filter tag

* add readme
  • Loading branch information
sadayuki-matsuno committed Nov 16, 2021
1 parent 9595a5d commit 696df2b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,14 @@ $ pet search
[ping]: ping 8.8.8.8 #network #google
```

You can exec snipet with filtering the tag

```
$ pet exec -t google
[ping]: ping 8.8.8.8 #network #google
```

## Sync
### Gist
You must obtain access token.
Expand Down
4 changes: 3 additions & 1 deletion cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func execute(cmd *cobra.Command, args []string) (err error) {
options = append(options, fmt.Sprintf("--query %s", shellescape.Quote(flag.Query)))
}

commands, err := filter(options)
commands, err := filter(options, flag.FilterTag)
if err != nil {
return err
}
Expand All @@ -47,6 +47,8 @@ func init() {
`Enable colorized output (only fzf)`)
execCmd.Flags().StringVarP(&config.Flag.Query, "query", "q", "",
`Initial value for query`)
execCmd.Flags().StringVarP(&config.Flag.FilterTag, "tag", "t", "",
`Filter tag`)
execCmd.Flags().BoolVarP(&config.Flag.Command, "command", "c", false,
`Show the command with the plain text before executing`)
}
4 changes: 3 additions & 1 deletion cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func search(cmd *cobra.Command, args []string) (err error) {
if flag.Query != "" {
options = append(options, fmt.Sprintf("--query %s", shellescape.Quote(flag.Query)))
}
commands, err := filter(options)
commands, err := filter(options, flag.FilterTag)
if err != nil {
return err
}
Expand All @@ -45,6 +45,8 @@ func init() {
`Enable colorized output (only fzf)`)
searchCmd.Flags().StringVarP(&config.Flag.Query, "query", "q", "",
`Initial value for query`)
searchCmd.Flags().StringVarP(&config.Flag.FilterTag, "tag", "t", "",
`Filter tag`)
searchCmd.Flags().StringVarP(&config.Flag.Delimiter, "delimiter", "d", "; ",
`Use delim as the command delimiter character`)
}
14 changes: 13 additions & 1 deletion cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,24 @@ func run(command string, r io.Reader, w io.Writer) error {
return cmd.Run()
}

func filter(options []string) (commands []string, err error) {
func filter(options []string, tag string) (commands []string, err error) {
var snippets snippet.Snippets
if err := snippets.Load(); err != nil {
return commands, fmt.Errorf("Load snippet failed: %v", err)
}

if 0 < len(tag) {
var filteredSnipets snippet.Snippets
for _, snipet := range snippets.Snippets {
for _, t := range snipet.Tag {
if tag == t {
filteredSnipets.Snippets = append(filteredSnipets.Snippets, snipet)
}
}
}
snippets = filteredSnipets
}

snippetTexts := map[string]snippet.SnippetInfo{}
var text string
for _, s := range snippets.Snippets {
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var Flag FlagConfig
type FlagConfig struct {
Debug bool
Query string
FilterTag string
Command bool
Delimiter string
OneLine bool
Expand Down

0 comments on commit 696df2b

Please sign in to comment.