-
Notifications
You must be signed in to change notification settings - Fork 1
/
fzf-search
executable file
·67 lines (55 loc) · 1.98 KB
/
fzf-search
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
#!/bin/bash
# # debug
# set -o xtrace
declare SRCDIR
SRCDIR=$(dirname $(readlink -e $0))
if [[ $1 =~ -h|--help ]]; then
man -l $SRCDIR/help.1
exit
fi
declare tmpdir
tmpdir=$(mktemp -d)
declare CACHE=$tmpdir/fzf-search-file-cache-${UID}.txt
trap "rm -rf $tmpdir" EXIT INT TERM
# QUERY is optionally set externally: QUERY='foo bar' fzf-search
declare QUERY _PATHS=("$@") # preserve spaces in path names
if [[ ! -t 0 ]]; then # stdin not attached to a tty
cat /dev/stdin > $CACHE
else
if [[ $# -gt 0 ]]; then
echo "${_PATHS[@]}" > $CACHE
else
rg --files > $CACHE
fi
fi
if [[ -n $NOCOLOR ]]; then
declare _COLOR=never
else
declare _COLOR=always
fi
if [[ -n $NOBINARY ]]; then
declare _NOBINARY=--no-binary
fi
# NOTE: fzf runs the command in a separate shell, can't use functions
declare RG="rg ${_NOBINARY} --column --line-number --no-heading --with-filename --color=${_COLOR} --smart-case"
declare RG_PIPE="cat $CACHE | xargs $RG"
declare FZF_DEFAULT_COMMAND="$RG_PIPE $QUERY || true"
declare FZF_DEFAULT_OPTS="--ansi --header-first --exact --multi"
declare VIEWER HEADER LESS_SCROLL="less +{2}g"
if [[ -f $(which bat 2> /dev/null) ]]; then
HEADER='~4,'
VIEWER="bat --style=full --color=always --paging=always -H {2} --pager '$LESS_SCROLL'"
else
VIEWER="$LESS_SCROLL -N '+/{q}'"
fi
declare PARSE_LINES="cat {+f} | cut -d: -f1 | uniq"
fzf --prompt='? ' --query "$QUERY" --disabled --delimiter : \
--header "« F1: help, C-s: recursive search, C-f: filter files, RET: view, M-RET: open »" \
--bind "f1:execute(man -l $SRCDIR/help.1 || true)" \
--preview "$VIEWER {1}" \
--preview-window "border-left:${HEADER},+{2}-3" \
--bind "enter:execute($VIEWER {1})" \
--bind "alt-enter:execute($EDITOR +{2} {1})" \
--bind "change:reload($RG_PIPE {q} || true)" \
--bind "ctrl-s:select-all+execute($PARSE_LINES | fzf-search || true)+deselect-all" \
--bind "ctrl-f:select-all+execute($PARSE_LINES | QUERY='{q}' fzf-file || true)+deselect-all"