From 65eeb575d609a335baa00ec2502ab44e7bdc2a3d Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Fri, 18 Nov 2022 17:43:00 +0100 Subject: bin/saferm: refactor --- bin/saferm | 49 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) (limited to 'bin') 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 "$@" -- cgit 1.4.1