refactor ag-replace
This commit is contained in:
parent
c232c94932
commit
654fe60e3b
|
@ -13,38 +13,29 @@ input=$(sanitize "$1")
|
||||||
replace=$(sanitize "$2")
|
replace=$(sanitize "$2")
|
||||||
|
|
||||||
RED=$(printf "\033[31m")
|
RED=$(printf "\033[31m")
|
||||||
GREEN=$(printf "\033[32m")
|
|
||||||
NC=$(printf "\033[0m")
|
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
|
# Show the search/replace results
|
||||||
ag "$1" $3 -s --group --nocolor --nobreak \
|
ag "$1" $3 -s --group --color --break --stats \
|
||||||
| perl -pe "s/(?<=.)$input/\033[4m$RED$replace$NC/g" \
|
| perl -pe "s/(?<=.)$input/\033[4m$NC$RED$replace$NC/g" \
|
||||||
| perl -pe "s/^([^:]+)/$GREEN\$1$NC/g"
|
| perl -pe "s/^([0-9]+ matches$)/\n\$1/g" \
|
||||||
|
| less -XF
|
||||||
|
|
||||||
# Ask for confirmation
|
# Ask for confirmation
|
||||||
echo
|
echo
|
||||||
echo -n "Apply $num changes? (y/n) "
|
echo -n "Apply changes? (y/n) "
|
||||||
read -n1 REPLY
|
read -n1 REPLY
|
||||||
echo
|
echo
|
||||||
|
|
||||||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||||||
# Now actually do the search replace! This is destructive. No, no backups are made.
|
# Now actually do the search replace! This is destructive. No, no
|
||||||
# You better have read the earlier prompt!
|
# backups are made. You better have read the earlier prompt!
|
||||||
files=`ag "$1" $3 -s --nogroup --nocolor --files-with-matches`
|
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 "$files" | sed -e "s/^/ ✓ /g"
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo "$num changes made!"
|
echo "Done!"
|
||||||
else
|
else
|
||||||
echo "Aborted!"
|
echo "Aborted!"
|
||||||
exit 2
|
exit 2
|
||||||
|
|
Loading…
Reference in a new issue