Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 111 lines (104 sloc) 2.52 KB
#!/bin/zsh
# Stolen from: https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
POSITIONAL=()
AG_PARAM=()
SEARCH_PLACES=()
NVIM_PARAM=()
JUST_FILES=0
CAT_FILES=0
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-g)
AG_PARAM+=("$2")
shift # past argument
shift # past value
;;
-sp)
SEARCH_PLACES+=("$2")
shift # past argument
shift # past value
;;
-v)
NVIM_PARAM+=("$2")
shift # past argument
shift # past value
;;
-s)
SEARCH_VAL="$2"
shift # past argument
shift # past value
;;
-f)
JUST_FILES=1
shift # past argument
;;
-nf)
JUST_FILES=2
shift # past argument
;;
-l)
JUST_FILES=3
shift # past argument
;;
-c)
CAT_FILES=1
shift # past argument
;;
-p)
CAT_FILES=2
shift # past argument
;;
--help)
echo "====== HELP ======"
echo "PARA | DESC"
echo "-g : Ag param"
echo "-v : Nvim param"
echo "-sp : File or directory to search. Prefer over just using -g"
echo "-s : Search val"
echo "-f : Edit just the file names, not the content"
echo "-nf : No editing"
echo "-l : Edit lines"
echo "-c : Echo out file names at the end"
echo "-p : Pastebin file names at the end"
exit
;;
*) # unknown option
if [[ -f "$1" || -d "$1" ]]; then
SEARCH_PLACES+=("$1")
else
POSITIONAL+=("$1") # save it in an array for later
printf "Ignoring option \`$1\`\n"
fi
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
FILES="$(ag "${AG_PARAM[@]}" -- "$SEARCH_VAL" "${SEARCH_PLACES[@]}")"
if [[ "${#SEARCH_PLACES[@]}" -eq "1" && -f "${SEARCH_PLACES[1]}" ]]; then
FILES="$(printf "%s\n" "$FILES" \
| sed "s/^/$(printf "%s\n" ${SEARCH_PLACES[1]} \
| sed "s/\//\\\AAA\//g" | sed "s/AAA//g"):/")"
fi
case $JUST_FILES in
0)
nvim -s <(printf "%s\n" "/$SEARCH_VAL") \
"${NVIM_PARAM[@]}" \
$(cut -d":" -f1 <<<"$FILES")
;;
1)
nvim <(cut -d":" -f1-2 <<<"$FILES")
;;
3)
nvim <(<<<${FILES})
;;
esac
case $CAT_FILES in
1)
cat <(<<<${FILES})
;;
2)
cat <(<<<${FILES}) | pastebinit
;;
esac
You can’t perform that action at this time.