unixorn/fzf-zsh-plugin

View on GitHub
bin/fzf-brew-cask-install

Summary

Maintainability
Test Coverage
#!/usr/bin/env bash
#
# Original source: https://bluz71.github.io/2018/11/26/fuzzy-finding-in-bash-with-fzf.html

set -o pipefail
if [[ -n "$DEBUG" ]]; then
  set -x
fi

fail() {
  printf '%s\n' "$1" >&2  ## Send message to stderr. Exclude >&2 if you don't want it that way.
  exit "${2-1}"  ## Return a code specified by $2 or 1 by default.
}

has() {
  which "$@" > /dev/null 2>&1
}

if ! has brew; then
  fail "Can't find brew in your PATH"
fi

# Install or open the webpage for the selected application
# using brew cask search as input source
# and display a info quickview window for the currently marked application
fzf-brew-cask-install() {
    token=$(brew search --casks | fzf-tmux --query="$1" +m --preview 'brew cask info {}')

    if [ "x$token" != "x" ]
    then
        echo "(I)nstall or open the (h)omepage of $token"
        read -r input
        if [ "$input" = "i" ] || [ "$input" = "I" ]; then
            brew cask install "$token"
        fi
        if [ "$input" = "h" ] || [ "$input" = "H" ]; then
            brew cask home "$token"
        fi
    fi
}
# shellcheck disable=SC2068
fzf-brew-install $@