Skip to content

Commit

Permalink
Merge pull request #172 from hjpotter92/update/aliases
Browse files Browse the repository at this point in the history
Add completions for aliases when defined for zsh.
  • Loading branch information
garabik committed Dec 28, 2020
2 parents 61c070d + 7575159 commit 98f4830
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions grc.zsh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
if [[ "$TERM" = dumb ]] || (( ! $+commands[grc] )); then
#!/usr/bin/env zsh

if [ "$TERM" = dumb ] || (( ! $+commands[grc] ))
then
return
fi

Expand All @@ -9,6 +12,7 @@ cmds=(
blkid
cc
configure
curl
cvs
df
diff
Expand Down Expand Up @@ -71,19 +75,28 @@ cmds=(
traceroute
traceroute6
tune2fs
ulimit
uptime
vmstat
wdiff
whois
)

function setup_alias() {
local name="$1"
local path="$(which "$name")"
$name() {
grc --colour=auto $commands[$0] "$@"
}
compdef "_${name}" "$name"

This comment has been minimized.

Copy link
@mcornella

mcornella Dec 28, 2020

This is not needed and might be wrong in some cases (where the completion function for $name is not _$name). If you remove this, the function of name $name will automatically get the completion of the command $name. Try it yourself with a mock function:

$ function docker { echo "$0 $@"; }
$ docker [TAB]
app*       -- Docker Application (Docker Inc., v0.8.0)
attach     -- Attach local standard input, output, and error streams to a running container
build      -- Build an image from a Dockerfile
builder    -- Manage builds
buildx*    -- Build with BuildKit (Docker Inc., v0.2.2-10-g3f18b65-tp-docker)
commit     -- Create a new image from a container's changes
config     -- Manage Docker configs
container  -- Manage containers
context    -- Manage contexts
...

Also you can't define a variable of the name path, that conflicts with the array form of $PATH. You're also not using it anywhere.

Finally, if you're just defining the function, you don't need the setup_alias function, just do this inside the for:

$cmd() {
  grc --colour=auto ${commands[$0]} "$@"
}

The curly braces in the $commands variable are important. If the ksh_arrays options is set it will error out without them.

}

# Set alias for available commands.
for cmd in $cmds ; do
if (( $+commands[$cmd] )) ; then
alias $cmd="grc --colour=auto $commands[$cmd]"
setup_alias $cmd
fi
done

# Clean up variables
unset cmds cmd

unset cmds cmd setup_alias

0 comments on commit 98f4830

Please sign in to comment.