From ed0856f36d30eb693e2aec158626602de53faebb Mon Sep 17 00:00:00 2001 From: rsteube Date: Sat, 28 Dec 2024 20:24:23 +0100 Subject: [PATCH] shell: cmd-clink --- command.go | 1 + example/cmd/_test/cmd-clink.lua | 18 +++++++++++++++++ example/cmd/root_test.go | 4 ++++ go.mod | 2 +- internal/shell/cmd_clink/action.go | 9 +++++++++ internal/shell/cmd_clink/snippet.go | 30 +++++++++++++++++++++++++++++ internal/shell/shell.go | 3 +++ 7 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 example/cmd/_test/cmd-clink.lua create mode 100644 internal/shell/cmd_clink/action.go create mode 100644 internal/shell/cmd_clink/snippet.go diff --git a/command.go b/command.go index 9b2e661f..e4b9a89e 100644 --- a/command.go +++ b/command.go @@ -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", diff --git a/example/cmd/_test/cmd-clink.lua b/example/cmd/_test/cmd-clink.lua new file mode 100644 index 00000000..3036a126 --- /dev/null +++ b/example/cmd/_test/cmd-clink.lua @@ -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) + diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go index 8f2f74b0..ec0cd69b 100644 --- a/example/cmd/root_test.go +++ b/example/cmd/root_test.go @@ -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") } diff --git a/go.mod b/go.mod index e97710b1..11cb928c 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/internal/shell/cmd_clink/action.go b/internal/shell/cmd_clink/action.go new file mode 100644 index 00000000..c5850213 --- /dev/null +++ b/internal/shell/cmd_clink/action.go @@ -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" +} diff --git a/internal/shell/cmd_clink/snippet.go b/internal/shell/cmd_clink/snippet.go new file mode 100644 index 00000000..67b949e4 --- /dev/null +++ b/internal/shell/cmd_clink/snippet.go @@ -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 +} diff --git a/internal/shell/shell.go b/internal/shell/shell.go index 780b291a..63e65bc2 100644 --- a/internal/shell/shell.go +++ b/internal/shell/shell.go @@ -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" @@ -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, @@ -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,