Skip to content

Commit

Permalink
Add zsh shell completion
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-sol committed Jun 12, 2023
1 parent 6e31556 commit 4a99ca5
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.idea
.vscode

Brewfile
Brewfile.lock.json
Gemfile.lock
Expand Down
131 changes: 131 additions & 0 deletions completions/zsh/brew-bundle
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#compdef brew
#autoload

__brew_bundle_commands() {
local -a commands
commands=(
'-h:Show help'
'--help:Show help'
'install:Install and upgrade (by default) all dependencies from the Brewfile'
'dump:Write all installed casks/formulae/images/taps into a Brewfile in the current directory'
'cleanup:Uninstall all dependencies not listed from the Brewfile'
'check:Check if all dependencies are installed from the Brewfile'
'list:List all dependencies present in the Brewfile'
'exec:Run an external command in an isolated build environment based on the Brewfile dependencies'
)
_describe -t commands 'commands' commands
}

__brew_bundle_expand_alias()
{
local command_or_alias="$1"
local -A aliases
aliases=(
setup install
set install
inst install
ins install
i install
du dump
d dump
clean cleanup
cl cleanup
rm cleanup
ch check
ls list
l list
ex exec
e exec
)
command="${aliases[$command_or_alias]:-$command_or_alias}"
print "${command}"
}

_brew_bundle_install() {
_arguments \
'--file[read the Brewfile from this location. Use --file=- to pipe to stdin/stdout]' \
'--global[read the Brewfile from ~/.Brewfile]' \
'-v[print output from commands as they are run]' \
'--verbose[print output from commands as they are run]' \
"--no-upgrade[don't run brew upgrade on outdated dependencies]" \
"--no-lock[don't output a Brewfile.lock.json]" \
'--cleanup[perform cleanup operation, same as running cleanup --force]' \
'--zap[cleanup casks using the zap command instead of uninstall]'
}

_brew_bundle_dump() {
_arguments \
'--file[read the Brewfile from this location. Use --file=- to pipe to stdin/stdout]' \
'--global[read the Brewfile from ~/.Brewfile]' \
'-f[overwrite an existing Brewfile]' \
'--force[overwrite an existing Brewfile]' \
'--describe[add a description comment above each line, unless the dependency does not have a description]' \
"--no-restart[don't add restart_service to formula lines]"
}

_brew_bundle_cleanup() {
_arguments \
'--file[read the Brewfile from this location. Use --file=- to pipe to stdin/stdout]' \
'--global[read the Brewfile from ~/.Brewfile]' \
'-f[actually perform cleanup operations]' \
'--force[actually perform cleanup operations]' \
'--zap[cleanup casks using the zap command instead of uninstall]'
}

_brew_bundle_check() {
_arguments \
'--file[read the Brewfile from this location. Use --file=- to pipe to stdin/stdout]' \
'--global[read the Brewfile from ~/.Brewfile]'
}

_brew_bundle_list() {
_arguments \
'--file[read the Brewfile from this location. Use --file=- to pipe to stdin/stdout]' \
'--global[read the Brewfile from ~/.Brewfile]' \
'--all[list all dependencies]' \
'--formula[list Homebrew dependencies]' \
'--brews[list Homebrew dependencies]' \
'--cask[list Homebrew Cask dependencies]' \
'--casks[list Homebrew Cask dependencies]' \
'--tap[list tap dependencies]' \
'--taps[list tap dependencies]' \
'--mas[list Mac App Store dependencies]' \
'--whalebrew[list Whalebrew dependencies]' \
'--vscode[list VSCode extensions]'
}

_brew_bundle_exec() {
_arguments \
'--file[read the Brewfile from this location. Use --file=- to pipe to stdin/stdout]' \
'--global[read the Brewfile from ~/.Brewfile]'
}

_brew_bundle_() {
return 1
}

_brew_bundle() {
local state line ret=1

_arguments -C \
'1: :->command' \
'*::arg:->args' && return 0

case $state in
command)
__brew_bundle_commands ;;
args)
local raw_command command completion_func
raw_command="${line[1]}"
command=$(__brew_bundle_expand_alias "$raw_command")
completion_func="_brew_bundle_${command//-/_}"

_call_function ret "${completion_func}" && return ret

_message "a completion function is not defined for brew bundle ${command}"
return 1
;;
esac
}

_brew_bundle "$@"

0 comments on commit 4a99ca5

Please sign in to comment.