-
-
Save DanielFGray/031a21947092a6949d90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
usage() { | |
cat <<'HELP' | |
-s string to search | |
-t list only text files | |
-c command to execute [defaults to vim] | |
HELP | |
} | |
has() { | |
command -v "$1" &> /dev/null | |
} | |
err() { | |
echo -e "\033[1;31m$1" >&2 | |
} | |
setCmd() { | |
if has "$1" ; then | |
cmd="$1" | |
else | |
err "$1 is not a valid command" | |
exit 1 | |
fi | |
} | |
if ! has 'ag' || ! has 'fzf'; then | |
err 'requires fzf and ag installed' | |
exit 1 | |
fi | |
cmd='vim' | |
textOnly=false | |
search='' | |
textOnly=false | |
cmdopts=() | |
agopts=() | |
while getopts "hs:c:" opt; do | |
case "$opt" in | |
h) usage; exit 0 ;; | |
c) setCmd "$OPTARG" ;; | |
a) agopts+=( "$OPTARG" ) ;; | |
esac | |
done | |
if [[ -z "$search" ]]; then | |
agopts+=( ' -g ""' ) | |
else | |
agopts+=( "'$search'" ) | |
if [[ "$cmd" == 'vim' ]]; then | |
cmdopts+=( " +/$search" ) | |
fi | |
fi | |
mapfile -t files < <(ag -l ${agopts[@]} | fzf -e -m --reverse) | |
if [[ ${#files[@]} == 0 ]]; then | |
exit 0 | |
fi | |
exec $cmd ${cmdopts[@]} "${files[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment