-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfrc
582 lines (497 loc) · 16.2 KB
/
lfrc
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# https://github.com/LukeSmithxyz/voidrice/blob/master/.config/lf/lfrc
# https://github.com/gokcehan/lf/blob/master/etc/lfrc.example
# https://github.com/gokcehan/lf/wiki/Integrations
# https://github.com/gokcehan/lf/wiki/Tips
# https://pkg.go.dev/github.com/gokcehan/lf#hdr-Configuration
# https://pkg.go.dev/github.com/gokcehan/lf#hdr-Options
# tabs are NOT supported (use kitty instead)
# interpreter for shell commands
set shell bash
# set '-eu' options for shell commands
# These options are used to have safer shell commands. Option '-e' is used to
# exit on error and option '-u' is used to give error for unset variables.
# Option '-f' disables pathname expansion which can be useful when $f, $fs, and
# $fx variables contain names with '*' or '?' characters. However, this option
# is used selectively within individual commands as it can be limiting at
# times.
set shellopts '-eu'
# set internal field separator (IFS) to "\n" for shell commands
# This is useful to automatically split file names in $fs and $fx properly
# since default file separator used in these variables (i.e. 'filesep' option)
# is newline. You need to consider the values of these options and create your
# commands accordingly.
set ifs "\n"
# leave some space at the top and the bottom of the screen
# set scrolloff 10
set scrolloff 9999
# set info []string (default '')
set anchorfind false
set drawbox
set incfilter true
set incsearch true
set mouse true
set number true
set promptfmt "\033[34;1m%d\033[0m\033[1m%f\033[0m"
set ratios 1:2
set relativenumber true
###
# # Use the `dim` attribute instead of underline for the cursor in the preview pane
# set cursorpreviewfmt "\033[7;2m"
map F filter
# use enter for shell commands
# map E shell
map E :push $bash<enter>
# map <m-1> select
map <m-down> down
map <m-up> up
map J page-down
map K page-up
# # show the result of execution of previous commands
# map ` !true
# https://github.com/gokcehan/lf/blob/a3127a2dc340ec9f9135f596f987e5c0184f3deb/doc.go#L182
# f Current file selection as a full path.
# fs Selected file(s) separated with the value of 'filesep' option as full path(s).
# fx Selected file(s) (i.e. 'fs') if there are any selected files, otherwise current file selection (i.e. 'f').
# # execute current file (must be executable)
# map x $$f
# map X !$f
# dedicated keys for file opener actions
map o &mimeopen $f
map O $mimeopen --ask $f
# define a custom 'open' command
# This command is called when current file is not a directory. You may want to
# use either file extensions and/or mime types here. Below uses an editor for
# text files and a file opener for the rest.
# this is clearly quite limited compared to rifle. detecting mimetype over file
# extension also leads to some undesirable behaviour (opening js/json with
# firefox by default)
cmd open &{{
case $(file --mime-type -Lb $f) in
text/* | */javascript | */json) lf -remote "send $id \$$EDITOR \$fx";;
*) for f in $fx; do $OPENER $f > /dev/null 2> /dev/null & done;;
esac
}}
# # mkdir command. See wiki if you want it to select created dir
# map N :push %mkdir<space>
# rename {{{
map r
# Rename the file with a completely different name
map rc push :rename<space>
# Edit the current filename
map ra ${{
# get 'basename' of the selection
filename="${f##*/}"
# quote it so we won't deal with quotes in the lf -remote command
filename="$(printf '%q' "$filename")"
filename="${filename// /<space>}"
lf -remote "send $id push :rename<space>$filename"
}}
# Edit filename before the extension
map a
map a ${{
# won't work if no .ext
# get 'basename' of the selection
filename="${f##*/}"
# quote it so we won't deal with quotes in the lf -remote command
filename="$(printf '%q' "$filename")"
filename="${filename// /<space>}"
lf -remote "send $id push :rename<space>$filename<a-b><c-b>"
}}
# simplest solution
# goes to start if no ext, only a minor annoyance
map a :rename; cmd-word-back; cmd-left
# <a-f> invalid
# map a push A<a-b><a-b><a-f> # before extension
# map a ${{
# # cmd rename-before-ext &{{
# if [ "$(echo "$fx" | wc -l)" -gt 1 ]; then
# lf -remote "send $id echo You can't change more than one file name"
# lf -remote "send $id unselect"
# else
# if [ -d "$f" ]; then
# lf -remote "send $id rename"
# else
# ext_len="$(echo ${f##*.} | wc -m)" # extention length
# for i in $(seq "$ext_len"); do
# pos="${pos}<left>"
# done
# lf -remote "send $id rename"
# lf -remote "send $id push "$pos""
# fi
# fi
# }}
cmd bulk-rename ${{
old="$(mktemp)"
new="$(mktemp)"
if [ -n "$fs" ]; then
fs="$(basename -a $fs)"
else
fs="$(ls)"
fi
printf '%s\n' "$fs" >"$old"
printf '%s\n' "$fs" >"$new"
$EDITOR "$new"
[ "$(wc -l < "$new")" -ne "$(wc -l < "$old")" ] && exit
paste "$old" "$new" | while IFS= read -r names; do
src="$(printf '%s' "$names" | cut -f1)"
dst="$(printf '%s' "$names" | cut -f2)"
if [ "$src" = "$dst" ] || [ -e "$dst" ]; then
continue
fi
mv -- "$src" "$dst"
done
rm -- "$old" "$new"
lf -remote "send $id unselect"
}}
# }}}
# define a custom 'rename' command without prompt for overwrite
# cmd rename %[ -e $1 ] && printf "file exists" || mv $f $1
# map r push :rename<space>
# symlinks {{{
cmd follow_link %{{
lf -remote "send ${id} select '$(readlink $f)'"
}}
map gL follow_link
# y (select for copy) and P to paste soft-link
# d (select for cut) and P to paste hard-link
cmd link %{{
set -- $(cat ~/.local/share/lf/files)
mode="$1"
shift
if [ "$#" -lt 1 ]; then
lf -remote "send $id echo no files to link"
exit 0
fi
case "$mode" in
# symbolically copy mode is indicating a soft link
copy) ln -sr -t . -- "$@";;
# while a move mode is indicating a hard link
move) ln -t . -- "$@";;
esac
rm ~/.local/share/lf/files
lf -remote "send clear"
}}
map P :link
# }}}
# copy/paste {{{
cmd paste_async &{{
set -- $(cat ~/.local/share/lf/files)
mode="$1"
shift
case "$mode" in
copy) cp -rn -- "$@" .;;
move) mv -n -- "$@" .;;
esac
rm ~/.local/share/lf/files
lf -remote "send clear"
}}
cmd paste_progress &{{
set -- $(cat ~/.local/share/lf/files)
mode="$1"
shift
case "$mode" in
copy)
rsync -av --ignore-existing --progress -- "$@" . |
stdbuf -i0 -o0 -e0 tr '\r' '\n' |
while IFS= read -r line; do
lf -remote "send $id echo $line"
done
;;
move) mv -n -- "$@" .;;
esac
rm ~/.local/share/lf/files
lf -remote "send clear"
}}
cmd paste_try_cow &{{
# # This was very helpful for debugging:
# log_file="$HOME/lf-reflink-log-$(date +'%Y-%m-%d_%H-%M-%S')"
# [ -f "$log_file" ] || touch "$log_file"
# exec 1>> $log_file 2>&1
# set -x
# In theory, this may fail,
# but I tested it on selection with 10k files - everything worked (bash)
set -- $(cat ~/.local/share/lf/files)
mode="$1"
shift
if [ $mode = 'copy' ]; then
# Reflink if all items of selection and the destination are on the
# same mount point and it is CoW fs.
# (to make sure reflink never fails in first place, so we don't have to
# clean up)
src_targets="$(df --output=target -- "$@" | sed '1d' | sort -u)"
if [ "$(df --output=target -- "$PWD" | tail -n 1)" = \
"$(echo "$src_targets" | tail -n 1)" ] && \
(( "$(echo "$src_targets" | wc -l)" == 1 )) && \
[[ "$(df --output=fstype -- "$PWD" | tail -n 1)" =~ ^(btrfs|xfs|zfs)$ ]]; then
echo 'selected copy and cp reflink paste'
start=$(date '+%s')
# Handle same names in dst
# TODO parallelism, idk - but exit/return/break won't stop the loop from subshell...
for i in "$@"; do
name="${i##*/}"
original="$name"
count=0
while [ -w "$PWD/$name" ]; do
count=$((count+1))
name="$original.~$count~"
done
set +e
cp_out="$(cp -rn --reflink=always -- "$i" "$PWD/$name" 2>&1)"
set -e
if [ ! -z "$cp_out" ]; then
lf -remote "send $id echoerr $cp_out"
exit 0
fi
done
finish=$(( $(date '+%s') - $start ))
t=''
if (( $finish > 2 )); then
t="${finish}s"
fi
# Or just skip a file when names are the same.
# (A LOT faster if you e.g. pasting selection of 10k files)
# cp -rn --reflink=always -- "$@" .
lf -remote "send clear"
green=$'\u001b[32m'
reset=$'\u001b[0m'
lf -remote "send $id echo ${green}reflinked!${reset} $t"
else
echo 'selected copy and lf native paste'
lf -remote "send $id paste"
lf -remote "send clear"
fi
elif [ $mode = 'move' ]; then
echo 'selected move and lf native paste'
lf -remote "send $id paste"
lf -remote "send clear"
fi
# # for debug
# set +x
lf -remote "send load"
}}
# name is different to avoid recursive calls
map p paste_try_cow
# }}}
# # make sure trash folder exists
# %mkdir -p ~/.trash
# # move current file or selected files to trash folder
# # (also see 'man mv' for backup/overwrite options)
# cmd trash %set -f; mv $fx ~/.trash
# map <delete> trash
# define a custom 'delete' command
cmd delete ${{
set -f
printf "$fx\n"
# printf "delete?[y/n]"
read -p 'delete? [y/n]' ans
[ "$ans" = "y" ] && rm -rfv $fx
}}
map D delete
# cmd dup ${{
# cp -v $fx $fx_
# }}
# map yp dup
# extract the current file with the right command
# (xkcd link: https://xkcd.com/1168/)
cmd extract ${{
set -f
case $f in
*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar xjvf $f;;
*.tar.gz|*.tgz) tar xzvf $f;;
*.tar.xz|*.txz) tar xJvf $f;;
*.zip) unzip $f;;
*.rar) unrar x $f;;
*.7z) 7z x $f;;
esac
}}
# compress current file or selected files with tar and gunzip
cmd tar ${{
set -f
mkdir $1
cp -r $fx $1
tar czf $1.tar.gz $1
rm -rf $1
}}
# compress current file or selected files with zip
cmd zip ${{
set -f
mkdir $1
cp -r $fx $1
zip -r $1.zip $1
rm -rf $1
}}
cmd edit-config ${{
$EDITOR ~/.config/lf/lfrc
lf -remote "send $id source ~/.config/lf/lfrc"
}}
map C edit-config
# # Dynamically set number of columns
# ${{
# w=$(tput cols)
# if [ $w -le 80 ]; then
# lf -remote "send $id set ratios 1:2"
# elif [ $w -le 160 ]; then
# lf -remote "send $id set ratios 1:2:3"
# else
# lf -remote "send $id set ratios 1:2:3:5"
# fi
# }}
cmd toggle_preview %{{
if [ "$lf_preview" = "true" ]; then
lf -remote "send $id :set preview false; set ratios 1:5"
else
lf -remote "send $id :set preview true; set ratios 1:2:3"
fi
}}
map zp toggle_preview
# map J :updir; down; open
# map J :updir; set dironly true; down; set dironly false; open
# map K :updir; set dironly true; up; set dironly false; open
# map K :updir; up; open
# cmd on-cd &{{
# case "$PWD" in
# /mnt/movies*)
# lf -remote "send $id set user_prev_sortby $lf_sortby"
# lf -remote "send $id set sortby natural"
# lf -remote "send $id set noreverse"
# lf -remote "send $id echomsg changed sort to natural"
# ;;
# *)
# # restore sorting on directory exit
# if [[ "$lf_user_prev_sortby" != "" ]]; then
# lf -remote "send $id set sortby $lf_user_prev_sortby"
# lf -remote "send $id set reverse"
# lf -remote "send $id echomsg restored sort to $lf_user_prev_sortby"
# lf -remote "send $id set user_prev_sortby ''"
# fi
# ;;
# esac
# }}
cmd on-cd &{{
# '&' commands run silently in background (which is what we want here),
# but are not connected to stdout.
# To make sure our escape sequence still reaches stdout we pipe it to /dev/tty
# printf "\033]0; $PWD\007" > /dev/tty
printf "\033]0; $(pwd | sed "s|$HOME|~|")\007" > /dev/tty
}}
# also run at startup
on-cd
# i don't like the default y mapping
# note: no 'unmap'
map y
cmd yank-dirname $dirname -- "$f" | head -c-1 | xclip -i -selection clipboard
cmd yank-path $printf '%s' "$fx" | xclip -i -selection clipboard
cmd yank-basename $basename -a -- $fx | head -c-1 | xclip -i -selection clipboard
map yp :yank-path
map yn :yank-basename
cmd yank-basename-without-extension ${{
echo "$fx" |
xargs -r -d '\n' basename -a |
awk -e '{
for (i=length($0); i > 0; i--) {
if (substr($0, i, 1) == ".") {
if (i == 1) print $0
else print substr($0, 0, i-1)
break
}
}
if (i == 0)
print $0
}' |
if [ -n "$fs" ]; then cat; else tr -d '\n'; fi |
xclip -i -selection clipboard
}}
cmd run-escaped %{{
IFS=" "
cmd="$1"
shift
"$cmd" "$*"
}}
map \\ push :run-escaped<space>
cmd select-files &{{
get_files() {
if [ "$lf_hidden" = 'false' ]; then
find "$PWD" -mindepth 1 -maxdepth 1 -type f -not -name '.*' -print0
else
find "$PWD" -mindepth 1 -maxdepth 1 -type f -print0
fi |
xargs -0 printf ' %q'
}
lf -remote "send $id :unselect; toggle $(get_files)"
}}
cmd select-dirs &{{
get_dirs() {
if [ "$lf_hidden" = 'false' ]; then
find "$PWD" -mindepth 1 -maxdepth 1 -type d -not -name '.*' -print0
else
find "$PWD" -mindepth 1 -maxdepth 1 -type d -print0
fi |
xargs -0 printf ' %q'
}
lf -remote "send $id :unselect; toggle $(get_dirs)"
}}
%[ $LF_LEVEL -eq 1 ] || echo "Warning: You're in a nested lf instance!"
cmd bookmark_jump ${{
res="$(cat $LF_BOOKMARK_PATH/$(ls $LF_BOOKMARK_PATH | fzf))"
lf -remote "send $id cd \"$res\""
}}
cmd bookmark_create ${{
read ans
echo $PWD > $LF_BOOKMARK_PATH/$ans
}}
cmd git_branch ${{
git branch | fzf | xargs git checkout
pwd_shell=$(pwd)
lf -remote "send $id updir"
lf -remote "send $id cd \"$pwd_shell\""
}}
map gb :git_branch
map gp ${{clear; git pull --rebase || true; echo "press ENTER"; read ENTER}}
map gs ${{clear; git status; echo "press ENTER"; read ENTER}}
map gl ${{clear; git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit}}
# cmd on-cd &{{
# # display git repository status in your prompt
# source /usr/share/git/completion/git-prompt.sh
# GIT_PS1_SHOWDIRTYSTATE=auto
# GIT_PS1_SHOWSTASHSTATE=auto
# GIT_PS1_SHOWUNTRACKEDFILES=auto
# GIT_PS1_SHOWUPSTREAM=auto
# GIT_PS1_COMPRESSSPARSESTATE=auto
# git=$(__git_ps1 " [GIT BRANCH:> %s]") || true
# fmt="\033[32;1m%u@%h\033[0m:\033[34;1m%w\033[0m\033[33;1m$git\033[0m"
# lf -remote "send $id set promptfmt \"$fmt\""
# }}
map f $vi $(fzf)
# cmd fzf $vi $(find . -name "$1" | fzf)
# map f push :fzf<space>
cmd fzf_jump ${{
res="$(find . -maxdepth 1 | fzf --reverse --header='Jump to location' | sed 's/\\/\\\\/g;s/"/\\"/g')"
if [ -d "$res" ] ; then
cmd="cd"
elif [ -f "$res" ] ; then
cmd="select"
else
exit 0
fi
lf -remote "send $id $cmd \"$res\""
}}
map <c-f> :fzf_jump
cmd trim_video ${{
ffpb -ss -i $f -c copy tmp.mkv
mv tmp.mkv $f
notify-send "$f trimmed!"
# lf -remote "send $id $cmd \"$res\""
# for file in %%s ; do ffmpeg -ss -i "$file" -c copy "$file"_1.mkv ; done
}}
map sA :set sortby atime; set noreverse; set info atime
map sC :set sortby ctime; set noreverse; set info ctime
map sE :set sortby ext; set noreverse; set info
map sN :set sortby natural; set noreverse; set info
map sS :set sortby size; set noreverse; set info size
map sT :set sortby time; set noreverse; set info time
map sa :set sortby atime; set reverse; set info atime
map sc :set sortby ctime; set reverse; set info ctime
map se :set sortby ext; set reverse; set info
map sn :set sortby natural; set reverse; set info
map ss :set sortby size; set reverse; set info size
map st :set sortby time; set reverse; set info time