Feature: zsh completions and formatting target lists with gotemplates #488
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.
Short description
This pull request implements following functionalities:
MAGEFILE_HASHFAST=1 mage -l -format "{{ .Name }} {{ .Synopsis }}"
(with hashfast)-completion
command to generate a completion script that can be sourced or deployed by the user.Detailed description
This pull request implements following new features:
Support listing mage targets in a given go-template format
Context: Autocompletion scripts can be wrapped around
mage -l
(list). This syntax has whitespaces and extra strings in the output, which need to be taken into account in parsing.Idea: Implement a format handler similar to
go list -format
command that accepts go template language string. Calling thenmage -l -format <template>
will describe each target name-synopsis pair how they should be listed.Implementation:
mage -l -format "<template-string>"
command with which custom formatted output can be created. This can be then easily'ish be parsed with shell scripts to provide completion scripts for targets. E.g. provided zsh shell script uses piped CSV as output and parses that with awk.Generate completion scripts
Context: Many golang tools nowadays support creation and sourcing autocompletion scripts. To keep the implementation simple,
mage
could print the completion scripts which user can then decide how to either source or deploy to their environment.Idea: Implement an extendable completion interface around which different shell formats can be extended.
Implementation:
completions/completion.go
package has been implemented with an interface which (at the moment) reads azsh
shell function script for autocompletion and prints this to the user. By default in zsh completion script, hashfast feature is disabled, but README guides users how to enable it, so hitting tab and getting results after initial compilation is much faster. Script also supports namespaces and all targets are sorted alphabetically.Demo
With dash+tab
With tab only
Enhancement ideas
go:embed
directive could be used instead of storing the completion script contents in Go files. This would require go1.16 at minimum, but could be considered later?Fixes #289