Skip to content

Commit

Permalink
Merge pull request #1331 from rsteube/cargo-nightly-flags
Browse files Browse the repository at this point in the history
cargo: nightly flag completion
  • Loading branch information
rsteube authored Sep 30, 2022
2 parents f20e14c + f71408a commit 7c023f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions completers/cargo_completer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/pkg/actions/bridge"
"github.com/rsteube/carapace-bin/pkg/actions/tools/cargo"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -37,6 +38,7 @@ func init() {
rootCmd.Flags().BoolP("version", "V", false, "Print version info and exit")

carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"Z": cargo.ActionNightlyFlags(),
"color": carapace.ActionValues("auto", "never", "always").StyleF(style.ForKeyword),
})

Expand Down
27 changes: 27 additions & 0 deletions pkg/actions/tools/cargo/nightly.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cargo

import (
"regexp"
"strings"

"github.com/rsteube/carapace"
)

// ActionNightlyFlags completes nightly flags
//
// allow-features (Allow *only* the listed unstable features)
// avoid-dev-deps (Avoid installing dev-dependencies if possible)
func ActionNightlyFlags() carapace.Action {
return carapace.ActionExecCommand("cargo", "-Z", "help")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
re := regexp.MustCompile(`^ +-Z (?P<name>[^ ]+) +-- (?P<description>.*)$`)

vals := make([]string, 0)
for _, line := range lines {
if matches := re.FindStringSubmatch(line); matches != nil {
vals = append(vals, matches[1], matches[2])
}
}
return carapace.ActionValuesDescribed(vals...)
})
}

0 comments on commit 7c023f7

Please sign in to comment.