-
Notifications
You must be signed in to change notification settings - Fork 3
/
fzf_browser.sh
executable file
·548 lines (495 loc) · 14.6 KB
/
fzf_browser.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
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
#!/usr/bin/env bash
################## CONFIGURATION ##############
# Use fd instead of find.
# Currently breaks directory selection in (dir+files mode)
__fuzzybrowse_use_fd=0
__fuzzybrowse_runInTerminal(){
urxvt -e "$@"
}
__fuzzybrowse_previewFile(){
__fuzzybrowse_runInTerminal less "$@"
}
__fuzzybrowse_runFile(){
# xdg-open "$@"
nohup xdg-open "$@" > /dev/null 2>&1 &
}
# List of extensions to ignore, separated by |
__fuzzybrow_file_ignore="log|bak|aux|lof|lol|lot|toc|bbl|blg|tmp|temp|swp|incomplete|o|class|pdb|cache|pyc|aria2|torrent|torrent.added|part|crdownload|CMakeCache.txt"
# List of folders to ignore, separated by |
__fuzzybrow_dir_ignore="_build|elm-stuff|node_modules|.git|.svn|.hg|deps|ex_gdax|CMakeFiles"
#################### END CONFIGURATION #########
# Opens fuzzy dir browser. Tab to switch between file mode and directory mode. Esc to quit.
fzf_browser() {
local res key sel prev_dir query stored_query tmp_prompt tmp_file tmp_dir
local initial_dir
initial_dir="$(pwd)"
local start_query
local out_file
local custom_prompt
local early_exit="--ansi"
local fzf_opts="--ansi"
local extraIgnoreDirs=""
local extraIgnoreFiles=""
local full_path=""
while getopts "Fshrep:q:o:f:i:d:" opt; do
case "$opt" in
h)
__fuzzybrowse_show_hidden=1
;;
r)
__fuzzybrowse_recursive=1
;;
s)
__fuzzybrowse_sort=1
;;
e)
early_exit="-1"
;;
p)
custom_prompt="$OPTARG"
;;
q)
start_query="$OPTARG"
;;
o)
out_file="$OPTARG"
;;
f)
fzf_opts="$OPTARG"
;;
i)
extraIgnoreFiles="|$OPTARG"
;;
d)
extraIgnoreDirs="|$OPTARG"
;;
F)
full_path=1
;;
*)
# invalid flag
;;
esac
done
shift $((OPTIND-1))
__fuzzybrow_file_ignore_pat="$(printf '.*\.\(%q\)$' "$__fuzzybrow_file_ignore""$extraIgnoreFiles")"
__fuzzybrow_dir_ignore_pat="$(printf '.*/\(%q\)\(/.*\|$\)' "$__fuzzybrow_dir_ignore""$extraIgnoreDirs")"
local start_dir
start_dir="$1"
if [[ "${start_query:0:1}" == "." ]]; then
__fuzzybrowse_show_hidden=1
fi
stored_query="$start_query"
if [[ -n "$out_file" ]]; then
echo -n "" > "$out_file"
fi
if [[ -n "$start_dir" ]]; then
if [[ "$start_dir" == "~" ]]; then
start_dir="$HOME"
fi
if [[ -d "$start_dir" ]]; then
cd "$start_dir"
else
>&2 echo "Invalid start directory: $start_dir"
return
fi
else
start_dir="$initial_dir"
fi
start_query=""
while true ; do
if [[ -n "$custom_prompt" ]]; then
tmp_prompt="--prompt=$custom_prompt""$(pwd)/"
else
tmp_prompt="--ansi"
fi
if [[ "$__fuzzybrowse_recursive" == 1 ]]; then
res="$(__fuzzybrowse_all_source | __fuzzybrowse_fzf_cmd "$tmp_prompt" "$early_exit" "-q" "$stored_query")"
else
res="$(__fuzzybrowse_combined_source 2>/dev/null | __fuzzybrowse_fzf_cmd "$tmp_prompt" "$early_exit" "-q" "$stored_query")"
fi
stored_query=""
if [[ -z "$res" ]]; then
dirs -c
cd "$initial_dir"
return
fi
query=$(echo "$res" | head -1)
sel=$(echo "$res"|tail -n +3)
key=$(echo "$res" | head -2 | tail -1)
if [[ "$early_exit" == "-1" && -z "$key" ]]; then
break
fi
early_exit="--ansi"
case "$query" in
".")
sel="$(pwd)/"
break
;;
"..")
pushd ".." > /dev/null 2>&1
;;
esac
case "$key" in
left|\#|\`)
pushd ".." > /dev/null 2>&1
;;
/)
break;
#if [[ -d "$sel" ]]; then
#pushd "$sel" > /dev/null 2>&1
#fi
;;
ctrl-q)
break;
#if [[ -d "$sel" ]]; then
#pushd "$sel" > /dev/null 2>&1
#fi
;;
ctrl-o)
prev_dir="$(pwd)"
popd > /dev/null 2>&1 || exit
;;
ctrl-u)
if [[ -n "$prev_dir" ]]; then
pushd "$prev_dir" > /dev/null 2>&1 || exit
prev_dir=""
fi
;;
\>)
sel="$(pwd)/"
break
;;
return)
tmp_file=$(__fuzzybrowse_get_entry "$sel")
if [[ -f "$tmp_file" ]]; then
sel="$tmp_file"
break
fi
if [[ -d "$tmp_file" ]]; then
pushd "$tmp_file" > /dev/null 2>&1 || exit
else
break
fi
;;
right)
stored_query="$query"
tmp_dir=$(__fuzzybrowse_get_dir "$sel")
if [[ -d "$tmp_dir" ]]; then
pushd "$tmp_dir" > /dev/null 2>&1 || exit
else
__fuzzybrowse_runFile "$sel"
fi
;;
ctrl-c)
dirs -c
cd "$initial_dir" || exit
return
;;
ctrl-a)
stored_query="$query"
__fuzzybrowse_show_hidden=$((__fuzzybrowse_show_hidden==0))
;;
ctrl-y)
stored_query="$query"
__fuzzybrowse_sort=$((__fuzzybrowse_sort==0))
;;
ctrl-s)
stored_query="$query"
__fuzzybrowse_sort=$((__fuzzybrowse_sort==0))
;;
ctrl-h)
pushd "$HOME" > /dev/null 2>&1 || exit
;;
ctrl-z)
tmp_dir="$(fasd -ld 2>&1 | sed -n 's/^[ 0-9.,]*//p' | fzf --tac +s)"
if [[ -n "$tmp_dir" ]]; then
pushd "$tmp_dir" > /dev/null 2>&1 || exit
fi
;;
ctrl-x)
export e
e="$(__fuzzybrowse_full_path "$(__fuzzybrowse_get_entry "$sel")")"
clear
echo "\$e = $e"
$SHELL
;;
ctrl-v)
stored_query="$query"
__fuzzybrowse_runFile "$(__fuzzybrowse_get_entry "$sel")" > /dev/null 2>&1
;;
ctrl-l)
stored_query="$query"
__fuzzybrowse_previewFile "$sel"
;;
ctrl-e)
stored_query="$query"
"$EDITOR" "$sel"
;;
# ctrl-t)
# stored_query="$query"
# export e
# e="$(__fuzz=ybrowse_full_path "$(__fuzzybrowse_get_entry "$sel")")"
# __fuzzybrowse_runInTerminal "$SHELL"
# ;;
\\)
len=${#query}
stored_query="${query:0:$len}"
__fuzzybrowse_recursive=$((__fuzzybrowse_recursive==0))
;;
ctrl-r)
stored_query="$query"
__fuzzybrowse_recursive=$((__fuzzybrowse_recursive==0))
;;
ctrl-g)
sel="$(echo "$sel"| rev | cut -f2- -d'/' | rev)"
if [[ -d "$sel" ]]; then
pushd "$sel" > /dev/null 2>&1 || exit
fi
;;
esac
done
dirs -c
local x out_path
echo "$sel" | while read -r x; do
if [[ ! -f "$x" ]]; then
x=$(__fuzzybrowse_get_dir "$x")
fi
if [[ "$full_path" == 1 ]]; then
out_path="$(__fuzzybrowse_full_path "$x")"
else
#out_path="$(printf "%q\n" "$(__fuzzybrowse_relpath "$initial_dir" "$x")")"
out_path="$(__fuzzybrowse_relpath "$initial_dir" "$x")"
if [[ "$out_path" == "" ]]; then
out_path="$(pwd)"
fi
if [[ "${out_path: -1}" == "/" ]]; then
out_path="${out_path:0:-1}"
fi
fi
fasd -A "$out_path" > /dev/null 2>&1
if [[ -n "$out_file" ]]; then
echo "$out_path" >> "$out_file"
else
echo "$out_path"
fi
done
cd "$initial_dir" || return
export __fuzzybrowse_show_hidden=0
export __fuzzybrowse_recursive=0
}
__fuzzybrowse_show_hidden=0
__fuzzybrowse_recursive=0
__fuzzybrow_populate_dir_list(){
local line
if [[ "$__fuzzybrowse_recursive" == 1 ]]; then
while read -r line ; do
if [[ -d "$line" ]]; then
printf '\e[36m%s/\n' "$line"
else
printf '\e[0m%s\n' "$line"
fi
done
else
while read -r line ; do
if [[ -d "$line" ]]; then
if [[ "$__fuzzybrowse_use_fd" == 1 ]]; then
printf "\e[36m$line/\e[0m $(cd "$line" && fd --max-depth 1 --type f |head -9 | grep -v -i "$__fuzzybrow_file_ignore_pat" | tr "\\n" "|" | sed 's/|/\\\e[36m | \\\e[0m/g')\n"
else
printf "\e[36m$line/\e[0m $(cd "$line" && find . -maxdepth 1 -type f |head -9 | grep -v -i "$__fuzzybrow_file_ignore_pat" |cut -c3- | tr "\\n" "|" | sed 's/|/\\\e[36m | \\\e[0m/g')\n"
fi
fi
done
fi
}
__fuzzybrowse_file_source(){
local max_dep=1
if [[ -n "$1" ]]; then
max_dep="$1"
fi
# TODO: -xtype temporarily disabled, because of OSX. Fix.
# if [[ "$__fuzzybrowse_show_hidden" == 1 ]]; then
# find . "$@" -maxdepth "$max_dep" -type f -o -xtype f ! -iregex "$__fuzzybrow_file_ignore_pat" | cut -c3-
# else
# find . "$@" -maxdepth "$max_dep" \( -type f -o -xtype f \) -not -path '*/\.*' ! -iregex "$__fuzzybrow_file_ignore_pat" | cut -c3-
# fi
if [[ "$__fuzzybrowse_show_hidden" == 1 ]]; then
if [[ "$__fuzzybrowse_use_fd" == 1 ]]; then
fd --max-depth "$max_dep" --type f -H
else
find . "$@" -maxdepth "$max_dep" -type f -! -iregex "$__fuzzybrow_file_ignore_pat" | cut -c3-
fi
else
if [[ "$__fuzzybrowse_use_fd" == 1 ]]; then
fd --max-depth "$max_dep" --type f
else
find . "$@" -maxdepth "$max_dep" \( -type f \) -not -path '*/\.*' ! -iregex "$__fuzzybrow_file_ignore_pat" | cut -c3-
fi
fi
}
__fuzzybrowse_dir_source(){
local max_dep=1
if [[ -n "$1" ]]; then
max_dep="$1"
fi
if [[ "$__fuzzybrowse_show_hidden" == 1 ]]; then
if [[ "$__fuzzybrowse_use_fd" == 1 ]]; then
fd --max-depth "$max_dep" -H | __fuzzybrow_populate_dir_list
else
find . -maxdepth "$max_dep" \( -type d -o -type l \) ! -iregex "$__fuzzybrow_dir_ignore_pat" | tail -n +2 | cut -c3- | __fuzzybrow_populate_dir_list
fi
else
if [[ "$__fuzzybrowse_use_fd" == 1 ]]; then
fd --max-depth "$max_dep" | __fuzzybrow_populate_dir_list
else
find . -maxdepth "$max_dep" \( -type d -o -type l \) -not -path '*/\.*' ! -iregex "$__fuzzybrow_dir_ignore_pat" | tail -n +2 | cut -c3- | __fuzzybrow_populate_dir_list
fi
fi
}
__fuzzybrowse_all_source(){
if [[ "$__fuzzybrowse_show_hidden" == 1 ]]; then
if [[ "$__fuzzybrowse_use_fd" == 1 ]]; then
fd -H | __fuzzybrow_populate_dir_list
else
find . ! -iregex "$__fuzzybrow_dir_ignore_pat" ! -iregex "$__fuzzybrow_file_ignore_pat" | tail -n +2 | cut -c3- | __fuzzybrow_populate_dir_list
fi
else
if [[ "$__fuzzybrowse_use_fd" == 1 ]]; then
fd | __fuzzybrow_populate_dir_list
else
find . -not -path '*/\.*' ! -iregex "$__fuzzybrow_dir_ignore_pat" ! -iregex "$__fuzzybrow_file_ignore_pat" | tail -n +2 | cut -c3- | __fuzzybrow_populate_dir_list
fi
fi
}
__fuzzybrowse_get_dir(){
echo "$@" | rev |cut -f2- -d'/' | rev
}
__fuzzybrowse_get_entry(){
local tmp_dir
if [[ -f "$@" ]]; then
echo "$@"
else
tmp_dir=$(__fuzzybrowse_get_dir "$sel")
if [[ -d "$tmp_dir" ]]; then
echo "$tmp_dir"
else
echo "$@"
fi
fi
}
__fuzzybrowse_combined_source(){
local max_dep=1
if [[ "$__fuzzybrowse_recursive" == 1 ]]; then
max_dep=99
fi
if [[ "$__fuzzybrowse_sort" == 1 ]]; then
cat <(__fuzzybrowse_dir_source "$max_dep" | sort) <(__fuzzybrowse_file_source "$max_dep" | sort)
else
cat <(__fuzzybrowse_dir_source "$max_dep") <(__fuzzybrowse_file_source "$max_dep" )
fi
}
__fuzzybrowse_fzf_cmd(){
local prePrompt=""
if [[ "$__fuzzybrowse_recursive" == 1 ]]; then
prePrompt="REC"
fi
if [[ "$__fuzzybrowse_show_hidden" == 1 ]]; then
if [[ -n "$prePrompt" ]]; then
prePrompt+="|"
fi
prePrompt+="HID"
fi
if [[ -n "$prePrompt" ]]; then
prePrompt="{$prePrompt}"
fi
fzf "$fzf_opts" \
--reverse \
--multi \
--prompt="$prePrompt ""$(pwd): " \
--ansi \
--extended \
--print-query "$@" \
--tiebreak=begin,index \
--expect=\
ctrl-c,\
ctrl-x,\
ctrl-s,\
\#,\
return,\
ctrl-o,\
ctrl-u,\
\`,\
\\,\
ctrl-h,\
ctrl-z,\
ctrl-r,\
ctrl-e,\
ctrl-l,\
/,\
ctrl-v,\
left,\
right,\
ctrl-g,\
\>,\
ctrl-a,\
ctrl-t,\
ctrl-y,\
ctrl-q
#`# Hack to fix syntax highlight in vim..
}
__fuzzybrowse_full_path(){
local base
base="/$(basename "$1")"
if [[ "$base" == "/." ]]; then
base=""
fi
printf "%q" "$(cd "$(dirname "$1")"; pwd)$base"
}
__fuzzybrowse_relpath(){
# both $1 and $2 are absolute paths beginning with /
# returns relative path to $2/$target from $1/$source
source=$(__fuzzybrowse_full_path "$1")
target=$(__fuzzybrowse_full_path "$2")
if [[ "$source" == "$target" ]]; then
echo "$source"
return
fi
common_part="$source" # for now
result="" # for now
while [[ "${target#$common_part}" == "${target}" ]]; do
# no match, means that candidate common part is not correct
# go up one level (reduce common part)
common_part="$(dirname "$common_part")"
# and record that we went back, with correct / handling
if [[ -z $result ]]; then
result=".."
else
result="../$result"
fi
done
local no_common
no_common=0
if [[ $common_part == "/" ]]; then
# special case for root (no common path)
#result="$result/"
result="/"
no_common=1
fi
# since we now have identified the common part,
# compute the non-common part
forward_part="${target#$common_part}"
# and now stick all parts together
if [[ -n $result ]] && [[ -n $forward_part ]]; then
result="$result$forward_part"
elif [[ -n $forward_part ]]; then
# extra slash removal
result="${forward_part:1}"
fi
if [[ "${result:0:2}" == "//" ]]; then
result="${result:1}"
fi
if [[ $no_common == 0 && "${result:0:1}" != "." ]]; then
result="./$result"
fi
echo "$result"
}