Skip to content

Commit 3a0dc16

Browse files
committed
support cmd-clink
1 parent c4fdeec commit 3a0dc16

File tree

10 files changed

+68
-5
lines changed

10 files changed

+68
-5
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Carapace-bin provides argument completion for multiple CLI commands ([full list]
1414

1515
Supported shells:
1616
- [Bash](https://www.gnu.org/software/bash/)
17+
- [Cmd](https://en.wikipedia.org/wiki/Cmd.exe) ([experimental](https://github.com/carapace-sh/carapace/issues/1107))
1718
- [Elvish](https://elv.sh/)
1819
- [Fish](https://fishshell.com/)
1920
- [Ion](https://doc.redox-os.org/ion-manual/) ([experimental](https://github.com/carapace-sh/carapace/issues/88))

cmd/carapace/cmd/invoke.go

+2
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func init() {
174174
switch c.Args[1] {
175175
case "bash",
176176
"bash-ble",
177+
"cmd-clink",
177178
"elvish",
178179
"export",
179180
"fish",
@@ -196,6 +197,7 @@ func init() {
196197
switch c.Args[1] {
197198
case "bash",
198199
"bash-ble",
200+
"cmd-clink",
199201
"elvish",
200202
"export",
201203
"fish",

cmd/carapace/cmd/lazyinit/cmd.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package lazyinit
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func CmdClink(completers []string) string {
9+
// TODO pathSnippet
10+
snippet := `local function carapace_completion(command)
11+
return function(word, word_index, line_state, match_builder)
12+
local compline = string.sub(line_state:getline(), 1, line_state:getcursor())
13+
local prog = "env CARAPACE_COMPLINE=" .. string.format("%%q", compline) .. " carapace " .. command .. " cmd-clink \"\""
14+
15+
local output = io.popen(prog):read("*a")
16+
for line in string.gmatch(output, '[^\r\n]+') do
17+
local matches = {}
18+
for m in string.gmatch(line, '[^\t]+') do
19+
table.insert(matches, m)
20+
end
21+
match_builder:addmatch({
22+
match = matches[1],
23+
display = matches[2],
24+
description = matches[3],
25+
type = "word",
26+
appendchar = matches[4],
27+
suppressappend = false
28+
})
29+
end
30+
return true
31+
end
32+
end
33+
34+
%v
35+
`
36+
argmatchers := make([]string, 0, len(completers))
37+
for _, completer := range completers {
38+
argmatchers = append(argmatchers,
39+
fmt.Sprintf(`clink.argmatcher("%v"):addarg({nowordbreakchars="'‵+;,", carapace_completion("%v")}):loop(1)`, completer, completer),
40+
fmt.Sprintf(`clink.argmatcher("%v.exe"):addarg({nowordbreakchars="'‵+;,", carapace_completion("%v")}):loop(1)`, completer, completer),
41+
)
42+
}
43+
return fmt.Sprintf(snippet, strings.Join(argmatchers, "\n"))
44+
}

cmd/carapace/cmd/lazyinit/snippet.go

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ func Snippet(shell string) string {
7171
return Bash(completerNames)
7272
case "bash-ble":
7373
return BashBle(completerNames)
74+
case "cmd-clink":
75+
return CmdClink(completerNames)
7476
case "elvish":
7577
return Elvish(completerNames)
7678
case "fish":

cmd/carapace/cmd/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func Execute(version string) error {
161161
switch shell {
162162
case "bash",
163163
"bash-ble",
164+
"cmd-clink",
164165
"elvish",
165166
"fish",
166167
"nushell",

docs/src/setup-cmd.png

56.6 KB
Loading

docs/src/setup.md

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ source <(carapace _carapace)
1313

1414
![](./setup-bash.png)
1515

16+
## Cmd
17+
18+
```lua
19+
# ~/AppData/Local/clink/carapace.lua
20+
load(io.popen('carapace _carapace cmd-clink'):read("*a"))()
21+
```
22+
23+
![](./setup-cmd.png)
24+
25+
> Needs [clink](https://chrisant996.github.io/clink/).
26+
1627
## Elvish
1728

1829
```sh

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ module github.com/carapace-sh/carapace-bin
33
go 1.23.1
44

55
require (
6-
github.com/carapace-sh/carapace v1.6.1
6+
github.com/carapace-sh/carapace v1.7.0
77
github.com/carapace-sh/carapace-bridge v1.2.3
88
github.com/carapace-sh/carapace-selfupdate v0.0.10
99
github.com/carapace-sh/carapace-shlex v1.0.1
1010
github.com/carapace-sh/carapace-spec v1.1.1
1111
github.com/pelletier/go-toml v1.9.5
1212
github.com/spf13/cobra v1.8.1
13-
github.com/spf13/pflag v1.0.5
13+
github.com/spf13/pflag v1.0.6
1414
golang.org/x/mod v0.23.0
1515
gopkg.in/ini.v1 v1.67.0
1616
gopkg.in/yaml.v3 v3.0.1

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/carapace-sh/carapace v1.6.1 h1:mh4byD5qEGzP1wv5JZ5JXdVOXrg4lIzDV0l6isl3xNE=
2-
github.com/carapace-sh/carapace v1.6.1/go.mod h1:djegtVDi/3duSAqZNU+/nCq7XtDRMRZUb5bW0O/HnEs=
1+
github.com/carapace-sh/carapace v1.7.0 h1:uJK6qvrXow6ZylbjGrU1pEyFCg0ZQ/L951GcIdl2C7U=
2+
github.com/carapace-sh/carapace v1.7.0/go.mod h1:fHdo3nEFe1QnIXxeA/Z1O9dCI83sfCsKfxrogpHfgtM=
33
github.com/carapace-sh/carapace-bridge v1.2.3 h1:lalVNRCyUqdPAJbrqkmIDKNnZYsBlo83JBsdP9WFzKM=
44
github.com/carapace-sh/carapace-bridge v1.2.3/go.mod h1:rEOS3MgzTQrCssS8WunHxPz31jZYNudcA7w1W5HrJs0=
55
github.com/carapace-sh/carapace-pflag v1.0.0 h1:uJMhl+vwEM/Eb0UdxZUuv4jo4rUAyPijkRGP5gfCuCE=

pkg/actions/env/carapace.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/carapace-sh/carapace"
55
"github.com/carapace-sh/carapace-bin/cmd/carapace/cmd/action"
66
"github.com/carapace-sh/carapace-bin/pkg/env"
7+
"github.com/carapace-sh/carapace-bridge/pkg/actions/bridge"
78
"github.com/carapace-sh/carapace/pkg/style"
89
)
910

@@ -14,6 +15,7 @@ func init() {
1415
Variables: map[string]string{
1516
// carapace
1617
"CARAPACE_COVERDIR": "coverage directory for sandbox tests",
18+
"CARAPACE_COMPLINE": "TODO", // TODO
1719
"CARAPACE_ENV": "register get-env, set-env and unset-env",
1820
"CARAPACE_HIDDEN": "show hidden commands/flags",
1921
"CARAPACE_LENIENT": "allow unknown flags",
@@ -31,6 +33,7 @@ func init() {
3133
VariableCompletion: map[string]carapace.Action{
3234
// carapace
3335
"CARAPACE_COVERDIR": carapace.ActionDirectories(),
36+
"CARAPACE_COMPLINE": bridge.ActionCarapaceBin().Split(),
3437
"CARAPACE_ENV": _bool,
3538
"CARAPACE_HIDDEN": _bool,
3639
"CARAPACE_LENIENT": _bool,
@@ -45,7 +48,6 @@ func init() {
4548
"CARAPACE_SANDBOX": carapace.ActionValues(),
4649
"CARAPACE_TOOLTIP": _bool,
4750
"CARAPACE_UNFILTERED": _bool,
48-
// carapace-bin
4951
"CARAPACE_EXCLUDES": carapace.Batch(
5052
carapace.ActionCallback(func(c carapace.Context) carapace.Action {
5153
c.Setenv(env.CARAPACE_EXCLUDES, "")

0 commit comments

Comments
 (0)