forked from dylanaraps/fff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fff
executable file
Β·1492 lines (1235 loc) Β· 46.8 KB
/
fff
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# fff - fucking fast file-manager.
get_os() {
# Figure out the current operating system to set some specific variables.
# '$OSTYPE' typically stores the name of the OS kernel.
case $OSTYPE in
# Mac OS X / macOS.
darwin*)
opener=open
file_flags=bIL
;;
haiku)
opener=open
[[ -z $FFF_TRASH_CMD ]] &&
FFF_TRASH_CMD=trash
[[ $FFF_TRASH_CMD == trash ]] && {
FFF_TRASH=$(finddir -v "$PWD" B_TRASH_DIRECTORY)
mkdir -p "$FFF_TRASH"
}
;;
esac
}
setup_terminal() {
# Setup the terminal for the TUI.
# '\e[?1049h': Use alternative screen buffer.
# '\e[?7l': Disable line wrapping.
# '\e[?25l': Hide the cursor.
# '\e[2J': Clear the screen.
# '\e[1;Nr': Limit scrolling to scrolling area.
# Also sets cursor to (0,0).
printf '\e[?1049h\e[?7l\e[?25l\e[2J\e[1;%sr' "$max_items"
# Hide echoing of user input
stty -echo
}
reset_terminal() {
# Reset the terminal to a useable state (undo all changes).
# '\e[?7h': Re-enable line wrapping.
# '\e[?25h': Unhide the cursor.
# '\e[2J': Clear the terminal.
# '\e[;r': Set the scroll region to its default value.
# Also sets cursor to (0,0).
# '\e[?1049l: Restore main screen buffer.
printf '\e[?7h\e[?25h\e[2J\e[;r\e[?1049l'
# Show user input.
stty echo
}
clear_screen() {
# Only clear the scrolling window (dir item list).
# '\e[%sH': Move cursor to bottom of scroll area.
# '\e[9999C': Move cursor to right edge of the terminal.
# '\e[1J': Clear screen to top left corner (from cursor up).
# '\e[2J': Clear screen fully (if using tmux) (fixes clear issues).
# '\e[1;%sr': Clearing the screen resets the scroll region(?). Re-set it.
# Also sets cursor to (0,0).
printf '\e[%sH\e[9999C\e[1J%b\e[1;%sr' \
"$((LINES-2))" "${TMUX:+\e[2J}" "$max_items"
}
setup_options() {
# Some options require some setup.
# This function is called once on open to parse
# select options so the operation isn't repeated
# multiple times in the code.
# Format for normal files.
[[ $FFF_FILE_FORMAT == *%f* ]] && {
file_pre=${FFF_FILE_FORMAT/'%f'*}
file_post=${FFF_FILE_FORMAT/*'%f'}
}
# Format for marked files.
[[ $FFF_MARK_FORMAT == *%f* ]] && {
mark_pre=${FFF_MARK_FORMAT/'%f'*}
mark_post=${FFF_MARK_FORMAT/*'%f'}
}
# Find supported 'file' arguments.
file -I &>/dev/null || : "${file_flags:=biL}"
}
get_term_size() {
# Get terminal size ('stty' is POSIX and always available).
# This can't be done reliably across all bash versions in pure bash.
read -r LINES COLUMNS < <(stty size)
# Max list items that fit in the scroll area.
((max_items=LINES-3))
}
get_ls_colors() {
# Parse the LS_COLORS variable and declare each file type
# as a separate variable.
# Format: ':.ext=0;0:*.jpg=0;0;0:*png=0;0;0;0:'
[[ -z $LS_COLORS ]] && {
FFF_LS_COLORS=0
return
}
# Turn $LS_COLORS into an array.
IFS=: read -ra ls_cols <<< "$LS_COLORS"
for ((i=0;i<${#ls_cols[@]};i++)); {
# Separate patterns from file types.
[[ ${ls_cols[i]} =~ ^\*[^\.] ]] &&
ls_patterns+="${ls_cols[i]/=*}|"
# Prepend 'ls_' to all LS_COLORS items
# if they aren't types of files (symbolic links, block files etc.)
[[ ${ls_cols[i]} =~ ^(\*|\.) ]] && {
ls_cols[i]=${ls_cols[i]#\*}
ls_cols[i]=ls_${ls_cols[i]#.}
}
}
# Strip non-ascii characters from the string as they're
# used as a key to color the dir items and variable
# names in bash must be '[a-zA-z0-9_]'.
ls_cols=("${ls_cols[@]//[^a-zA-Z0-9=\\;]/_}")
# Store the patterns in a '|' separated string
# for use in a REGEX match later.
ls_patterns=${ls_patterns//\*}
ls_patterns=${ls_patterns%?}
# Define the ls_ variables.
# 'declare' can't be used here as variables are scoped
# locally. 'declare -g' is not available in 'bash 3'.
# 'export' is a viable alternative.
export "${ls_cols[@]}" &>/dev/null
}
get_w3m_path() {
# Find the path to the w3m-img library.
w3m_paths=(/usr/{pkg/,}{local/,}{bin,lib,libexec,lib64,libexec64}/w3m/w3mi*)
read -r w3m _ < <(type -p "$FFF_W3M_PATH" w3mimgdisplay "${w3m_paths[@]}")
}
get_mime_type() {
# Get a file's mime_type.
mime_type=$(file "-${file_flags:-biL}" "$1" 2>/dev/null)
}
get_icon() {
# $1 Absolute path to the file
# $2 name of the file/directory
# $3 the extracted extension from the file name
# Icons for directories
[[ -d "$1" ]] && {
case "$2" in
# English
'.git' ) printf -- 'ξ»'; return ;;
'Desktop' ) printf -- 'ο'; return ;;
'Documents' ) printf -- 'ο'; return ;;
'Downloads' ) printf -- 'ο'; return ;;
'Dotfiles' ) printf -- 'ξ'; return ;;
'Dropbox' ) printf -- 'ξ'; return ;;
'Music' ) printf -- 'ο₯'; return ;;
'Pictures' ) printf -- 'ξ'; return ;;
'Public' ) printf -- 'ο«'; return ;;
'Templates' ) printf -- 'ο
'; return ;;
'Videos' ) printf -- 'ο'; return ;;
# Spanish
'Escritorio' ) printf -- 'ο'; return ;;
'Documentos' ) printf -- 'ο'; return ;;
'Descargas' ) printf -- 'ο'; return ;;
'MΓΊsica' ) printf -- 'ο₯'; return ;;
'ImΓ‘genes' ) printf -- 'ξ'; return ;;
'PΓΊblico' ) printf -- 'ο«'; return ;;
'Plantillas' ) printf -- 'ο
'; return ;;
'VΓdeos' ) printf -- 'ο'; return ;;
# French
'Bureau' ) printf -- 'ο'; return ;;
'Images' ) printf -- 'ξ'; return ;;
'Musique' ) printf -- 'ο₯'; return ;;
'Publique' ) printf -- 'ο«'; return ;;
'TΓ©lΓ©chargements' ) printf -- 'ο'; return ;;
'VidΓ©os' ) printf -- 'ο'; return ;;
# Portuguese
'Imagens' ) printf -- 'ξ'; return ;;
'Modelos' ) printf -- 'ο
'; return ;;
'Γrea de trabalho') printf -- 'ο'; return ;;
# Italian
'Documenti' ) printf -- 'ο'; return ;;
'Immagini' ) printf -- 'ξ'; return ;;
'Modelli' ) printf -- 'ο
'; return ;;
'Musica' ) printf -- 'ο₯'; return ;;
'Pubblici' ) printf -- 'ο«'; return ;;
'Scaricati' ) printf -- 'ο'; return ;;
'Scrivania' ) printf -- 'ο'; return ;;
'Video' ) printf -- 'ο'; return ;;
# German
'Bilder' ) printf -- 'ξ'; return ;;
'Dokumente' ) printf -- 'ο'; return ;;
'Musik' ) printf -- 'ο₯'; return ;;
'Schreibtisch' ) printf -- 'ο'; return ;;
'Vorlagen' ) printf -- 'ο
'; return ;;
'Γffentlich' ) printf -- 'ο«'; return ;;
# Hungarian
'Dokumentumok' ) printf -- 'ο'; return ;;
'KΓ©pek' ) printf -- 'ξ'; return ;;
'Zene' ) printf -- 'ο₯'; return ;;
'LetΓΆltΓ©sek' ) printf -- 'ο'; return ;;
'VideΓ³k' ) printf -- 'ο'; return ;;
'SzΓ‘mΓtΓ³gΓ©p' ) printf -- 'ο'; return ;;
* ) printf -- 'ξΏ'; return ;;
esac
}
# Icons for files with no extension
[[ "$2" == *"/$3" ]] && {
case "$2" in
'_gvimrc' | '_vimrc' |\
'bspwmrc' |'cmakelists.txt'|\
'config' | 'Makefile' |\
'makefile' | 'sxhkdrc' |\
'ini' ) printf -- 'ξ'; return ;;
'authorized_keys' |\
'known_hosts' |\
'license' |\
'LICENSE' ) printf -- 'ξ'; return ;;
'gemfile' |\
'Rakefile' |\
'rakefile' ) printf -- 'ξ'; return ;;
'a.out' |\
'configure' ) printf -- 'ξ'; return ;;
'dockerfile' ) printf -- 'ξ°'; return ;;
'Dockerfile' ) printf -- 'ο'; return ;;
'dropbox' ) printf -- 'ξ'; return ;;
'exact-match-case-sensitive-2' ) printf -- 'X2'; return ;;
'ledger' ) printf -- 'ο
'; return ;;
'node_modules' ) printf -- 'ξ'; return ;;
'playlists' ) printf -- 'ο'; return ;;
'procfile' ) printf -- 'ξ'; return ;;
'README' ) printf -- 'ξ'; return ;;
'*' ) printf -- 'ξ'; return ;;
esac
}
# Icon for files with the name starting with '.'
# without an extension
[[ "$2" == ".$3" ]] && {
case "$2" in
'.bash_aliases' |\
'.bash_history' |\
'.bash_logout' |\
'.bash_profile' |\
'.bashprofile' |\
'.bashrc' |\
'.dmrc' |\
'.DS_Store' |\
'.fasd' |\
'.gitattributes' |\
'.gitconfig' |\
'.gitignore' |\
'.inputrc' |\
'.jack-settings' |\
'.nvidia-settings-rc' |\
'.pam_environment' |\
'.profile' |\
'.recently-used' |\
'.selected_editor' |\
'.Xauthority' |\
'.Xdefaults' |\
'.xinitrc' |\
'.xinputrc' |\
'.Xresources' |\
'.zshrc' ) printf -- 'ξ'; return ;;
'.vim' |\
'.viminfo' |\
'.vimrc' ) printf -- 'ξ
'; return ;;
'.fehbg' ) printf -- 'ξ'; return ;;
'.gvimrc' ) printf -- 'ξ«'; return ;;
'.ncmpcpp' ) printf -- 'ο'; return ;;
'*' ) printf -- 'ξ'; return ;;
esac
}
# Icon for files whose names have an extension
[[ "$2" == *"."* ]] && {
# Special files
case "$2" in
'cmakelists.txt' |\
'Makefile.ac' |\
'Makefile.in' |\
'mimeapps.list' |\
'user-dirs.dirs' ) printf -- 'ξ'; return ;;
'README.markdown' |\
'README.md' |\
'README.rst' |\
'README.txt' ) printf -- 'ξ'; return ;;
'config.ac' |\
'config.m4' |\
'config.mk' ) printf -- 'ξ€'; return ;;
'gruntfile.coffee' |\
'gruntfile.js' |\
'gruntfile.ls' ) printf -- 'ξ'; return ;;
'package-lock.json' |\
'package.json' |\
'webpack.config.js' ) printf -- 'ξ'; return ;;
'gulpfile.coffee' |\
'gulpfile.js' |\
'gulpfile.ls' ) printf -- 'ξ'; return ;;
'LICENSE.txt' |\
'LICENSE.md' ) printf -- 'ξ'; return ;;
'.gitlab-ci.yml' ) printf -- 'ο'; return ;;
'config.ru' ) printf -- 'ξ'; return ;;
'docker-compose.yml' ) printf -- 'ο'; return ;;
'exact-match-case-sensitive-1.txt' ) printf -- 'X1'; return ;;
'favicon.ico' ) printf -- 'ξ£'; return ;;
'mix.lock' ) printf -- 'ξ'; return ;;
'react.jsx' ) printf -- 'ξ₯'; return ;;
esac
# extension
case "$3" in
'7z' | 'apk' |\
'bz2' | 'cab' |\
'cpio'| 'deb' |\
'gem' | 'gz' |\
'gzip'| 'lha' |\
'lzh' | 'lzma' |\
'rar' | 'rpm' |\
'tar' | 'tgz' |\
'xbps'| 'xz' |\
'zip' ) printf -- 'ο'; return ;;
'bat' | 'conf' |\
'cvs' |\
'htaccess' |\
'htpasswd' |\
'ini' | 'rc' |\
'toml'| 'yaml' |\
'yml' ) printf -- 'ξ'; return ;;
'asp' | 'awk' |\
'bash'| 'csh' |\
'efi' | 'elf' |\
'fish'| 'ksh' |\
'ps1' | 'rom' |\
'zsh' | 'sh' ) printf -- 'ξ'; return ;;
'avi' | 'flv' |\
'm4v' | 'mkv' |\
'mov' | 'mp4' |\
'mpeg'| 'mpg' |\
'webm' ) printf -- 'ο'; return ;;
'bmp' | 'gif' |\
'ico' | 'jpeg' |\
'jpg' | 'png' |\
'ppt' | 'pptx' |\
'webp' ) printf -- 'ξ'; return ;;
'aup' | 'cue' |\
'flac'| 'm4a' |\
'mp3' | 'ogg' |\
'wav' ) printf -- 'ο'; return ;;
'c' | 'c++' |\
'cc' | 'cp' |\
'cpp' | 'cxx' |\
'h' | 'hpp' ) printf -- 'ξ'; return ;;
'docx'| 'doc' |\
'epub'| 'pdf' |\
'rtf' | 'xls' |\
'xlsx' ) printf -- 'ο'; return ;;
'ejs' | 'haml' |\
'htm' | 'html' |\
'slim'| 'xhtml'|\
'xml' ) printf -- 'ξ'; return ;;
'a' | 'cmake' |\
'jl' | 'o' |\
'so' ) printf -- 'ξ€'; return ;;
'asm' | 'css' |\
'less'| 's' ) printf -- 'ξ'; return ;;
'db' | 'dump' |\
'img' | 'iso' |\
'sql' ) printf -- 'ξ'; return ;;
'f#' | 'fs' |\
'fsi' | 'fsx' |\
'fsscript' ) printf -- 'ξ§'; return ;;
'markdown' |\
'md' | 'mdx' |\
'rmd' ) printf -- 'ξ'; return ;;
'gemspec' |\
'rake'| 'rb' ) printf -- 'ξ'; return ;;
'dll' | 'exe' |\
'msi' ) printf -- 'ξ'; return ;;
'eex' | 'ex' |\
'exs' | 'leex' ) printf -- 'ξ'; return ;;
'class' |\
'jar' | 'java' ) printf -- 'ξΈ'; return ;;
'mustache' |\
'hbs' ) printf -- 'ξ'; return ;;
'json' |\
'webmanifest' ) printf -- 'ξ'; return ;;
'py' | 'pyc' |\
'pyd' | 'pyo' ) printf -- 'ξ'; return ;;
'cbr' | 'cbz' ) printf -- 'ο΅'; return ;;
'clj' | 'cljc' ) printf -- 'ξ¨'; return ;;
'cljs'| 'edn' ) printf -- 'ξͺ'; return ;;
'hrl' | 'erl' ) printf -- 'ξ±'; return ;;
'hh' | 'hxx' ) printf -- 'ο½'; return ;;
'hs' | 'lhs' ) printf -- 'ξ'; return ;;
'js' | 'mjs' ) printf -- 'ξ'; return ;;
'jsx' | 'tsx' ) printf -- 'ξΊ'; return ;;
'key' | 'pub' ) printf -- 'ξ'; return ;;
'ml' | 'mli' ) printf -- 'Ξ»'; return ;;
'pl' | 'pm' ) printf -- 'ξ©'; return ;;
'vim' | 'vimrc' ) printf -- 'ξ
'; return ;;
'psb' | 'psd' ) printf -- 'ξΈ'; return ;;
'rlib'| 'rs' ) printf -- 'ξ¨'; return ;;
'sass'| 'scss' ) printf -- 'ξ'; return ;;
'sln' | 'suo' ) printf -- 'ξ'; return ;;
'coffee' ) printf -- 'ξ'; return ;;
'ai' ) printf -- 'ξ΄'; return ;;
'cs' ) printf -- 'ο '; return ;;
'd' ) printf -- 'ξ―'; return ;;
'dart' ) printf -- 'ξ'; return ;;
'diff' ) printf -- 'ξ¨'; return ;;
'elm' ) printf -- 'ξ¬'; return ;;
'fi' ) printf -- '|'; return ;;
'go' ) printf -- 'ξ§'; return ;;
'log' ) printf -- 'οͺ'; return ;;
'lua' ) printf -- 'ξ '; return ;;
'nix' ) printf -- 'ο'; return ;;
'php' ) printf -- 'ξ'; return ;;
'pp' ) printf -- 'ο'; return ;;
'r' ) printf -- 'ο³'; return ;;
'rproj' ) printf -- 'ο«
'; return ;;
'rss' ) printf -- 'ξ'; return ;;
'scala' ) printf -- 'ξ·'; return ;;
'styl' ) printf -- 'ξ'; return ;;
'swift' ) printf -- 'ξ'; return ;;
't' ) printf -- 'ξ©'; return ;;
'tex' ) printf -- 'ο¨'; return ;;
'ts' ) printf -- 'ξ¨'; return ;;
'twig' ) printf -- 'ξ'; return ;;
'vue' ) printf -- 'ο΅'; return ;;
'xcplayground' ) printf -- 'ξ'; return ;;
'xul' ) printf -- 'ξ
'; return ;;
esac
}
printf -- 'ξ'; return
}
status_line() {
# Status_line to print when files are marked for operation.
local mark_ui="[${#marked_files[@]}] selected (${file_program[*]}) [p] ->"
# Escape the directory string.
# Remove all non-printable characters.
PWD_escaped=${PWD//[^[:print:]]/^[}
# '\e7': Save cursor position.
# This is more widely supported than '\e[s'.
# '\e[%sH': Move cursor to bottom of the terminal.
# '\e[30;41m': Set foreground and background colors.
# '%*s': Insert enough spaces to fill the screen width.
# This sets the background color to the whole line
# and fixes issues in 'screen' where '\e[K' doesn't work.
# '\r': Move cursor back to column 0 (was at EOL due to above).
# '\e[m': Reset text formatting.
# '\e[H\e[K': Clear line below status_line.
# '\e8': Restore cursor position.
# This is more widely supported than '\e[u'.
printf '\e7\e[%sH\e[3%s;4%sm%*s\r%s %s%s\e[m\e[%sH\e[K\e8' \
"$((LINES-1))" \
"${FFF_COL5:-0}" \
"${FFF_COL2:-1}" \
"$COLUMNS" "" \
"($((scroll+1))/$((list_total+1)))" \
"${marked_files[*]:+${mark_ui}}" \
"${1:-${PWD_escaped:-/}}" \
"$LINES"
}
read_dir() {
# Read a directory to an array and sort it directories first.
local dirs
local files
local item_index
# Set window name.
printf '\e]2;fff: %s\e'\\ "$PWD"
# If '$PWD' is '/', unset it to avoid '//'.
[[ $PWD == / ]] && PWD=
for item in "$PWD"/*; do
if [[ -d $item ]]; then
dirs+=("$item")
((item_index++))
# Find the position of the child directory in the
# parent directory list.
[[ $item == "$OLDPWD" ]] &&
((previous_index=item_index))
else
files+=("$item")
fi
done
list=("${dirs[@]}" "${files[@]}")
# Indicate that the directory is empty.
[[ -z ${list[0]} ]] &&
list[0]=empty
((list_total=${#list[@]}-1))
# Save the original dir in a second list as a backup.
cur_list=("${list[@]}")
}
print_line() {
# Format the list item and print it.
local file_name=${list[$1]##*/}
local file_ext=${file_name##*.}
local format
local suffix
local icon
# If the dir item doesn't exist, end here.
if [[ -z ${list[$1]} ]]; then
return
# Directory.
elif [[ -d ${list[$1]} ]]; then
format+=\\e[${di:-1;3${FFF_COL1:-2}}m
suffix+=/
# Block special file.
elif [[ -b ${list[$1]} ]]; then
format+=\\e[${bd:-40;33;01}m
# Character special file.
elif [[ -c ${list[$1]} ]]; then
format+=\\e[${cd:-40;33;01}m
# Executable file.
elif [[ -x ${list[$1]} ]]; then
format+=\\e[${ex:-01;32}m
# Symbolic Link (broken).
elif [[ -h ${list[$1]} && ! -e ${list[$1]} ]]; then
format+=\\e[${mi:-01;31;7}m
# Symbolic Link.
elif [[ -h ${list[$1]} ]]; then
format+=\\e[${ln:-01;36}m
# Fifo file.
elif [[ -p ${list[$1]} ]]; then
format+=\\e[${pi:-40;33}m
# Socket file.
elif [[ -S ${list[$1]} ]]; then
format+=\\e[${so:-01;35}m
# Color files that end in a pattern as defined in LS_COLORS.
# 'BASH_REMATCH' is an array that stores each REGEX match.
elif [[ $FFF_LS_COLORS == 1 &&
$ls_patterns &&
$file_name =~ ($ls_patterns)$ ]]; then
match=${BASH_REMATCH[0]}
file_ext=ls_${match//[^a-zA-Z0-9=\\;]/_}
format+=\\e[${!file_ext:-${fi:-37}}m
# Color files based on file extension and LS_COLORS.
# Check if file extension adheres to POSIX naming
# stardard before checking if it's a variable.
elif [[ $FFF_LS_COLORS == 1 &&
$file_ext != "$file_name" &&
$file_ext =~ ^[a-zA-Z0-9_]*$ ]]; then
file_ext=ls_${file_ext}
format+=\\e[${!file_ext:-${fi:-37}}m
else
format+=\\e[${fi:-37}m
fi
# If the list item is under the cursor.
(("$1" == scroll)) &&
format+="\\e[1;3${FFF_COL4:-6};7m"
# If the list item is marked for operation.
[[ ${marked_files[$1]} == "${list[$1]:-null}" ]] && {
format+=\\e[3${FFF_COL3:-1}m${mark_pre:= }
suffix+=${mark_post:=*}
}
# Escape the directory string.
# Remove all non-printable characters.
file_name="${file_name//[^[:print:]]/^[}"
(( "${FFF_FILE_ICON:=0}" == 1 )) && \
icon="$(get_icon "${list[$1]}" "$file_name" "$file_ext") "
printf '\r%b%s\e[m\r' \
"${file_pre}${format}${icon}"\
"${file_name}${suffix}${file_post}"
}
draw_dir() {
# Print the max directory items that fit in the scroll area.
local scroll_start=$scroll
local scroll_new_pos
local scroll_end
# When going up the directory tree, place the cursor on the position
# of the previous directory.
((find_previous == 1)) && {
((scroll_start=previous_index-1))
((scroll=scroll_start))
# Clear the directory history. We're here now.
find_previous=
}
# If current dir is near the top of the list, keep scroll position.
if ((list_total < max_items || scroll < max_items/2)); then
((scroll_start=0))
((scroll_end=max_items))
((scroll_new_pos=scroll+1))
# If curent dir is near the end of the list, keep scroll position.
elif ((list_total - scroll < max_items/2)); then
((scroll_start=list_total-max_items+1))
((scroll_new_pos=max_items-(list_total-scroll)))
((scroll_end=list_total+1))
# If current dir is somewhere in the middle, center scroll position.
else
((scroll_start=scroll-max_items/2))
((scroll_end=scroll_start+max_items))
((scroll_new_pos=max_items/2+1))
fi
# Reset cursor position.
printf '\e[H'
for ((i=scroll_start;i<scroll_end;i++)); {
# Don't print one too many newlines.
((i > scroll_start)) &&
printf '\n'
print_line "$i"
}
# Move the cursor to its new position if it changed.
# If the variable 'scroll_new_pos' is empty, the cursor
# is moved to line '0'.
printf '\e[%sH' "$scroll_new_pos"
((y=scroll_new_pos))
}
draw_img() {
# Draw an image file on the screen using w3m-img.
# We can use the framebuffer; set win_info_cmd as appropriate.
[[ $(tty) == /dev/tty[0-9]* && -w /dev/fb0 ]] &&
win_info_cmd=fbset
# X isn't running and we can't use the framebuffer, do nothing.
[[ -z $DISPLAY && $win_info_cmd != fbset ]] &&
return
# File isn't an image file, do nothing.
get_mime_type "${list[scroll]}"
[[ $mime_type != image/* ]] &&
return
# w3m-img isn't installed, do nothing.
type -p "$w3m" &>/dev/null || {
cmd_line "error: Couldn't find 'w3m-img', is it installed?"
return
}
# $win_info_cmd isn't installed, do nothing.
type -p "${win_info_cmd:=xdotool}" &>/dev/null || {
cmd_line "error: Couldn't find '$win_info_cmd', is it installed?"
return
}
# Get terminal window size in pixels and set it to WIDTH and HEIGHT.
if [[ $win_info_cmd == xdotool ]]; then
IFS=$'\n' read -d "" -ra win_info \
< <(xdotool getactivewindow getwindowgeometry --shell)
declare "${win_info[@]}" &>/dev/null || {
cmd_line "error: Failed to retrieve window size."
return
}
else
[[ $(fbset --show) =~ .*\"([0-9]+x[0-9]+)\".* ]]
IFS=x read -r WIDTH HEIGHT <<< "${BASH_REMATCH[1]}"
fi
# Get the image size in pixels.
read -r img_width img_height < <("$w3m" <<< "5;${list[scroll]}")
# Substract the status_line area from the image size.
((HEIGHT=HEIGHT-HEIGHT*5/LINES))
((img_width > WIDTH)) && {
((img_height=img_height*WIDTH/img_width))
((img_width=WIDTH))
}
((img_height > HEIGHT)) && {
((img_width=img_width*HEIGHT/img_height))
((img_height=HEIGHT))
}
clear_screen
status_line "${list[scroll]}"
# Add a small delay to fix issues in VTE terminals.
((BASH_VERSINFO[0] > 3)) &&
read "${read_flags[@]}" -srn 1
# Display the image.
printf '0;1;%s;%s;%s;%s;;;;;%s\n3;\n4\n' \
"${FFF_W3M_XOFFSET:-0}" \
"${FFF_W3M_YOFFSET:-0}" \
"$img_width" \
"$img_height" \
"${list[scroll]}" | "$w3m" &>/dev/null
# Wait for user input.
read -ern 1
# Clear the image.
printf '6;%s;%s;%s;%s\n3;' \
"${FFF_W3M_XOFFSET:-0}" \
"${FFF_W3M_YOFFSET:-0}" \
"$WIDTH" \
"$HEIGHT" | "$w3m" &>/dev/null
redraw
}
redraw() {
# Redraw the current window.
# If 'full' is passed, re-fetch the directory list.
[[ $1 == full ]] && {
read_dir
scroll=0
}
clear_screen
draw_dir
status_line
}
mark() {
# Mark file for operation.
# If an item is marked in a second directory,
# clear the marked files.
[[ $PWD != "$mark_dir" ]] &&
marked_files=()
# Don't allow the user to mark the empty directory list item.
[[ ${list[0]} == empty && -z ${list[1]} ]] &&
return
if [[ $1 == all ]]; then
if ((${#marked_files[@]} != ${#list[@]})); then
marked_files=("${list[@]}")
mark_dir=$PWD
else
marked_files=()
fi
redraw
else
if [[ ${marked_files[$1]} == "${list[$1]}" ]]; then
unset 'marked_files[scroll]'
else
marked_files[$1]="${list[$1]}"
mark_dir=$PWD
fi
# Clear line before changing it.
printf '\e[K'
print_line "$1"
fi
# Find the program to use.
case "$2" in
${FFF_KEY_YANK:=y}|${FFF_KEY_YANK_ALL:=Y}) file_program=(cp -iR) ;;
${FFF_KEY_MOVE:=m}|${FFF_KEY_MOVE_ALL:=M}) file_program=(mv -i) ;;
${FFF_KEY_LINK:=s}|${FFF_KEY_LINK_ALL:=S}) file_program=(ln -s) ;;
# These are 'fff' functions.
${FFF_KEY_TRASH:=d}|${FFF_KEY_TRASH_ALL:=D})
file_program=(trash)
;;
${FFF_KEY_BULK_RENAME:=b}|${FFF_KEY_BULK_RENAME_ALL:=B})
file_program=(bulk_rename)
;;
esac
status_line
}
trash() {
# Trash a file.
cmd_line "trash [${#marked_files[@]}] items? [y/n]: " y n
[[ $cmd_reply != y ]] &&
return
if [[ $FFF_TRASH_CMD ]]; then
# Pass all but the last argument to the user's
# custom script. command is used to prevent this function
# from conflicting with commands named "trash".
command "$FFF_TRASH_CMD" "${@:1:$#-1}"
else
cd "$FFF_TRASH" || cmd_line "error: Can't cd to trash directory."
if cp -alf "$@" &>/dev/null; then
rm -r "${@:1:$#-1}"
else
mv -f "$@"
fi
# Go back to where we were.
cd "$OLDPWD" ||:
fi
}
bulk_rename() {
# Bulk rename files using '$EDITOR'.
rename_file=${XDG_CACHE_HOME:=${HOME}/.cache}/fff/bulk_rename
marked_files=("${@:1:$#-1}")
# Save marked files to a file and open them for editing.
printf '%s\n' "${marked_files[@]##*/}" > "$rename_file"
"${EDITOR:-vi}" "$rename_file"
# Read the renamed files to an array.
IFS=$'\n' read -d "" -ra changed_files < "$rename_file"
# If the user deleted a line, stop here.
((${#marked_files[@]} != ${#changed_files[@]})) && {
rm "$rename_file"
cmd_line "error: Line mismatch in rename file. Doing nothing."
return
}
printf '%s\n%s\n' \
"# This file will be executed when the editor is closed." \
"# Clear the file to abort." > "$rename_file"
# Construct the rename commands.
for ((i=0;i<${#marked_files[@]};i++)); {
[[ ${marked_files[i]} != "${PWD}/${changed_files[i]}" ]] && {
printf 'mv -i -- %q %q\n' \
"${marked_files[i]}" "${PWD}/${changed_files[i]}"
local renamed=1
}
} >> "$rename_file"
# Let the user double-check the commands and execute them.
((renamed == 1)) && {
"${EDITOR:-vi}" "$rename_file"
source "$rename_file"
rm "$rename_file"
}
# Fix terminal settings after '$EDITOR'.
setup_terminal
}
open() {
# Open directories and files.
if [[ -d $1/ ]]; then
search=
search_end_early=
cd "${1:-/}" ||:
redraw full
elif [[ -f $1 ]]; then
# Figure out what kind of file we're working with.
get_mime_type "$1"
# Open all text-based files in '$EDITOR'.
# Everything else goes through 'xdg-open'/'open'.
case "$mime_type" in
text/*|*x-empty*|*json*)
# If 'fff' was opened as a file picker, save the opened
# file in a file called 'opened_file'.
((file_picker == 1)) && {
printf '%s\n' "$1" > \
"${XDG_CACHE_HOME:=${HOME}/.cache}/fff/opened_file"
exit
}
clear_screen
reset_terminal
"${VISUAL:-${EDITOR:-vi}}" "$1"
setup_terminal
redraw
;;
*)
# 'nohup': Make the process immune to hangups.
# '&': Send it to the background.
# 'disown': Detach it from the shell.
nohup "${FFF_OPENER:-${opener:-xdg-open}}" "$1" &>/dev/null &
disown
;;
esac
fi
}
cmd_line() {
# Write to the command_line (under status_line).
cmd_reply=
# '\e7': Save cursor position.
# '\e[?25h': Unhide the cursor.
# '\e[%sH': Move cursor to bottom (cmd_line).
printf '\e7\e[%sH\e[?25h' "$LINES"
# '\r\e[K': Redraw the read prompt on every keypress.
# This is mimicking what happens normally.
while IFS= read -rsn 1 -p $'\r\e[K'"${1}${cmd_reply}" read_reply; do
case $read_reply in
# Backspace.
$'\177'|$'\b')
cmd_reply=${cmd_reply%?}
# Clear tab-completion.
unset comp c
;;
# Tab.
$'\t')
comp_glob="$cmd_reply*"
# Pass the argument dirs to limit completion to directories.
[[ $2 == dirs ]] &&
comp_glob="$cmd_reply*/"
# Generate a completion list once.
[[ -z ${comp[0]} ]] &&
IFS=$'\n' read -d "" -ra comp < <(compgen -G "$comp_glob")