Skip to content

Commit

Permalink
Merge pull request #2286 from rsteube/man-action
Browse files Browse the repository at this point in the history
man: extracted ActionManpages
  • Loading branch information
rsteube authored Mar 6, 2024
2 parents 19b7941 + 626a211 commit 02fc0cb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
23 changes: 2 additions & 21 deletions completers/man_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cmd

import (
"regexp"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/tools/man"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -80,24 +78,7 @@ func init() {
if rootCmd.Flag("local-file").Changed {
return carapace.ActionFiles(".man")
}
return ActionManPages()
return man.ActionManPages()
}),
)
}

func ActionManPages() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return carapace.ActionExecCommand("man", "-k", c.Value)(func(output []byte) carapace.Action {
r := regexp.MustCompile(`^(?P<name>.*) \(\d+\) +- (?P<description>.*)$`)

vals := make([]string, 0)
for _, line := range strings.Split(string(output), "\n") {
if r.MatchString(line) {
matches := r.FindStringSubmatch(line)
vals = append(vals, matches[1], matches[2])
}
}
return carapace.ActionValuesDescribed(vals...)
})
})
}
29 changes: 29 additions & 0 deletions pkg/actions/tools/man/manpage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package man

import (
"regexp"
"strings"

"github.com/rsteube/carapace"
)

// ActionManPages completes manpages
//
// git (the stupid content tracker)
// git-add (Add file contents to the index)
func ActionManPages() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
return carapace.ActionExecCommand("man", "-k", c.Value)(func(output []byte) carapace.Action {
r := regexp.MustCompile(`^(?P<name>.*) \(\d+\) +- (?P<description>.*)$`)

vals := make([]string, 0)
for _, line := range strings.Split(string(output), "\n") {
if r.MatchString(line) {
matches := r.FindStringSubmatch(line)
vals = append(vals, matches[1], matches[2])
}
}
return carapace.ActionValuesDescribed(vals...)
})
})
}

0 comments on commit 02fc0cb

Please sign in to comment.