rcmdnk's blog

Brew-fileにコマンド補完を追加
COMPLETION: A Southwest Supernatural Thriller

Brew-file has been updated.

The new wrapper function allows you to use some subcommands of brew-file directly as a brew’s subcommand.

Completion functions are also implemented for both Bash and Zsh.

Sponsored Links

Completion

There are new files of etc/bash_completion.d/brew-file and share/zsh/site-functions/_brew-file.

_brew-file is just a symbolic link to brew-file.

They have the functions of the completion for brew-file command.

To enable the completion, please add the following settings for each Bash or Zsh.

  • For Bash

First, please install Bash-Completion: http://bash-completion.alioth.debian.org/ by:

$ brew install bash-completion

Then, add following settings to your .bashrc:

1
2
3
4
brew_completion=$(brew --prefix 2>/dev/null)/etc/bash_completion
if [ $? -eq 0 ] && [ -f "$brew_completion" ];then
  source $brew_completion
fi
  • For Zsh

Add following settings in your .zshrc:

1
2
3
4
5
6
brew_completion=$(brew --prefix 2>/dev/null)/share/zsh/zsh-site-functions
if [ $? -eq 0 ] && [ -d "$brew_completion" ];then
  fpath=($brew_completion $fpath)
fi
autoload -U compinit
compinit

In case you have installed zsh-completions (can be installed by brew: $ brew install zsh-completions)、 settings can be like:

1
2
3
4
5
6
7
8
for d in "/share/zsh-completions" "/share/zsh/zsh-site-functions";do
  brew_completion=$(brew --prefix 2>/dev/null)$d
  if [ $? -eq 0 ] && [ -d "$brew_completion" ];then
    fpath=($brew_completion $fpath)
  fi
done
autoload -U compinit
compinit

to enable both zsh-completions and site-functions.

brew-wrap

The above completion is done only for brew-file command. It doesn’t complete such brew file (brew + file subcommand).

etc/brew-wrap has a wrapper setting to override original brew completion. It adds file subcommand to brew, and enable a completion for brew file as same as brew-file.

1
2
3
4
# wrap brew (brew-wrap in brew-file)
if [ -f $(brew --prefix)/etc/brew-wrap ];then
  source $(brew --prefix)/etc/brew-wrap
fi

This file should be read after above completion settings, like

1
2
3
4
5
6
7
8
9
10
# Bash completion
brew_completion=$(brew --prefix 2>/dev/null)/etc/bash_completion
if [ $? -eq 0 ] && [ -f "$brew_completion" ];then
  source $brew_completion
fi

# wrap brew (brew-wrap in brew-file)
if [ -f $(brew --prefix)/etc/brew-wrap ];then
  source $(brew --prefix)/etc/brew-wrap
fi

brew-wrap wraps brew command itself to automatically update Brewfile after such brew install or brew uninstall.

Moreover, some subcommands of brew-file can be used directly as a brew’s subcommand, if the command is not in originally brew’s subcommands.

Such init or casklist are not in brew’s subcommands. Therefore

$ brew init

becomes as same as

$ brew file init

and

$ brew casklist

can be used as

$ brew file casklist
Sponsored Links
Sponsored Links

« Can't ignore unknown argument in subparser of ArgumentParser of Python even if parse_known_args is given Adding additional file usage to Brew-file »

}