-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcommand.go
127 lines (113 loc) · 3.15 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package carapace
import (
"fmt"
"io"
"os"
"strings"
"github.com/carapace-sh/carapace/internal/spec"
"github.com/carapace-sh/carapace/pkg/style"
"github.com/spf13/cobra"
)
func addCompletionCommand(targetCmd *cobra.Command) {
for _, c := range targetCmd.Commands() {
if c.Name() == "_carapace" {
return
}
}
carapaceCmd := &cobra.Command{
Use: "_carapace",
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
LOG.Print(strings.Repeat("-", 80))
LOG.Printf("%#v", os.Args)
if len(args) > 2 && strings.HasPrefix(args[2], "_") {
cmd.Hidden = false
}
if !cmd.HasParent() {
panic("missing parent command") // this should never happen
}
parentCmd := cmd.Parent()
if parentCmd.Annotations[annotation_standalone] == "true" {
// TODO how to handle an explicit `_carapace` command?
parentCmd.RemoveCommand(cmd) // don't complete local `_carapace` in standalone mode
}
if s, err := complete(parentCmd, args); err != nil {
fmt.Fprintln(io.MultiWriter(parentCmd.OutOrStderr(), LOG.Writer()), err.Error())
} else {
fmt.Fprintln(io.MultiWriter(parentCmd.OutOrStdout(), LOG.Writer()), s)
}
},
FParseErrWhitelist: cobra.FParseErrWhitelist{
UnknownFlags: true,
},
DisableFlagParsing: true,
}
targetCmd.AddCommand(carapaceCmd)
Carapace{carapaceCmd}.PositionalCompletion(
ActionStyledValues(
"bash", "#d35673",
"bash-ble", "#c2039a",
"cmd-clink", "#2B3436",
"elvish", "#ffd6c9",
"export", style.Default,
"fish", "#7ea8fc",
"ion", "#0e5d6d",
"nushell", "#29d866",
"oil", "#373a36",
"powershell", "#e8a16f",
"tcsh", "#412f09",
"xonsh", "#a8ffa9",
"zsh", "#efda53",
),
ActionValues(targetCmd.Root().Name()),
)
Carapace{carapaceCmd}.PositionalAnyCompletion(
ActionCallback(func(c Context) Action {
args := []string{"_carapace", "export", ""}
args = append(args, c.Args[2:]...)
args = append(args, c.Value)
executable, err := os.Executable()
if err != nil {
return ActionMessage(err.Error())
}
return ActionExecCommand(executable, args...)(func(output []byte) Action { // TODO does not work with sandbox tests for `example _carapace ...`
if string(output) == "" {
return ActionValues()
}
return ActionImport(output)
})
}),
)
specCmd := &cobra.Command{
Use: "spec",
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprint(cmd.OutOrStdout(), spec.Spec(targetCmd))
},
}
carapaceCmd.AddCommand(specCmd)
styleCmd := &cobra.Command{
Use: "style",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {},
}
carapaceCmd.AddCommand(styleCmd)
styleSetCmd := &cobra.Command{
Use: "set",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
for _, arg := range args {
if splitted := strings.SplitN(arg, "=", 2); len(splitted) == 2 {
if err := style.Set(splitted[0], splitted[1]); err != nil {
fmt.Fprint(cmd.ErrOrStderr(), err.Error())
}
} else {
fmt.Fprintf(cmd.ErrOrStderr(), "invalid format: '%v'", arg)
}
}
},
}
styleCmd.AddCommand(styleSetCmd)
Carapace{styleSetCmd}.PositionalAnyCompletion(
ActionStyleConfig(),
)
}