Skip to content

Commit

Permalink
feat: Add Bash completion for Homebrew aliases
Browse files Browse the repository at this point in the history
Add Bash completion support for Homebrew aliases:
https://github.com/Homebrew/homebrew-aliases

Unfortunately I'm not familiar enough neither with Ruby, nor with Fish/Zsh 🤷🏻

Fixes Homebrew/homebrew-aliases#91
  • Loading branch information
yermulnik committed Nov 28, 2024
1 parent 84823d8 commit d74b92e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Library/Homebrew/completions/bash.erb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ __brew_complete_commands() {

local cur="${COMP_WORDS[COMP_CWORD]}"
local cmds
local -a cmd_aliases

if [[ -n ${__HOMEBREW_COMMANDS} ]]
then
Expand All @@ -122,6 +123,8 @@ __brew_complete_commands() {
then
cmds="$(< "${HOMEBREW_REPOSITORY}/completions/internal_commands_list.txt")"
fi
while read -r alias; do cmd_aliases+=("${alias}"); done < <(compgen -W "$(__brew_list_aliases)")
[[ -n ${cmd_aliases[*]+"${cmd_aliases[*]}"} ]] && cmds+=" ${cmd_aliases[*]} alias unalias"
while read -r line; do COMPREPLY+=("${line}"); done < <(compgen -W "${cmds}" -- "${cur}")
export __HOMEBREW_COMMANDS=${cmds}
}
Expand All @@ -131,6 +134,26 @@ __brew_complete_files() {
command -v compopt &> /dev/null && compopt -o default
}

# https://github.com/Homebrew/homebrew-aliases
__brew_list_aliases() {
local aliases_dir="${HOME}/.brew-aliases"
local pattern="^# alias: brew ([[:alnum:]-]+)$"
local -a aliases

[[ ! -d ${aliases_dir} ]] && return

for file in "${aliases_dir}"/*; do
[[ ! -f ${file} ]] && continue
while read -r line; do
if [[ ${line} =~ ${pattern} ]]; then
aliases+=("${BASH_REMATCH[1]}")
break
fi
done < "${file}"
done
[[ -n ${aliases[*]+"${aliases[*]}"} ]] && echo "${aliases[@]}"
}

<%= completion_functions.join("\n") %>

_brew() {
Expand Down

0 comments on commit d74b92e

Please sign in to comment.