1
0
Fork 0

new trashing and duping commands for lf

This commit is contained in:
Luca Bilke 2022-10-22 16:35:03 +02:00
parent 34341c8756
commit cfb2715343
3 changed files with 82 additions and 7 deletions

View File

@ -72,12 +72,17 @@ cmd delete ${{
}}
cmd trash ${{
clear; tput cup $(($(tput lines)/3)); tput bold
set -f
printf "%s\n\t" "$fx"
printf "trash?[y/N]"
read ans
[ $ans = "y" ] && trash-put $fx
clear; tput cup $(($(tput lines)/3)); tput bold
set -f
printf "%s\n\t" "$fx"
printf "trash?[y/N]"
read ans
if [ $ans = "y" ]; then
for f in $fx; do
echo $f
trash-put $f
done
fi
}}
cmd rsyncto ${{
@ -143,6 +148,16 @@ cmd paste &{{
lf -remote "send clear"
}}
cmd dupe &{{
for file in $fx; do
find "$PWD" "$file" | grep -oP '(?<=.\.~)\d+(?=~$)' | sort -n | tail -1 | (
ext=$(($(cat /dev/stdin)+1))
filedest="$(echo "$file" | sed 's/.~[[:digit:]]*~$//').~$ext~"
cp "$file" "$filedest"
)
done
}}
cmd share $curl -F"file=@$fx" https://0x0.snaile.de | xclip -r -selection c
# Bindings
@ -153,6 +168,7 @@ map <c-c>d $lf -remote "send $id cd '$(cat ${XDG_DATA_HOME:-$HOME/.local/share}/
map J $dir=$(sed -e 's/\s*#.*//' -e '/^$/d' -e 's/^\S*\s*//' ${XDG_CONFIG_HOME:-$HOME/.config}/shell/bm-dirs | fzf) && lf -remote "send $id cd $(eval echo $dir)"
map D trash
map <c-d> delete
map T $trash-restore $PWD
map E extract
map R rsyncto
map P link
@ -174,7 +190,7 @@ map I push A<a-b> # after extension
map i push A<a-b><a-b><a-f> # before extension
map c push A<c-u> # new rename
map B bulkrename
map b $cp "$f" "$f-COPY"
map b dupe
map u :clear; unselect
map n &echo $f | xclip -r -selection c
map N

28
.local/bin/trash-put Executable file
View File

@ -0,0 +1,28 @@
#!/bin/sh
set -e
[ -e "$1" ] || exit 1
getfsroot() {
printf "%s" "$(df "$1" --output=target | tail -1)"
}
filepath="$(realpath "$1")"
filename="$(basename "$1")"
fsroot="$(getfsroot "$1")"
[ "$fsroot" = "$(getfsroot "${XDG_DATA_HOME:-$HOME/.local/share}")" ] \
&& basedir="${XDG_DATA_HOME:-$HOME/.local/share}/Trash" \
|| basedir="${fsroot}/.Trash"
infodir="$basedir/info"
filedir="$basedir/files"
mkdir -p "$infodir" 1>/dev/null 2>&1
mkdir -p "$filedir" 1>/dev/null 2>&1
find "$filedir" -regex ".*$filename.*" | grep -oP '(?<=.\.~)\d+(?=~$)' | sort -n | tail -1 | (
ext=$(($(cat /dev/stdin)+1))
filedest="$filedir/$filename.~$ext~"
command mv -f "$filepath" "$filedest"
cat <<EOF > "$infodir/$(basename "$filedest")"
[Trash Info]
Path=$filepath
DeletionDate=$(date +%Y%m%dT%T)
EOF
)

31
.local/bin/trash-restore Executable file
View File

@ -0,0 +1,31 @@
#!/bin/sh
set -e
getfsroot() {
printf "%s" "$(df "$1" --output=target | tail -1)"
}
list() {
# 1st arg is trash files directory
# 2nd arg is directory to match trash files for
[ ! "$(ls -A "$1")" ] && exit 1
for file in "$1"/* ; do
[ "$(head -1 "$file")" = "[Trash Info]" ] && \
fpath=$(grep Path "$file" | cut -d '=' -f2) && \
echo "$fpath" | grep -qP "^$2/[^/]+$" && \
printf "%s %s %s\n" \
"$(basename "$file")" \
"$fpath" \
"$(date -d "$(grep Date "$file" | cut -d '=' -f2)" +'%x %X')"
done
}
[ -n "$1" ] && dir="$(realpath "$1")" || dir="$(getfsroot "$PWD")"
fsroot="$(getfsroot "$dir")"
[ "$fsroot" = "$(getfsroot "${XDG_DATA_HOME:-$HOME/.local/share}")" ] \
&& basedir="${XDG_DATA_HOME:-$HOME/.local/share}/Trash" \
|| basedir="${fsroot}/.Trash"
sel="$(list "$basedir/info" "$dir" | fzf)"
file="$basedir/files/$(echo "$sel" | cut -d ' ' -f1)"
dest="$(echo "$sel" | cut -d ' ' -f2)"
command mv -ib "$file" "$dest"
command rm "$basedir/info/$(echo "$sel" | cut -d ' ' -f1)"