-
-
Notifications
You must be signed in to change notification settings - Fork 405
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
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d5fa860
add basic implementation of bash completion
umbynos cdefe24
update cobra to 1.0.0 because of https://github.com/spf13/cobra/pull/…
umbynos 34ac811
add support for zsh and fish, improved help messages
umbynos 7e661fa
add flag ´--no-descriptions´ to disable automatic command description…
umbynos fe15901
fix fish not supporting env variables with dash in the name
umbynos b6fd1d7
fixed zsh completion not working
umbynos 054ae56
revert "#compdef" patch
umbynos 526a2dd
add check on --no-description flag
umbynos 07f2489
add docs
umbynos aabfda3
Apply suggestions from code review
umbynos 875610f
forgot a space
umbynos a589a7b
fix fish docs
umbynos c8ed341
add test for completion
umbynos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to license@arduino.cc. | ||
|
||
package completion | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"strings" | ||
|
||
"github.com/arduino/arduino-cli/cli/errorcodes" | ||
"github.com/arduino/arduino-cli/cli/feedback" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
completionNoDesc bool //Disable completion description for shells that support it | ||
) | ||
|
||
// NewCommand created a new `version` command | ||
func NewCommand() *cobra.Command { | ||
command := &cobra.Command{ | ||
Use: "completion [bash|zsh|fish] [--no-descriptions]", | ||
ValidArgs: []string{"bash", "zsh", "fish"}, | ||
Args: cobra.ExactArgs(1), | ||
Short: "Generates completion scripts", | ||
Long: "Generates completion scripts for various shells", | ||
Example: " " + os.Args[0] + " completion bash > completion.sh\n" + | ||
" " + "source completion.sh", | ||
Run: run, | ||
} | ||
command.Flags().BoolVar(&completionNoDesc, "no-descriptions", false, "Disable completion description for shells that support it") | ||
|
||
return 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) | ||
// 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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
umbynos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`arduino-cli` supports also command-line completion (also known as *tab completion*) for basic commands. | ||
umbynos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Currently only `bash`, `zsh`, `fish` shells are supported | ||
|
||
### Before you start | ||
In order to generate the file required to make the completion work you have to [install](installation.md) the `arduino-cli` first. | ||
umbynos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Generate the completion file | ||
To generate the completion file you can use `arduino-cli completion [bash|zsh|fish] [--no-descriptions]`. | ||
By default this command will print on the standard output (the shell window) the content of the completion file. To save to an actual file use `>` redirect symbol. | ||
umbynos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Bash | ||
Use `arduino-cli completion bash > arduino-cli.sh` to generate the completion file. | ||
At this point you can moove that file in `/etc/bash_completion.d/` (root access is required) with `sudo mv arduino-cli.sh /etc/bash_completion.d/`. | ||
umbynos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
A not recommended alternative is to source the completion file in `.bashrc`. | ||
Remember to open a new shell to test the functionality | ||
umbynos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Zsh | ||
Use `arduino-cli completion zsh > _arduino-cli` to generate the completion file. | ||
At this point you can place the file in a directory listed in yout `fpath` if you have already created a directory to store your completion. | ||
umbynos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Or if you want you can create a directory, add it to your `fpath` and copy the file in it: | ||
|
||
1. `mkdir ~/completion_zsh` | ||
2. add `fpath=($HOME/completion_zsh $fpath)` at the beginning of your `.zshrc` file | ||
3. `mv _arduino-cli ~/completion_zsh/` | ||
Remember to open a new shell to test the functionality | ||
umbynos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
*N.B.* | ||
The ZSH completion is working with [Oh-My-Zsh](https://ohmyz.sh/) but not with [Prezto](https://github.com/sorin-ionescu/prezto) (the zsh completion system is working in a different way than classic zsh). But hopefully it will be fixed in the future | ||
umbynos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Fish | ||
Use `arduino-cli completion fish > arduino-cli.fish` to generate the completion file. | ||
At this point you can place the file in `~/.config/fish/completions` with `mv arduino-cli.fish ~/.config/fish/completions/` | ||
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 `--no-descriptions` flag when calling `completion` command and the generated file will not have descriptions | ||
umbynos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
*N.B.* | ||
This flags is not compatible with bash or zsh | ||
umbynos marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.