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

Fix zsh completion #892

Merged
merged 5 commits into from Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 8 additions & 10 deletions cli/completion/completion.go
Expand Up @@ -16,9 +16,7 @@
package completion

import (
"bytes"
"os"
"strings"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
Expand All @@ -29,7 +27,7 @@ var (
completionNoDesc bool //Disable completion description for shells that support it
)

// NewCommand created a new `version` command
// NewCommand created a new `completion` command
func NewCommand() *cobra.Command {
command := &cobra.Command{
Use: "completion [bash|zsh|fish] [--no-descriptions]",
Expand All @@ -47,7 +45,7 @@ func NewCommand() *cobra.Command {
}

func run(cmd *cobra.Command, args []string) {
if completionNoDesc && (args[0] == "bash" || args[0] == "zsh") {
if completionNoDesc && (args[0] == "bash") {
feedback.Errorf("Error: command description is not supported by %v", args[0])
os.Exit(errorcodes.ErrGeneric)
}
Expand All @@ -56,14 +54,14 @@ func run(cmd *cobra.Command, args []string) {
cmd.Root().GenBashCompletion(os.Stdout)
break
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
if completionNoDesc {
cmd.Root().GenZshCompletionNoDesc(os.Stdout)
} else {
cmd.Root().GenZshCompletion(os.Stdout)
}
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)
cmd.Root().GenFishCompletion(os.Stdout, !completionNoDesc)
break
}
}
8 changes: 4 additions & 4 deletions docs/command-line-completion.md
Expand Up @@ -46,11 +46,11 @@ Remember to open a new shell to test the functionality.

#### Disabling command and flag descriptions

By default fish completion has command and flag description enabled by default. If you want to disable this behaviour
you can simply pass the `--no-descriptions` flag when calling `completion` command and the generated file will not have
descriptions
By default fish and zsh completion have command and flag description enabled by default. If you want to disable this
behaviour you can simply pass the `--no-descriptions` flag when calling `completion` command and the generated file will
not have descriptions

_N.B._ This flag is not compatible with bash or zsh
_N.B._ This flag is not compatible with bash

### Brew

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Expand Up @@ -38,7 +38,7 @@ require (
github.com/schollz/closestmatch v2.1.0+incompatible
github.com/segmentio/stats/v4 v4.5.3
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v1.0.0
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c
github.com/spf13/jwalterweatherman v1.0.0
github.com/spf13/viper v1.6.2
github.com/stretchr/testify v1.6.1
Expand All @@ -55,5 +55,5 @@ require (
google.golang.org/grpc v1.27.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
gopkg.in/yaml.v2 v2.2.4
gopkg.in/yaml.v2 v2.3.0
)
6 changes: 6 additions & 0 deletions go.sum
Expand Up @@ -199,6 +199,10 @@ github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/cobra v1.0.1-0.20200629195214-2c5a0d300f8b h1:grM+VdcoRu+xbzmCXM1KuH5UQGk9Lc8yCiwZZ2PKVdU=
github.com/spf13/cobra v1.0.1-0.20200629195214-2c5a0d300f8b/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c h1:/dP/1GnfVIlWnB0YDImenSmneUCw3wjyq2RMgAG1e2o=
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c/go.mod h1:aeNIJzz/GSSVlS+gpCpQWZ83BKbsoW57mr90+YthtkQ=
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
Expand Down Expand Up @@ -310,6 +314,8 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
14 changes: 8 additions & 6 deletions test/test_completion.py
Expand Up @@ -34,15 +34,15 @@ def test_completion_zsh(run_command):
assert result.ok
assert result.stderr == ""
assert "#compdef _arduino-cli arduino-cli" in result.stdout
assert "function _arduino-cli" in result.stdout
assert "_arduino-cli()" in result.stdout


def test_completion_fish(run_command):
result = run_command("completion fish")
assert result.ok
assert result.stderr == ""
assert "# fish completion for arduino-cli" in result.stdout
assert "function __arduino-cli_perform_completion" in result.stdout
assert "function __arduino_cli_perform_completion" in result.stdout


def test_completion_bash_no_desc(run_command):
Expand All @@ -54,15 +54,17 @@ def test_completion_bash_no_desc(run_command):

def test_completion_zsh_no_desc(run_command):
result = run_command("completion zsh --no-descriptions")
assert not result.ok
assert result.stdout == ""
assert "Error: command description is not supported by zsh" in result.stderr
assert result.ok
assert result.stderr == ""
assert "#compdef _arduino-cli arduino-cli" in result.stdout
assert "_arduino-cli()" in result.stdout
assert "__completeNoDesc" in result.stdout


def test_completion_fish_no_desc(run_command):
result = run_command("completion fish --no-descriptions")
assert result.ok
assert result.stderr == ""
assert "# fish completion for arduino-cli" in result.stdout
assert "function __arduino-cli_perform_completion" in result.stdout
assert "function __arduino_cli_perform_completion" in result.stdout
assert "__completeNoDesc" in result.stdout