-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added completer for tealdeer tldr client
Co-authored-by: aftix <aftix@aftix.xyz>
- Loading branch information
Showing
4 changed files
with
118 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) | ||
}) | ||
}) | ||
} |