(scripts) extended whitespace remover script

This commit is contained in:
scbj
2026-04-21 14:35:55 +02:00
committed by zegonix
parent 64d8115fbe
commit cd50caf64c
+80 -63
View File
@@ -1,105 +1,122 @@
#!/usr/bin/env bash #!/usr/bin/env bash
function remove-whitespace { 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 help
unset quiet unset silent
unset no_action
unset path_args
unset file_args
unset ffunix
unset files unset files
for arg in $@; do for arg in $@; do
case ${arg} in case ${arg} in
"-e" | "--eol" | "--end-of-line") "-u" | "--unix")
line_endings="true" ffunix="true"
continue continue
;; ;;
"-h" | "--help" | "-help") "-h" | "--help" | "-help")
help="true" help="true"
continue continue
;; ;;
"-n" | "--quiet") "-n" | "--no-action")
quiet="true" no_action="true"
continue
;;
"-s" | "--silent")
silent="true"
continue continue
;; ;;
*) ;; *) ;;
esac esac
if [[ -f "${arg}" ]]; then
grep -Iq . "${arg}" && files=(${files} "${arg}")
continue
fi
if [[ -d "${arg}" ]]; then if [[ -d "${arg}" ]]; then
files=(${files} $(find "${arg}" \ path_args+=("${arg}")
-not -path "*/.git/*" \ continue
-not -name "*.lst" \ elif [[ -f "${arg}" ]]; then
-not -name "*.map" \ file_args+=("${arg}")
-not -name "*.svd" \
-not -name "*.sym" \
-type f \
-exec grep -Iq . {} \; \
-print))
continue continue
fi fi
echo "(unrecognised argument: '${arg}')"
done done
## search current directory if no argument is provided if [[ -n "${ffunix}" ]]; then
if [[ -z "${files[@]}" ]]; then sed_cmd='s/[[:space:]]\+$//'
files=($(find . \ grep_regex=$'[[:space:]]+$'
-not -path "*/.git/*" \ else
-not -name "*.lst" \ sed_cmd='s/[[:blank:]]\+\(\r\?\)$/\1/'
-not -name "*.map" \ grep_regex=$'[[:blank:]]+\r?$'
-not -name "*.svd" \
-not -name "*.sym" \
-type f \
-exec grep -Iq . {} \; \
-print))
fi fi
if [[ -n "${dir_arg}" ]] && [[ -n "${file_arg}" ]]; then for arg in ${path_args[@]}; do
help="true" files+=($(
fi 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 if [[ -n "${help}" ]] || [[ -z "${files[@]}" ]]; then
echo "Usage: remove-whitespace [-h] [-n] path [path]" cat <<EOF
echo "" Usage: remove-whitespace [-h] [-n] path [path]
echo "Remove trailing whitespace from plain text files."
echo "Path points to either a file or directory, or both." Remove trailing whitespace from plain text files.
echo "Directories are search recursively for non binary." Path points to either a file or directory, or both.
echo "If no paths are provided, the current working" Directories are searched recursively.
echo "directory is search for plain text files." If no paths are provided, the current working
echo "" directory is searched.
echo "Options:"
echo " -e, --eol," Options:
echo " --end-of-line convert all line endings to unix style" -u, --unix convert all line endings to unix style
echo " line endings" line endings
echo " -h, --help show this help" -h, --help show this help
echo " -n, --quiet suppress output" -n, --no-action do not delete, only detect whitespace
this switch forces silent=false
-s, --silent suppress output
EOF
return 0 return 0
fi fi
unset trailing_whitespace if [[ -z "${no_action}" ]]; then
for file in ${files[@]}; do
for file in ${files[@]}; do if [[ -n "${ffunix}" ]]; then
sed -i 's/\r$//' ${file} sed -i "${sed_cmd}" ${file}
if [[ -n "$(sed -ne '/[[:blank:]]\+\r\?$/p' ${file})" ]]; then
if [[ -n "${line_endings}" ]]; then
sed -i 's/[[:blank:]]\+\(\r\?\)$//' ${file}
else else
sed -i 's/[[:blank:]]\+\(\r\?\)$/\1/' ${file} sed -i "${sed_cmd}" ${file}
fi fi
trailing_whitespace+="${file}\n" done
fi fi
done
echo "" echo ""
if [[ -z "${quiet}" ]] && if [[ -z "${silent} ${no_action}" ]] &&
[[ -n "${trailing_whitespace}" ]]; then [[ -n "${files}" ]]; then
tput setaf 1 tput setaf 1
tput smso tput smso
echo "The following files contained whitespace:" echo "The following files contained whitespace:"
tput sgr0 tput sgr0
echo -e "${trailing_whitespace}" for file in ${files[@]}; do
echo "${file}"
done
fi fi
} }