1
0
Fork 0
dotfiles/.local/bin/trash-restore

41 lines
1.0 KiB
Plaintext
Raw Normal View History

#!/bin/sh
2023-11-16 14:06:50 +01:00
set -e
getfsroot() {
2023-02-07 17:19:53 +01:00
printf "%s" "$(df "$1" --output=target | tail -1)"
}
list() {
2023-02-07 17:19:53 +01:00
# 1st arg is trash files directory
# 2nd arg is directory to match trash files for
2023-11-16 14:06:50 +01:00
[ ! "$(command ls -A "$1")" ] && exit 1
2023-02-07 17:19:53 +01:00
for file in "$1"/*; do
[ "$(head -1 "$file")" = "[Trash Info]" ] &&
2023-11-16 14:06:50 +01:00
filepath=$(grep Path "$file" | cut -d '=' -f2) &&
echo "$filepath" | grep -qP "^$2/[^/]+$" &&
printf "%s %s %s\n" \
2023-02-07 17:19:53 +01:00
"$(basename "$file")" \
2023-11-16 14:06:50 +01:00
"$filepath" \
2023-02-07 17:19:53 +01:00
"$(date -d "$(grep Date "$file" | cut -d '=' -f2)" +'%x %X')"
done
}
2023-11-16 14:06:50 +01:00
if [ -n "$1" ]; then
dir="$(realpath "$1")"
else
dir="$(realpath "$PWD")"
fi
fsroot="$(getfsroot "$dir")"
2023-11-16 14:06:50 +01:00
if [ "$fsroot" = "$(getfsroot "${XDG_DATA_HOME:-$HOME/.local/share}")" ]; then
basedir="${XDG_DATA_HOME:-$HOME/.local/share}/Trash"
else
2023-02-07 17:19:53 +01:00
basedir="${fsroot}/.Trash"
2023-11-16 14:06:50 +01:00
fi
sel="$(list "$basedir/info" "$dir" | fzf)"
2023-11-16 14:06:50 +01:00
file="$basedir/files/$(echo "$sel" | cut -d '' -f1)"
dest="$(echo "$sel" | cut -d '' -f3)"
command mv -ib "$file" "$dest"
2023-11-16 14:06:50 +01:00
command rm "$basedir/info/$(echo "$sel" | cut -d '' -f1)"