(scripts) extended whitespace remover script
This commit is contained in:
@@ -1,105 +1,122 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
function remove-whitespace {
|
||||
# exclude file types
|
||||
# (ignore generated files/directories)
|
||||
exclude_paths=(
|
||||
"*/.git/*"
|
||||
)
|
||||
exclude_files=(
|
||||
"*.lst"
|
||||
"*.map"
|
||||
"*.svd"
|
||||
"*.sym"
|
||||
)
|
||||
|
||||
unset line_endings
|
||||
unset help
|
||||
unset quiet
|
||||
unset silent
|
||||
unset no_action
|
||||
unset path_args
|
||||
unset file_args
|
||||
unset ffunix
|
||||
unset files
|
||||
|
||||
for arg in $@; do
|
||||
case ${arg} in
|
||||
"-e" | "--eol" | "--end-of-line")
|
||||
line_endings="true"
|
||||
"-u" | "--unix")
|
||||
ffunix="true"
|
||||
continue
|
||||
;;
|
||||
"-h" | "--help" | "-help")
|
||||
help="true"
|
||||
continue
|
||||
;;
|
||||
"-n" | "--quiet")
|
||||
quiet="true"
|
||||
"-n" | "--no-action")
|
||||
no_action="true"
|
||||
continue
|
||||
;;
|
||||
"-s" | "--silent")
|
||||
silent="true"
|
||||
continue
|
||||
;;
|
||||
*) ;;
|
||||
esac
|
||||
|
||||
if [[ -f "${arg}" ]]; then
|
||||
grep -Iq . "${arg}" && files=(${files} "${arg}")
|
||||
continue
|
||||
fi
|
||||
if [[ -d "${arg}" ]]; then
|
||||
files=(${files} $(find "${arg}" \
|
||||
-not -path "*/.git/*" \
|
||||
-not -name "*.lst" \
|
||||
-not -name "*.map" \
|
||||
-not -name "*.svd" \
|
||||
-not -name "*.sym" \
|
||||
-type f \
|
||||
-exec grep -Iq . {} \; \
|
||||
-print))
|
||||
path_args+=("${arg}")
|
||||
continue
|
||||
elif [[ -f "${arg}" ]]; then
|
||||
file_args+=("${arg}")
|
||||
continue
|
||||
fi
|
||||
|
||||
echo "(unrecognised argument: '${arg}')"
|
||||
done
|
||||
|
||||
## search current directory if no argument is provided
|
||||
if [[ -z "${files[@]}" ]]; then
|
||||
files=($(find . \
|
||||
-not -path "*/.git/*" \
|
||||
-not -name "*.lst" \
|
||||
-not -name "*.map" \
|
||||
-not -name "*.svd" \
|
||||
-not -name "*.sym" \
|
||||
-type f \
|
||||
-exec grep -Iq . {} \; \
|
||||
-print))
|
||||
if [[ -n "${ffunix}" ]]; then
|
||||
sed_cmd='s/[[:space:]]\+$//'
|
||||
grep_regex=$'[[:space:]]+$'
|
||||
else
|
||||
sed_cmd='s/[[:blank:]]\+\(\r\?\)$/\1/'
|
||||
grep_regex=$'[[:blank:]]+\r?$'
|
||||
fi
|
||||
|
||||
if [[ -n "${dir_arg}" ]] && [[ -n "${file_arg}" ]]; then
|
||||
help="true"
|
||||
fi
|
||||
for arg in ${path_args[@]}; do
|
||||
files+=($(
|
||||
find "${arg}" \
|
||||
"${exclude_paths[@]/#/-not -path }" \
|
||||
"${exclude_files[@]/#/-not -name }" \
|
||||
-type f \
|
||||
-exec grep -EIq "${grep_regex}" {} \; \
|
||||
-print
|
||||
))
|
||||
done
|
||||
for arg in ${file_args[@]}; do
|
||||
grep -EIq "${grep_regex}" ${arg} && files+=("${arg}")
|
||||
done
|
||||
|
||||
if [[ -n "${help}" ]]; then
|
||||
echo "Usage: remove-whitespace [-h] [-n] path [path]"
|
||||
echo ""
|
||||
echo "Remove trailing whitespace from plain text files."
|
||||
echo "Path points to either a file or directory, or both."
|
||||
echo "Directories are search recursively for non binary."
|
||||
echo "If no paths are provided, the current working"
|
||||
echo "directory is search for plain text files."
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo " -e, --eol,"
|
||||
echo " --end-of-line convert all line endings to unix style"
|
||||
echo " line endings"
|
||||
echo " -h, --help show this help"
|
||||
echo " -n, --quiet suppress output"
|
||||
if [[ -n "${help}" ]] || [[ -z "${files[@]}" ]]; then
|
||||
cat <<EOF
|
||||
Usage: remove-whitespace [-h] [-n] path [path]
|
||||
|
||||
Remove trailing whitespace from plain text files.
|
||||
Path points to either a file or directory, or both.
|
||||
Directories are searched recursively.
|
||||
If no paths are provided, the current working
|
||||
directory is searched.
|
||||
|
||||
Options:
|
||||
-u, --unix convert all line endings to unix style
|
||||
line endings
|
||||
-h, --help show this help
|
||||
-n, --no-action do not delete, only detect whitespace
|
||||
this switch forces silent=false
|
||||
-s, --silent suppress output
|
||||
EOF
|
||||
|
||||
return 0
|
||||
fi
|
||||
|
||||
unset trailing_whitespace
|
||||
|
||||
for file in ${files[@]}; do
|
||||
sed -i 's/\r$//' ${file}
|
||||
if [[ -n "$(sed -ne '/[[:blank:]]\+\r\?$/p' ${file})" ]]; then
|
||||
if [[ -n "${line_endings}" ]]; then
|
||||
sed -i 's/[[:blank:]]\+\(\r\?\)$//' ${file}
|
||||
if [[ -z "${no_action}" ]]; then
|
||||
for file in ${files[@]}; do
|
||||
if [[ -n "${ffunix}" ]]; then
|
||||
sed -i "${sed_cmd}" ${file}
|
||||
else
|
||||
sed -i 's/[[:blank:]]\+\(\r\?\)$/\1/' ${file}
|
||||
sed -i "${sed_cmd}" ${file}
|
||||
fi
|
||||
trailing_whitespace+="${file}\n"
|
||||
fi
|
||||
done
|
||||
done
|
||||
fi
|
||||
|
||||
echo ""
|
||||
|
||||
if [[ -z "${quiet}" ]] &&
|
||||
[[ -n "${trailing_whitespace}" ]]; then
|
||||
if [[ -z "${silent} ${no_action}" ]] &&
|
||||
[[ -n "${files}" ]]; then
|
||||
tput setaf 1
|
||||
tput smso
|
||||
echo "The following files contained whitespace:"
|
||||
tput sgr0
|
||||
echo -e "${trailing_whitespace}"
|
||||
for file in ${files[@]}; do
|
||||
echo "${file}"
|
||||
done
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user