refactor ag-replace

This commit is contained in:
Henrik Lissner 2014-09-05 14:26:55 -04:00
parent c232c94932
commit 654fe60e3b

View file

@ -13,38 +13,29 @@ input=$(sanitize "$1")
replace=$(sanitize "$2")
RED=$(printf "\033[31m")
GREEN=$(printf "\033[32m")
NC=$(printf "\033[0m")
# Do a dry run for analysis
ag "$1" -s --nocolor --nogroup > /tmp/ag-repl
num=`cat /tmp/ag-repl | wc -l | sed -e "s/ //g"`
rm -rf /tmp/ag-repl
if [ "$num" -eq 0 ]; then
echo "No results were found!"
exit 1
fi
# Show the search/replace results
ag "$1" $3 -s --group --nocolor --nobreak \
| perl -pe "s/(?<=.)$input/\033[4m$RED$replace$NC/g" \
| perl -pe "s/^([^:]+)/$GREEN\$1$NC/g"
ag "$1" $3 -s --group --color --break --stats \
| perl -pe "s/(?<=.)$input/\033[4m$NC$RED$replace$NC/g" \
| perl -pe "s/^([0-9]+ matches$)/\n\$1/g" \
| less -XF
# Ask for confirmation
echo
echo -n "Apply $num changes? (y/n) "
echo -n "Apply changes? (y/n) "
read -n1 REPLY
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Now actually do the search replace! This is destructive. No, no backups are made.
# You better have read the earlier prompt!
# Now actually do the search replace! This is destructive. No, no
# backups are made. You better have read the earlier prompt!
files=`ag "$1" $3 -s --nogroup --nocolor --files-with-matches`
out=`echo "$files" | xargs perl -pi -e "s/$input/$replace/g"`
echo "$files" | xargs perl -pi -e "s/$input/$replace/g"
echo "$files" | sed -e "s/^/ ✓ /g"
echo
echo "$num changes made!"
echo "Done!"
else
echo "Aborted!"
exit 2