summary refs log tree commit diff
path: root/bin/trash
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2021-07-29 23:45:43 +0200
committerRobert Günzler <r@gnzler.io>2021-07-29 23:48:28 +0200
commite77881a72b735ca12aa055054656afdfbd6a8fc8 (patch)
tree53ed4778acca18f55141e4efbdc0a841b64116a5 /bin/trash
parent3079e935c793a9aa1831fd165e28636eb7a87dae (diff)
bin: format scripts
Diffstat (limited to 'bin/trash')
-rwxr-xr-xbin/trash98
1 files changed, 56 insertions, 42 deletions
diff --git a/bin/trash b/bin/trash
index b3c534d..5a040cb 100755
--- a/bin/trash
+++ b/bin/trash
@@ -3,72 +3,86 @@
 set -e
 [ -n "$DEBUG" ] && set -x
 
+rmbin=/bin/rm
+
 TRASH="$HOME/.trash"
 if [ ! -d "$TRASH" ]; then
-    mkdir -p "$TRASH"
+	mkdir -p "$TRASH"
 fi
 
+ls_trash() {
+	# shellcheck disable=SC2068
+	fd -H . "${TRASH:?}" $@
+}
+
 empty_trash() {
-    if [ ! -d "$TRASH" ] || [ -z "$(ls "$TRASH")" ]; then
-        exit 0
-    fi
-    echo " +++ trash +++"
-    ls "$TRASH"
-    printf " +++ take it out? [yN] "; read -r yn
-    if [ "$yn" = "y" ]; then
-        printf ' +++ removing %s/* ' "$TRASH"
-        rm -rf "${TRASH:?}/*"
-        echo 'done.'
-    fi
+	if [ ! -d "$TRASH" ] || [ -z "$(ls_trash)" ]; then
+		exit 0
+	fi
+	echo " +++ trash +++"
+	ls_trash
+	read -r yn
+	if [ "$yn" = "y" ]; then
+		printf ' +++ removing everything under %s (%d)... ' "$TRASH" "$(fd . "${TRASH:?}" | wc -l)"
+		ls_trash -x $rmbin -r
+		echo 'done.'
+	fi
 }
 
 install_auto_empty() {
-    case "$SHELL" in
-        *zsh) logoutfile="$ZDOTDIR/.zlogout" ;;
-        *bash) logoutfile="$HOME/.bash_logout" ;;
-        *)
-            echo "unknown shell $SHELL" 1>&2; exit 1 ;;
-    esac
-    cat > "$logoutfile" <<EOF
+	case "$SHELL" in
+		*zsh) logoutfile="$ZDOTDIR/.zlogout" ;;
+		*bash) logoutfile="$HOME/.bash_logout" ;;
+		*)
+			echo "unknown shell $SHELL" 1>&2
+			exit 1
+			;;
+	esac
+	cat >"$logoutfile" <<EOF
 # auto-empty-trash routine of $0
 yes | $0 --empty
 EOF
 }
 
 ask() {
-    r=$(printf 'yes\nno\n' | MENU_PROMPT="󰩹 delete $1" menu)
-    [ "${r}" = "yes" ] && return 0
-    return 1
+	r=$(printf 'yes\nno\n' | MENU_PROMPT="󰩹 delete $1" menu)
+	[ "${r}" = "yes" ] && return 0
+	return 1
 }
 
 interactive=${TRASH_ASK:-}
 trashrm=${TRASH_RM:-}
 
 case "$1" in
-    -e|--empty)
-        empty_trash
-        exit 0 ;;
-    --install)
-        install_auto_empty
-        exit 0 ;;
-    -i)
-        interactive=1
-        shift ;;
-    -h|--help)
-        echo "usage: trash [-e|--empty] FILE..."
-        exit 1 ;;
+	-e | --empty)
+		empty_trash
+		exit 0
+		;;
+	--install)
+		install_auto_empty
+		exit 0
+		;;
+	-i)
+		interactive=1
+		shift
+		;;
+	-h | --help)
+		echo "usage: trash [-e|--empty] FILE..."
+		exit 1
+		;;
 esac
 
 # main
 while test $# -gt 0; do
-    f="$1" ; shift
+	f="$1"
+	shift
 
-    [ -n "${interactive}" ] && { ask "$f" || continue; }
+	[ -n "${interactive}" ] && { ask "$f" || continue; }
 
-    echo "+ $(basename "$f")"
-    if [ -z "${trashrm}" ]; then
-        rm "$f"
-    else
-        mv "$f" "$TRASH/"
-    fi
+	echo "+ $(basename "$f")"
+	if [ -n "${trashrm}" ]; then
+		[ -d "$f" ] && $rmbin -r "$f" || $rmbin "$f"
+	else
+		mv "$f" "$TRASH/"
+	fi
 done