-
Notifications
You must be signed in to change notification settings - Fork 0
/
fzf_trash.sh
executable file
·143 lines (126 loc) · 3.49 KB
/
fzf_trash.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/sh
# path: /home/klassiker/.local/share/repos/fzf/fzf_trash.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/fzf
# date: 2024-07-10T20:48:17+0200
# speed up script and avoid language problems by using standard c
LC_ALL=C
LANG=C
# config
edit="$EDITOR"
# help
script=$(basename "$0")
help="$script [-h/--help] -- script to manage files/folders with trash-cli
Usage:
$script
Examples:
$script"
[ -n "$1" ] \
&& printf "%s\n" "$help" \
&& exit 0
trash_remove() {
trash-list \
| cut -d ' ' -f3- \
| sort -u -fV \
| fzf -m +s \
--preview-window "up:75%:wrap" \
--preview "trash-list | grep {}$" \
| while IFS= read -r entry; do
trash-rm "$entry"
done
}
trash_put() {
find . -maxdepth 1 \
| sed -e 1d -e 's/^.\///' \
| sort -fV \
| fzf -m +s \
--bind 'focus:transform-preview-label:echo [ {} ]' \
--preview-window "right:75%" \
--preview "highlight {}" \
| while IFS= read -r entry; do
trash-put "$(pwd)/$entry"
done
}
delete_meta_files() {
# config
cmd="trash-put -v"
# create cache
cache_file=$(mktemp -t delete_metafiles.XXXXXX)
# create delete script
printf "%b" \
"#!/bin/sh\n\n" \
"# This script will be executed when you close the editor.\n" \
"# Please check everything! Clear the file to abort.\n\n" \
> "$cache_file"
find . \! -path "*/Trash/files/*" \
\( \
-name ".DS_Store" \
-o -name ".AppleDB" \
-o -name ".AppleDouble" \
-o -name ".@__qini" \
-o -name ".@__thumb" \
-o -name "._*" \
-o -name ":2e*" \
\) -exec printf "$cmd \"{}\"\n" \; \
| sed -e "s/$cmd \".\//$cmd \"/" \
| sort -fV >> "$cache_file"
# check delete script
"$edit" "$cache_file"
# execute delete script
chmod 755 "$cache_file"
"$cache_file"
# delete cache
[ -f "$cache_file" ] \
&& rm -f "$cache_file"
}
while true; do
# menu
select=$(printf "%s\n" \
"restore from trash" \
"put to trash" \
"delete meta files" \
"remove from trash" \
"empty trash" \
| fzf --cycle \
--bind 'focus:transform-preview-label:echo [ {} ]' \
--preview-window "right:75%:wrap" \
--preview "case {} in
'delete meta files')
printf '» create script to delete meta files like:\n\n'
printf ' %s\n' \
'.DS_Store' \
'.AppleDB' \
'.AppleDouble' \
'.@__qini' \
'.@__thumb' \
'._*' \
':2e*'
printf '\n» working directory: %s\n' \"$(pwd)\"
;;
*)
trash-list
;;
esac" \
)
# select executables
case "$select" in
"restore from trash")
trash-restore
;;
"put to trash")
trash_put
;;
"delete meta files")
delete_meta_files
;;
"remove from trash")
trash_remove
;;
"empty trash")
trash-empty
;;
*)
break
;;
esac
done