Skip to content

Commit

Permalink
added spec generation
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 24, 2022
1 parent 41fac23 commit a765ff6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
12 changes: 11 additions & 1 deletion carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"log"
"os"
"sort"
"strings"

"github.com/rsteube/carapace/internal/common"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/rsteube/carapace/internal/shell/nushell"
"github.com/rsteube/carapace/internal/shell/oil"
"github.com/rsteube/carapace/internal/shell/powershell"
"github.com/rsteube/carapace/internal/shell/spec"
"github.com/rsteube/carapace/internal/shell/tcsh"
"github.com/rsteube/carapace/internal/shell/xonsh"
"github.com/rsteube/carapace/internal/shell/zsh"
Expand Down Expand Up @@ -135,14 +137,21 @@ func (c Carapace) Snippet(shell string) (string, error) {
"nushell": nushell.Snippet,
"oil": oil.Snippet,
"powershell": powershell.Snippet,
"spec": spec.Snippet,
"tcsh": tcsh.Snippet,
"xonsh": xonsh.Snippet,
"zsh": zsh.Snippet,
}
if s, ok := shellSnippets[shell]; ok {
return s(c.cmd.Root()), nil
}
return "", fmt.Errorf("expected 'bash', bash-ble, 'elvish', 'fish', 'ion', 'nushell', 'oil', 'powershell', 'tcsh', 'xonsh' or 'zsh' [was: %v]", shell)

expected := make([]string, 0)
for key := range shellSnippets {
expected = append(expected, key)
}
sort.Strings(expected)
return "", fmt.Errorf("expected one of '%v' [was: %v]", strings.Join(expected, "', '"), shell)
}

func lookupFlag(cmd *cobra.Command, arg string) (flag *pflag.Flag) {
Expand Down Expand Up @@ -199,6 +208,7 @@ func addCompletionCommand(cmd *cobra.Command) {
"nushell", "#29d866",
"oil", "#373a36",
"powershell", "#e8a16f",
"spec", style.Default,
"tcsh", "#412f09",
"xonsh", "#a8ffa9",
"zsh", "#efda53",
Expand Down
25 changes: 25 additions & 0 deletions internal/shell/spec/snippet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Package spec provides spec file generation for use with carapace-bin
package spec

import (
"fmt"
"strings"

"github.com/rsteube/carapace/internal/uid"
"github.com/spf13/cobra"
)

// Snippet generates the spec file
func Snippet(cmd *cobra.Command) string {
replacer := strings.NewReplacer( // TODO might need more replacements
`"`, `\"`,
`'`, `\'`,
`[`, `\[`,
`]`, `\]`,
)
return fmt.Sprintf(`name: %v
description: %v
completion:
positionalany: ["$_bridge.Carapace(%v)"]
`, uid.Executable(), replacer.Replace(cmd.Short), uid.Executable())
}

0 comments on commit a765ff6

Please sign in to comment.