Skip to content

Commit

Permalink
shell: cmd-clink
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 29, 2024
1 parent c9244b5 commit ed0856f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func addCompletionCommand(targetCmd *cobra.Command) {
ActionStyledValues(
"bash", "#d35673",
"bash-ble", "#c2039a",
"cmd-clink", "#2B3436",
"elvish", "#ffd6c9",
"export", style.Default,
"fish", "#7ea8fc",
Expand Down
18 changes: 18 additions & 0 deletions example/cmd/_test/cmd-clink.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local function example_completion(word, word_index, line_state, match_builder)
args = { "example", "_carapace", "fish", "\"\"" }
for i = 2,word_index,1 do
table.insert(args, string.format("%q" ,line_state:getword(i)))
end
-- table.insert(args, word)

output = io.popen(table.concat(args, " ")):read("*a")
for line in string.gmatch(output, '[^\r\n]+') do
-- match_builder:addmatch(line)
match_builder:addmatch(string.gsub(line, '\t.*', ""))
end

return true
end

clink.argmatcher("example"):addarg({example_completion}):loop(1)

4 changes: 4 additions & 0 deletions example/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func TestBashBle(t *testing.T) {
testScript(t, "bash-ble", "./_test/bash-ble.sh")
}

func TestCmdClink(t *testing.T) {
testScript(t, "cmd-clink", "./_test/cmd-clink.lua")
}

func TestElvish(t *testing.T) {
testScript(t, "elvish", "./_test/elvish.elv")
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/carapace-sh/carapace

go 1.15
go 1.16

require (
github.com/carapace-sh/carapace-shlex v1.0.1
Expand Down
9 changes: 9 additions & 0 deletions internal/shell/cmd_clink/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cmd_clink

import (
"github.com/carapace-sh/carapace/internal/common"
)

func ActionRawValues(currentWord string, meta common.Meta, values common.RawValues) string {
return "TODO"
}
30 changes: 30 additions & 0 deletions internal/shell/cmd_clink/snippet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd_clink

import (
"fmt"

"github.com/carapace-sh/carapace/pkg/uid"
"github.com/spf13/cobra"
)

func Snippet(cmd *cobra.Command) string {
result := fmt.Sprintf(`local function %v_completion(word, word_index, line_state, match_builder)
args = { %#v, "_carapace", "fish", "\"\"" }
for i = 2,word_index,1 do
table.insert(args, string.format("%%q" ,line_state:getword(i)))
end
-- table.insert(args, word)
output = io.popen(table.concat(args, " ")):read("*a")
for line in string.gmatch(output, '[^\r\n]+') do
-- match_builder:addmatch(line)
match_builder:addmatch(string.gsub(line, '\t.*', ""))
end
return true
end
clink.argmatcher("%v"):addarg({%v_completion}):loop(1)
`, cmd.Name(), uid.Executable(), cmd.Name(), cmd.Name())
return result
}
3 changes: 3 additions & 0 deletions internal/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/carapace-sh/carapace/internal/env"
"github.com/carapace-sh/carapace/internal/shell/bash"
"github.com/carapace-sh/carapace/internal/shell/bash_ble"
"github.com/carapace-sh/carapace/internal/shell/cmd_clink"
"github.com/carapace-sh/carapace/internal/shell/elvish"
"github.com/carapace-sh/carapace/internal/shell/export"
"github.com/carapace-sh/carapace/internal/shell/fish"
Expand All @@ -33,6 +34,7 @@ func Snippet(cmd *cobra.Command, shell string) (string, error) {
shellSnippets := map[string]func(cmd *cobra.Command) string{
"bash": bash.Snippet,
"bash-ble": bash_ble.Snippet,
"cmd-clink": cmd_clink.Snippet,
"export": export.Snippet,
"fish": fish.Snippet,
"elvish": elvish.Snippet,
Expand Down Expand Up @@ -60,6 +62,7 @@ func Value(shell string, value string, meta common.Meta, values common.RawValues
shellFuncs := map[string]func(currentWord string, meta common.Meta, values common.RawValues) string{
"bash": bash.ActionRawValues,
"bash-ble": bash_ble.ActionRawValues,
"cmd-clink": cmd_clink.ActionRawValues,
"fish": fish.ActionRawValues,
"elvish": elvish.ActionRawValues,
"export": export.ActionRawValues,
Expand Down

0 comments on commit ed0856f

Please sign in to comment.