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

mage completions for flags, targets with descriptions #40

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 19 additions & 4 deletions completions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@
Currently upstream mage doesn't support auto completions but since those are implemented separately from the binary itself it's not a problem.

### ZSH
Copy autocomplete script into $fpath:
Copy autocomplete script into your `$fpath` where applicable, e.g. on Linux:

```sh
cp mage.zsh /usr/share/zsh/site-functions/_mage
```

#### Faster autocomplete

The autocompletion utilizes `mage -l` for target definitions and descriptions. The binary is cached but hitting tab still has a second or so
delay for each refresh. To speed up things, you can add following to your `.zshrc` file:

```
zstyle ':completions:*:mage:*' hash-fast true
```

This enables the Mage's fast hashing for files for the autocompletion.

### Next

Start a new shell and auto completions should work properly.

## TODO:
- [ ] bash completion
- [ ] completion for flags
- [ ] completion for default commands
- [ ] description for each command
- [x] completion for flags
- [x] completion for default commands
- [x] description for each command
- [ ] handle non-mage directories without error
- [ ] upstream pr
62 changes: 55 additions & 7 deletions completions/mage.zsh
Original file line number Diff line number Diff line change
@@ -1,14 +1,62 @@
#compdef mage

local curcontext="$curcontext" state line _opts ret=1
local curcontext="$curcontext" state line ret=1
typeset -A opt_args
local magepath="magefiles"
local -a targets

_arguments -C '*: :->targets' && ret=0
_arguments -C \
'-clean[clean out old generated binaries from CACHE_DIR]' \
'-compile[output a static binary to the given path]:compilepath:->compilepath' \
'-init[create a starting template if no mage files exist]' \
'-h[show help]' \
'-l[list mage targets in this directory]' \
'-d[directory to read magefiles from (default "." or "magefiles" if exists)]:magepath:->magepath' \
'-debug[turn on debug messages]' \
'-f[force recreation of compiled magefile]' \
'-goarch[sets the GOARCH for the binary created by -compile (default: current arch)]' \
'-gocmd[use the given go binary to compile the output (default: "go")]' \
'-goos[sets the GOOS for the binary created by -compile (default: current OS)]' \
'-ldflags[sets the ldflags for the binary created by -compile (default: "")]' \
'-h[show description of a target]' \
'-keep[keep intermediate mage files around after running]' \
'-t[timeout in duration parsable format (e.g. 5m30s)]' \
'-v[show verbose output when running mage targets]' \
'-w[working directory where magefiles will run (default -d value)]' \
'*: :->trg'

case $state in
targets)
_values "mage target" \
$(mage | awk 'FNR > 1 {print $1}') && \
ret=0
(( $+opt_args[-d] )) && magepath=$opt_args[-d]

zstyle ':completion:*:mage:*' list-grouped false
zstyle -s ':completion:*:mage:*' hash-fast hash_fast false

_get_targets() {
# check if magefile exists
[[ ! -f "$magepath/mage.go" ]] && return 1

local IFS=$'\n'
targets=($(MAGEFILE_HASHFAST=$hash_fast mage -d $magepath -l | awk 'FNR > 1 {
target = $1;
gsub(/:/, "\\:", target);
gsub(/^ +| +$/, "", $0);

description = substr($0, length(target)+1);
gsub(/^ +| +$/, "", description);

print target ":" description;
}'))
}

case "$state" in
trg)
_get_targets || ret=1
_describe 'mage' targets && ret=0
;;
compilepath)
_path_files && ret=0
;;
magepath)
_path_files -/ && ret=0
;;
esac

Expand Down