-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
command.go
108 lines (97 loc) · 2.52 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
package carapace
import (
"fmt"
"io"
"os"
"strings"
"github.com/rsteube/carapace/internal/uid"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
)
func addCompletionCommand(cmd *cobra.Command) {
for _, c := range cmd.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
}
if s, err := complete(cmd.Parent(), args); err != nil {
fmt.Fprintln(io.MultiWriter(cmd.OutOrStderr(), LOG.Writer()), err.Error())
} else {
fmt.Fprintln(io.MultiWriter(cmd.OutOrStdout(), LOG.Writer()), s)
}
},
FParseErrWhitelist: cobra.FParseErrWhitelist{
UnknownFlags: true,
},
DisableFlagParsing: true,
}
cmd.AddCommand(carapaceCmd)
Carapace{carapaceCmd}.PositionalCompletion(
ActionStyledValues(
"bash", "#d35673",
"bash-ble", "#c2039a",
"elvish", "#ffd6c9",
"export", style.Default,
"fish", "#7ea8fc",
"ion", "#0e5d6d",
"nushell", "#29d866",
"oil", "#373a36",
"powershell", "#e8a16f",
"spec", style.Default,
"tcsh", "#412f09",
"xonsh", "#a8ffa9",
"zsh", "#efda53",
),
ActionValues(cmd.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)
return ActionExecCommand(uid.Executable(), args...)(func(output []byte) Action {
if string(output) == "" {
return ActionValues()
}
return ActionImport(output)
})
}),
)
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(),
)
}