Skip to content

Commit

Permalink
carapace: added diff command
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Feb 24, 2024
1 parent 9bd28e8 commit 13e9bf6
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 3 deletions.
169 changes: 169 additions & 0 deletions cmd/carapace/cmd/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace-bin/cmd/carapace/cmd/action"
"github.com/rsteube/carapace-bridge/pkg/actions/bridge"
shlex "github.com/rsteube/carapace-shlex"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
)

var diffCmd = &cobra.Command{
Use: "--diff",
Short: "",
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
var stdout, stderr bytes.Buffer
rootCmd := cmd.Root()
rootCmd.SetArgs(append([]string{"_carapace", "export", ""}, args...))
rootCmd.SetOut(&stdout)
rootCmd.SetErr(&stderr)
rootCmd.Execute()

var export struct {
Values []struct {
Value string `json:"value"`
Description string `json:"description,omitempty"`
Style string `json:"style,omitempty"`
}
}
if err := json.Unmarshal(stdout.Bytes(), &export); err != nil {
return err
}

command, _bridge, _ := strings.Cut(args[0], "/")
macro := "bridge.Zsh"
switch _bridge {
case "argcomplete":
macro = "bridge.Argcomplete"
case "bash":
macro = "bridge.Bash"
case "carapace":
macro = "bridge.Carapace"
case "clap":
macro = "bridge.CarapaceBin"
case "click":
macro = "bridge.Click"
case "cobra":
macro = "bridge.Cobra"
case "complete":
macro = "bridge.Complete"
case "fish":
macro = "bridge.Fish"
case "inshellisense":
macro = "bridge.Inshellisense"
case "kingpin":
macro = "bridge.Kingpin"
case "powershell":
macro = "bridge.Powershell"
case "urfavecli":
macro = "bridge.Urfavecli"
case "yargs":
macro = "bridge.Yargs"
default:
_bridge = "zsh"

}
joined := shlex.Join(args[1:])
fmt.Printf("diff --git a/%v b/carapace\n", _bridge)
fmt.Printf("--- a/%v/carapace --macro '%v([%v])' %v\n", _bridge, macro, command, joined) // TODO macro by _bridge
fmt.Printf("+++ b/carapace/carapace %v export %v %v\n", command, command, joined)
for _, v := range export.Values {
s := v.Value
if v.Description != "" {
s += fmt.Sprintf(" (%v)", v.Description)
}

switch v.Style {
case style.Red:
s = "- " + s
case style.Green:
s = "+ " + s
default:
s = " " + s
}

fmt.Println(s)
}
return nil
},
}

func init() {
carapace.Gen(diffCmd).Standalone()
diffCmd.Flags().SetInterspersed(false)

carapace.Gen(diffCmd).PositionalCompletion(
carapace.ActionMultiPartsN("/", 2, func(c carapace.Context) carapace.Action {
switch len(c.Parts) {
case 0:
return action.ActionCompleters(action.CompleterOpts{Internal: true, Spec: true})
default:
return carapace.ActionStyledValues(
"argcomplete", style.Default,
"bash", "#d35673",
"carapace", style.Default,
"clap", style.Default,
"click", style.Default,
"cobra", style.Default,
"complete", style.Default,
"fish", "#7ea8fc",
"inshellisense", style.Default,
"kingpin", style.Default,
"macro", style.Default,
"powershell", "#e8a16f",
"urfavecli", style.Default,
"yargs", style.Default,
"zsh", "#efda53",
)
}
}),
)

carapace.Gen(diffCmd).PositionalAnyCompletion(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
command, _bridge, _ := strings.Cut(c.Args[0], "/")
return carapace.Diff(
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
switch _bridge {
case "argcomplete":
return bridge.ActionArgcomplete(command)
case "bash":
return bridge.ActionBash(command)
case "carapace":
return bridge.ActionCarapace(command)
case "clap":
return bridge.ActionClap(command)
case "click":
return bridge.ActionClick(command)
case "cobra":
return bridge.ActionCobra(command)
case "complete":
return bridge.ActionComplete(command)
case "fish":
return bridge.ActionFish(command)
case "inshellisense":
return bridge.ActionInshellisense(command)
case "kingpin":
return bridge.ActionKingpin(command)
case "powershell":
return bridge.ActionPowershell(command)
case "urfavecli":
return bridge.ActionUrfavecli(command)
case "yargs":
return bridge.ActionYargs(command)
default:
return bridge.ActionZsh(command)
}
}),
bridge.ActionCarapaceBin(command),
).Shift(1)
}),
)
}
7 changes: 7 additions & 0 deletions cmd/carapace/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ var rootCmd = &cobra.Command{
case "--condition":
conditionCmd.SetArgs(args[1:])
conditionCmd.Execute()
case "--diff":
diffCmd.SetArgs(args[1:])
diffCmd.Execute()
case "--macro":
macroCmd.SetArgs(args[1:])
macroCmd.Execute()
Expand Down Expand Up @@ -179,6 +182,7 @@ func init() {
rootCmd.Flags().Bool("clear-cache", false, "clear caches")
rootCmd.Flags().Bool("codegen", false, "generate code for spec file")
rootCmd.Flags().Bool("condition", false, "list or execute condition")
rootCmd.Flags().Bool("diff", false, "diff completion")
rootCmd.Flags().BoolP("help", "h", false, "help for carapace")
rootCmd.Flags().Bool("list", false, "list completers")
rootCmd.Flags().Bool("macro", false, "list or execute macros")
Expand All @@ -191,6 +195,7 @@ func init() {
"clear-cache",
"codegen",
"condition",
"diff",
"help",
"list",
"macro",
Expand Down Expand Up @@ -223,6 +228,8 @@ func init() {
return carapace.ActionExecute(codegenCmd).Shift(1)
case "--condition":
return carapace.ActionExecute(conditionCmd).Shift(1)
case "--diff":
return carapace.ActionExecute(diffCmd).Shift(1)
case "--help":
return carapace.ActionValues()
case "--list":
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21

require (
github.com/pelletier/go-toml v1.9.5
github.com/rsteube/carapace v0.50.0
github.com/rsteube/carapace v0.50.1
github.com/rsteube/carapace-bridge v0.2.14
github.com/rsteube/carapace-shlex v0.1.2
github.com/rsteube/carapace-spec v0.15.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3v
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rsteube/carapace v0.50.0 h1:LO3ehEjcdbIx9owiyCiVfgL5A0cJPGq4X7c8V5DsA20=
github.com/rsteube/carapace v0.50.0/go.mod h1:syVOvI8e2rEEK/9aMZxfWuHvcnQK/EcnTV4roClEnLE=
github.com/rsteube/carapace v0.50.1 h1:UShTOwM08wSvFuniKtqZvmBQ3pSDuoS6QrCu2w7TLU4=
github.com/rsteube/carapace v0.50.1/go.mod h1:syVOvI8e2rEEK/9aMZxfWuHvcnQK/EcnTV4roClEnLE=
github.com/rsteube/carapace-bridge v0.2.14 h1:yl4/7PRdNdXUK2wt/jyi9J24GoLdwjTnH4GYkNxPgj4=
github.com/rsteube/carapace-bridge v0.2.14/go.mod h1:CrqtRDd1D8uT9woDXnEuer7tq8IfvR9MFAr3PuFKV7E=
github.com/rsteube/carapace-pflag v0.2.0 h1:EYqFO9Haib3NDCPqKu0VxOGi9YQBkXk1IzlHdT0M0vw=
Expand Down

0 comments on commit 13e9bf6

Please sign in to comment.