Skip to content

Commit e8178e8

Browse files
committed
Add convenience arg in the completion to simplify use in bash
1 parent 6b294c8 commit e8178e8

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

cmd/lib.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@ import (
1818

1919
type CompletionArgs struct {
2020
Args []string `json:"args"`
21+
LastArg string `json:"last_arg"`
2122
CurrentWord string `json:"current_word"`
2223
}
2324

25+
func NewCompletionArgs(args []string, currentWord string) CompletionArgs {
26+
return CompletionArgs{
27+
Args: args,
28+
LastArg: args[len(args)-1],
29+
CurrentWord: currentWord,
30+
}
31+
}
32+
2433
func ValidArgsFunctionForScripts(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
2534
config := NewConfig()
2635
rootDir := config.RootDir()
@@ -68,7 +77,7 @@ func ValidArgsFunctionForScripts(cmd *cobra.Command, args []string, toComplete s
6877
// Execute the joint path as a shell script
6978
completionFlag := []string{"--completion"}
7079
cmd := exec.Command(joint, completionFlag...)
71-
envArg := CompletionArgs{Args: args, CurrentWord: toComplete}
80+
envArg := NewCompletionArgs(args, toComplete)
7281
c, err := json.Marshal(envArg)
7382
if err != nil {
7483
log.Fatal(err)

test/tome-cli.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ for await (let [executable, fn] of [["tome-cli", tome], ["wrapper.sh", wrapper]]
108108
}
109109

110110
Deno.test(`tome-cli: TOME_COMPLETION passed through as env`, async function (t): Promise<void> {
111-
const { code, lines, executable } = await tome(`__complete exec folder test-env-injection ""`);
111+
const { code, lines, executable } = await tome(`__complete exec folder test-env-injection "a"`);
112112
assertEquals(code, 0);
113113
await assertEquals(lines.filter(l => l.startsWith("TOME_COMPLETION")).sort(), [
114-
'TOME_COMPLETION={"args":["folder","test-env-injection"],"current_word":""}',
114+
'TOME_COMPLETION={"args":["folder","test-env-injection"],"last_arg":"test-env-injection","current_word":"a"}',
115115
].sort());
116116
})

0 commit comments

Comments
 (0)