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

Add shell completion for zsh #1166 #1216

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
105 changes: 105 additions & 0 deletions completions/zsh/brew-bundle
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#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_install() {
_arguments \
'--file[read the Brewfile from this location. Use --file=- to pipe to stdin/stdout]' \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's any way to rely on any of the Homebrew/brew code to autogenerate some/all of this or at least have CI to ensure it's kept in sync?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't research that :c
I know there are some tools to automate this script creation in golang, but not sure for ruby

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, I'll try to help! Check out:

https://github.com/Homebrew/brew/blob/a56a2707075a10827b4c49b3cc51cae7bb94c2d6/completions/zsh/_brew#L4-L6

and

https://github.com/Homebrew/brew/blob/a56a2707075a10827b4c49b3cc51cae7bb94c2d6/Library/Homebrew/completions.rb

which you could maybe use for auto-generation?

Don't worry if you can't handle the e.g. GitHub Actions side: I can help with all that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, i'm not good at ruby (actually, it's my first try), but after some research of given code, i think i can make similar for brew bundle.
It requires some refactoring and i think, i would make it, if may

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tony-sol just wanted to check how things are going and what you're thinking! Thanks again for the PR!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MikeMcQuaid sorry, i've been unlucky with free time lately :c

i remember about this PR, but can't say, when could continue work on it :c

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tony-sol No worries! Just saw the rebase and wanted to check!

'--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 command completion_func
command="${line[1]}"
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 "$@"