Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter auto complete options #133

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions internal/frontend/default-frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,20 @@ func (self *defaultFrontend) addExecutableCommands() {
}
})
}
output, err := self.executeValidArgsOfCommand(group, name, originalArgs)
output, err := self.executeValidArgsOfCommand(group, name, originalArgs, toComplete)
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
parts := strings.Split(output, "\n")
if len(parts) > 0 {
if strings.HasPrefix(parts[0], "#") { // skip the first control line, for further controls
// filter the parts that starts with toComplete
filteredParts := []string{}
for idx, p := range parts {
if strings.HasPrefix(p, toComplete) || (idx == 0 && strings.HasPrefix(p, "#")) {
filteredParts = append(filteredParts, p)
}
}
if len(filteredParts) > 0 {
if strings.HasPrefix(filteredParts[0], "#") { // skip the first control line, for further controls
// the first line starting with # is the control line, it controls the completion behavior when the return body is empty
shellDirective := cobra.ShellCompDirectiveNoFileComp
switch strings.TrimSpace(strings.TrimLeft(parts[0], "#")) {
Expand All @@ -185,9 +192,9 @@ func (self *defaultFrontend) addExecutableCommands() {
case "no-file-completion":
shellDirective = cobra.ShellCompDirectiveNoFileComp
}
return parts[1:], shellDirective
return filteredParts[1:], shellDirective
}
return parts, cobra.ShellCompDirectiveNoFileComp
return filteredParts, cobra.ShellCompDirectiveNoFileComp
}
}
if len(validArgs) > 0 {
Expand Down Expand Up @@ -281,13 +288,15 @@ func (self *defaultFrontend) executeCommand(group, name string, args []string, i
}

// execute the valid args command of the cdt command
func (self *defaultFrontend) executeValidArgsOfCommand(group, name string, args []string) (string, error) {
func (self *defaultFrontend) executeValidArgsOfCommand(group, name string, args []string, toComplete string) (string, error) {
iCmd, err := self.getExecutableCommand(group, name)
if err != nil {
return "", err
}

envCtx := self.getCmdEnvContext([]string{}, []string{})
envCtx := self.getCmdEnvContext([]string{
fmt.Sprintf("%s=%s", self.appCtx.EnvVarName("TO_COMPLETE"), toComplete),
}, []string{})

_, output, err := iCmd.ExecuteValidArgsCmd(envCtx, args...)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions test/integration/test-auto-complete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ else
exit 1
fi

echo "> test dynamic argument auto-complete with prefix filter"
RESULT=$($CL_PATH __complete greeting saybonjour Jo)
echo "$RESULT" | grep -q "John"
if [ $? -eq 0 ]; then
echo "OK"
else
echo "KO - should auto-complete dynamic arguments"
exit 1
fi
echo "$RESULT" | grep -q "Kate"
if [ $? -eq 0 ]; then
echo "KO - should only return options starts with the prefix"
exit 1
else
echo "OK"
fi
echo "$RESULT" | grep -q "Jo$"
if [ $? -eq 0 ]; then
echo "OK"
else
echo "KO - should successfully inject TO_COMPLETE envrionment variable"
exit 1
fi

echo "> test flag name auto-complete"
RESULT=$($CL_PATH __complete bonjour -)
echo "$RESULT" | grep -q "\-\-lang"
Expand Down
2 changes: 2 additions & 0 deletions test/packages-src/bonjour/auto-complete.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

ECHO %*

ECHO %CL_TO_COMPLETE%

ECHO John
ECHO Kate
1 change: 1 addition & 0 deletions test/packages-src/bonjour/auto-complete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# first line print all the arguments to this script to check if all necessary arguments are passed to it
echo "$@"

echo $CL_TO_COMPLETE

echo "John"
echo "Kate"
10 changes: 9 additions & 1 deletion test/packages-src/bonjour/manifest.mf
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@
"executable": "{{.PackageDir}}/bonjour.{{if eq .Os \"windows\"}}bat{{else}}sh{{end}}",
"args": [],
"validArgsCmd": [ "{{.PackageDir}}/auto-complete.{{if eq .Os \"windows\"}}bat{{else}}sh{{end}}" ]
},
{
"name": "saybonjour2",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this new command really usef?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, this test command should be deleted. It is intended to cover a different use case, which is removed from this patch.

"type": "executable",
"group": "greeting",
"short": "print bonjour from command launcher",
"executable": "{{.PackageDir}}/bonjour.{{if eq .Os \"windows\"}}bat{{else}}sh{{end}}",
"args": [],
"validArgsCmd": [ "{{.PackageDir}}/auto-complete.{{if eq .Os \"windows\"}}bat{{else}}sh{{end}}" ]
}

]
}
Loading