Skip to content

Commit

Permalink
Merge pull request #2346 from aftix/tealdeer_completer
Browse files Browse the repository at this point in the history
Added completer for tealdeer tldr client
  • Loading branch information
rsteube authored Apr 14, 2024
2 parents 6c4f9c1 + 085165e commit b4dc9ba
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 22 deletions.
55 changes: 55 additions & 0 deletions completers/tealdeer_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -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(),
)
}
7 changes: 7 additions & 0 deletions completers/tealdeer_completer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/carapace-sh/carapace-bin/completers/tealdeer_completer/cmd"

func main() {
cmd.Execute()
}
30 changes: 8 additions & 22 deletions completers/tldr_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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(),
)
}
48 changes: 48 additions & 0 deletions pkg/actions/tools/tldr/command.go
Original file line number Diff line number Diff line change
@@ -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...)
})
})
}

0 comments on commit b4dc9ba

Please sign in to comment.