diff options
| author | Robert Günzler <r@gnzler.io> | 2022-11-18 17:43:00 +0100 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2022-11-18 18:38:20 +0100 |
| commit | 65eeb575d609a335baa00ec2502ab44e7bdc2a3d (patch) | |
| tree | a46f8ef374c233971bcc96c12c08357e5cb47891 /bin/saferm | |
| parent | d6afa4cb7fbd919989b12277f1e67bbf883a2853 (diff) | |
bin/saferm: refactor
Diffstat (limited to 'bin/saferm')
| -rwxr-xr-x | bin/saferm | 49 |
1 files changed, 35 insertions, 14 deletions
diff --git a/bin/saferm b/bin/saferm index 467fec2..10c59a7 100755 --- a/bin/saferm +++ b/bin/saferm @@ -1,5 +1,8 @@ #!/bin/bash +set -e +[ -n "$DEBUG" ] && set -x + if [ ! -t 1 ] || [ -n "$UNSAFE_RM" ]; then # shellcheck disable=SC2068 exec /bin/rm "$@" @@ -33,22 +36,40 @@ if [ -n "${force}" ] && [ -n "${recurse}" ]; then exit 1 fi -tput setaf 1 -( - for ((idx = OPTIND; idx <= $#; idx++)); do +rm_prompt_arg=-I +files=() +for ((idx = OPTIND; idx <= $#; idx++)); do + if [ -e "${!idx}" ]; then if [ -d "${!idx}" ]; then - exa --color=never --all --tree "${!idx}" >&2 - echo "!" - echo "!" "$(du -sh "${!idx}")" - echo "!" "$(fd --no-ignore --type file . "${!idx}" | wc -l)" "files" - echo "!" "$(fd --no-ignore --type directory . "${!idx}" | wc -l)" "directories" - echo "!" + tput setaf 1 + ( + exa --color=never --all --tree "${!idx}" >&2 + echo "!" + echo "!" "$(du -sh --total "${!idx}" | grep total)" + echo "!" "$(fd --no-ignore --type file . "${!idx}" | wc -l)" "files" + echo "!" "$(fd --no-ignore --type directory . "${!idx}" | wc -l)" "directories" + echo "!" + ) >&2 + tput sgr0 else - echo "${!idx}" + tput setaf 1 + echo "${!idx}" >&2 + files+=("${!idx}") fi - done -) >&2 -tput sgr0 + fi +done + +if [ ${#files[@]} -gt 0 ]; then + rm_prompt_arg=-i + tput setaf 1 + ( + echo "!" + echo "!" "$(du -sh --total "${files[@]}" | grep total)" + echo "!" "${#files[@]}" "files" + echo "!" + ) >&2 + tput sgr0 +fi # shellcheck disable=SC2068 -exec /bin/rm -I --one-file-system "$@" +exec /bin/rm ${rm_prompt_arg} --one-file-system "$@" |