Skip to content

add basic implementation of tab completion #663

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

Merged
merged 13 commits into from
Jun 12, 2020
Merged
Prev Previous commit
Next Next commit
add check on --no-description flag
add comments regarding "hacks"
change Replacer with ReplaceAll (for readability)
  • Loading branch information
umbynos committed Jun 8, 2020
commit 526a2dd4ae8068ba116dabfee2e37337179177ed
13 changes: 11 additions & 2 deletions cli/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"os"
"strings"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/spf13/cobra"
)

Expand All @@ -45,21 +47,28 @@ func NewCommand() *cobra.Command {
}

func run(cmd *cobra.Command, args []string) {
if completionNoDesc && (args[0] == "bash" || args[0] == "zsh") {
feedback.Errorf("Error: command description is not supported by %v", args[0])
os.Exit(errorcodes.ErrGeneric)
}
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
break
case "zsh":
buf := new(bytes.Buffer)
cmd.Root().GenZshCompletion(buf)
r := strings.NewReplacer("[", "\\[", "]", "\\]") //insert escaping before [ and ]
s := r.Replace(buf.String())
// Next 3 lines are Hack, we'll wait new version of cobra with ZshV2Completion https://github.com/spf13/cobra/pull/1070
//insert escaping before [ and ]
s := strings.ReplaceAll(buf.String(), "[", "\\[")
s = strings.ReplaceAll(s, "]", "\\]")
s = strings.ReplaceAll(s, "\\[1\\]", "[1]") // revert the case
os.Stdout.WriteString(s)
break
case "fish":
buf := new(bytes.Buffer)
cmd.Root().GenFishCompletion(buf, !completionNoDesc)
// Next 2 lines are Hack, fixed here https://github.com/spf13/cobra/pull/1122
s := strings.ReplaceAll(buf.String(), "arduino-cli_comp", "arduino_cli_comp") //required because fish does not support env variables with "-" in the name
os.Stdout.WriteString(s)
break
Expand Down