diff --git a/completers/tealdeer_completer/cmd/root.go b/completers/tealdeer_completer/cmd/root.go new file mode 100644 index 0000000000..e408f1b6c2 --- /dev/null +++ b/completers/tealdeer_completer/cmd/root.go @@ -0,0 +1,55 @@ +package cmd + +import ( + "github.com/carapace-sh/carapace" + "github.com/carapace-sh/carapace-bin/pkg/actions/os" + "github.com/carapace-sh/carapace-bin/pkg/actions/tools/tldr" + "github.com/carapace-sh/carapace-bridge/pkg/actions/bridge" + "github.com/carapace-sh/carapace/pkg/style" + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "tealdeer", + Short: "A fast TLDR client", + Long: "https://github.com/dbrgn/tealdeer", + Run: func(cmd *cobra.Command, args []string) {}, +} + +func Execute() error { + return rootCmd.Execute() +} +func init() { + carapace.Gen(rootCmd).Standalone() + + rootCmd.Flags().BoolP("clear-cache", "c", false, "Clear the local cache") + rootCmd.Flags().String("color", "", "Control whether to use color") + rootCmd.Flags().BoolP("help", "h", false, "Print help information") + rootCmd.Flags().StringP("language", "L", "", "Override the language") + rootCmd.Flags().BoolP("list", "l", false, "List all commands in the cache") + rootCmd.Flags().Bool("no-auto-update", false, "If auto update is configured, disable it for this run") + rootCmd.Flags().Bool("pager", false, "Use a pager to page output") + rootCmd.Flags().StringP("platform", "p", "", "Override the operating system") + rootCmd.Flags().BoolP("quiet", "q", false, "Suppress informational messages") + rootCmd.Flags().BoolP("raw", "r", false, "Display the raw markdown instead of rendering it") + rootCmd.Flags().StringP("render", "f", "", "Render a specific markdown file") + rootCmd.Flags().Bool("seed-config", false, "Create a basic config") + rootCmd.Flags().Bool("show-paths", false, "Show file and directory paths used by tealdeer") + rootCmd.Flags().BoolP("update", "u", false, "Update the local cache") + rootCmd.Flags().BoolP("version", "v", false, "Print the version") + + carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{ + "color": carapace.ActionValues("always", "auto", "never").StyleF(style.ForKeyword), + "language": os.ActionLanguages(), + "platform": carapace.ActionValues("linux", "macos"), + "render": carapace.ActionFiles(), + }) + + carapace.Gen(rootCmd).PositionalCompletion( + tldr.ActionCommands(), + ) + + carapace.Gen(rootCmd).PositionalAnyCompletion( + bridge.ActionCarapaceBin(), + ) +} diff --git a/completers/tealdeer_completer/main.go b/completers/tealdeer_completer/main.go new file mode 100644 index 0000000000..db651bf8c7 --- /dev/null +++ b/completers/tealdeer_completer/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/carapace-sh/carapace-bin/completers/tealdeer_completer/cmd" + +func main() { + cmd.Execute() +} diff --git a/completers/tldr_completer/cmd/root.go b/completers/tldr_completer/cmd/root.go index 493559165b..889ee060dd 100644 --- a/completers/tldr_completer/cmd/root.go +++ b/completers/tldr_completer/cmd/root.go @@ -1,10 +1,10 @@ package cmd import ( - "encoding/json" - "strings" - "github.com/carapace-sh/carapace" + "github.com/carapace-sh/carapace-bin/pkg/actions/os" + "github.com/carapace-sh/carapace-bin/pkg/actions/tools/tldr" + "github.com/carapace-sh/carapace-bridge/pkg/actions/bridge" "github.com/spf13/cobra" ) @@ -32,29 +32,15 @@ func init() { rootCmd.Flags().BoolP("version", "v", false, "show program's version number and exit") carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{ + "language": os.ActionLanguages(), "platform": carapace.ActionValues("linux", "osx", "sunos", "windows", "common"), }) carapace.Gen(rootCmd).PositionalCompletion( - ActionCommands(), + tldr.ActionCommands(), ) -} - -func ActionCommands() carapace.Action { - return carapace.ActionCallback(func(c carapace.Context) carapace.Action { - return carapace.ActionExecCommand("tldr", "--list")(func(output []byte) carapace.Action { - var commands []string - fixedOutput := strings.Replace(string(output), `'`, `"`, -1) - if err := json.Unmarshal([]byte(fixedOutput), &commands); err != nil { - return carapace.ActionMessage(err.Error()) - } else { - if len(commands) == 0 { - return carapace.ActionMessage("execute `tldr -u` first") - } else { - return carapace.ActionValues(commands...) - } - } - }) - }) + carapace.Gen(rootCmd).PositionalAnyCompletion( + bridge.ActionCarapaceBin(), + ) } diff --git a/pkg/actions/tools/tldr/command.go b/pkg/actions/tools/tldr/command.go new file mode 100644 index 0000000000..fa5a5b43ee --- /dev/null +++ b/pkg/actions/tools/tldr/command.go @@ -0,0 +1,48 @@ +package tldr + +import ( + "encoding/json" + "strings" + + "github.com/carapace-sh/carapace" +) + +func ActionCommands() carapace.Action { + return carapace.ActionExecCommand("tldr", "--help")(func(output []byte) carapace.Action { + switch { + case strings.Contains(string(output), "Python"): + return actionCommandsPython() + case strings.Contains(string(output), "tealdeer"): + return actionCommandsTealdeer() + default: + return carapace.ActionValues() + } + }) +} + +func actionCommandsPython() carapace.Action { + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return carapace.ActionExecCommand("tldr", "--list")(func(output []byte) carapace.Action { + var commands []string + fixedOutput := strings.Replace(string(output), `'`, `"`, -1) + if err := json.Unmarshal([]byte(fixedOutput), &commands); err != nil { + return carapace.ActionMessage(err.Error()) + } else { + if len(commands) == 0 { + return carapace.ActionMessage("execute `tldr -u` first") + } else { + return carapace.ActionValues(commands...) + } + } + }) + }) +} + +func actionCommandsTealdeer() carapace.Action { + return carapace.ActionCallback(func(c carapace.Context) carapace.Action { + return carapace.ActionExecCommand("tldr", "--list")(func(output []byte) carapace.Action { + lines := strings.Split(string(output), "\n") + return carapace.ActionValues(lines...) + }) + }) +}