fish: fix homebrew functions

This commit is contained in:
Charles Gould 2020-02-23 22:57:40 -05:00
parent 6dd195581e
commit 6ac76c8d50
5 changed files with 61 additions and 29 deletions

View File

@ -0,0 +1,12 @@
function brew_install --description "Select packages to install"
set --local inst_pkgs $argv
if test (count $inst_pkgs) -eq 0
set inst_pkgs (brew search | eval "fzf $FZF_DEFAULT_OPTS --multi --header='[brew:install]'")
end
if test (count $inst_pkgs) -gt 0
echo "Installing: $inst_pkgs"
brew install $inst_pkgs
end
end

View File

@ -1,15 +0,0 @@
function brew_installed --description "List installed top-level formulas"
set --local __brew_pkgs ''
set --local __brew_deps ''
for __brew_pkg in (brew deps --installed)
set __brew_pkgs $__brew_pkgs (string split ':' $__brew_pkg | head -n 1)
set __brew_deps $__brew_deps (string split ' ' (string split ':' $__brew_pkg | tail -n +2))
end
for __brew_pkg in $__brew_pkgs
if not contains $__brew_pkg $__brew_deps
echo $__brew_pkg
end
end
end

View File

@ -1,7 +0,0 @@
function brew_remove --description "Select homebrew packages to remove"
set --local pkgs (brew leaves | eval "fzf $FZF_DEFAULT_OPTS --multi --header='[brew:uninstall]'")
if not test (count $pkgs) = 0
brew uninstall $pkgs
end
end

View File

@ -1,7 +0,0 @@
function brew_search --description "Find homebrew packages to install"
set --local pkgs (brew search | eval "fzf $FZF_DEFAULT_OPTS --multi --header='[brew:install]'")
if not test (count $pkgs) = 0
brew install $pkgs
end
end

View File

@ -0,0 +1,49 @@
function brew_uninstall --description "Select packages to uninstall"
set --local uninst_pkgs $argv
if test (count $uninst_pkgs) -eq 0
set uninst_pkgs (brew leaves | eval "fzf $FZF_DEFAULT_OPTS --multi --header='[brew:uninstall]'")
end
if test (count $uninst_pkgs) -gt 0
echo "Determining package dependencies..."
# Output is multiline with format "pkgname:" if no dependencies, otherwise "pkgname: dep1 dep2 dep3 ..."
set --local brew_deps_installed (string collect (brew deps --installed))
# Accumulate uninst_pkgs and their transitive dependencies
set --local uninst_pkgs_all $uninst_pkgs
for bdi in $brew_deps_installed
set --local pkg (string split ':' $bdi | head -n 1)
set --local deps (string split ' ' (string split ':' $bdi | tail -n +2))
if contains -- $pkg $uninst_pkgs
for dep in $deps
if test -n $dep
if not contains -- $dep $uninst_pkgs_all
set uninst_pkgs_all $uninst_pkgs_all $dep
end
end
end
end
end
# Don't uninstall packages that are still dependencies of other packages
set --local uninst_pkgs_final $uninst_pkgs_all
for bdi in $brew_deps_installed
set --local pkg (string split ':' $bdi | head -n 1)
set --local deps (string split ' ' (string split ':' $bdi | tail -n +2))
if not contains -- $pkg $uninst_pkgs_all
for dep in $deps
if test -n $dep
if set --local index (contains --index $dep $uninst_pkgs_final)
set --erase uninst_pkgs_final[$index]
end
end
end
end
end
echo "Uninstalling: $uninst_pkgs_final"
brew uninstall $uninst_pkgs_final
end
end