-
Notifications
You must be signed in to change notification settings - Fork 32
/
fv
executable file
·208 lines (185 loc) · 4.09 KB
/
fv
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
#!/usr/bin/env bash
usage() {
LESS=-FEXR less <<'HELP'
fv [OPTIONS] [SEARCH]
fuzzy file filtering and command executing
a) allfiles=1 ;;
c) cmd="$OPTARG" ;;
d) dtach=1 ;;
o) loop=1 ;;
s) small=1 ;;
-a search all dirs and hidden files (possibly quirky)
-c command to execute [defaults to vim]
-d run in background (for ie non-terminal programs)
-h show this help
-l additional arguments to pass to filtering program
-o run continuously
-s run in a smaller window
HELP
}
declare cmd=''
declare cmdopts=()
declare search_str=''
declare search_cmd=''
declare search_opts=()
declare allfiles=''
declare loop
declare small
declare -A colors
colors[red]=$(tput setaf 1)
colors[green]=$(tput setaf 2)
colors[blue]=$(tput setaf 4)
colors[reset]=$(tput sgr0)
color() {
local c
c="$1"
shift
printf '%s' "${colors[$c]}"
printf '%s\n' "$@"
printf '%s' "${colors[reset]}"
}
err() {
color red "$@" >&2
}
die() {
[[ -n "$1" ]] && err "$1"
exit 1
}
has() {
local o c verbose
verbose=0
while getopts 'v' o; do
case "$o" in
v) v=1 ;;
esac
done
shift "$((OPTIND-1))"
for c; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
(( verbose > 0 )) && err "$c not found"
return 1
fi
done
}
select_from() {
local o c cmd OPTARG OPTIND
cmd='command -v'
while getopts 'c:' o; do
case "$o" in
c) cmd="$OPTARG" ;;
esac
done
shift "$((OPTIND-1))"
for c; do
if $cmd "${c%% *}" &> /dev/null; then
echo "$c"
return 0
fi
done
return 1
}
while getopts 'ac:dhlos' opt; do
case "$opt" in
a) allfiles=1 ;;
c) cmd="$OPTARG" ;;
d) dtach=1 ;;
h) usage; exit 0 ;;
l) search_opts+=( '-l' ) ;;
o) loop=1 ;;
s) small=1 ;;
esac
done
shift "$((OPTIND-1))"
has -v 'fzf' || die
if [[ -v FV_CMD ]]; then
cmd="$FV_CMD"
elif [[ -z "$cmd" ]]; then
cmd=$(select_from 'v' 'vim')
fi
if [[ -v FV_SEARCH ]]; then
search_cmd="$FV_SEARCH"
else
search_cmd=$(select_from 'ag' 'ack' 'grep')
fi
if [[ "$search_cmd" == 'grep' ]]; then
err 'grep is slow, you should strongly consider installing ag or ack'
sleep .75
fi
if [[ -n "$1" ]]; then
if [[ -e "$1" ]]; then
search_opts+=( "$1" )
else
search_str="$1"
fi
shift
fi
case "$search_cmd" in
'ag')
search_opts+=( '--color' )
if [[ -n "$allfiles" ]]; then
search_opts+=( '-u' '--hidden' )
fi
if [[ "$search_str" == '' ]]; then
search_opts+=( '-l' )
fi ;;
'ack')
if [[ "$search_str" == '' ]]; then
if [[ -z "$allfiles" ]]; then
search_opts+=( '-f' )
else
search_opts+=( '-g' '^[^\.]' )
fi
else
search_opts+=( '-l' )
# search_opts+=( '--match' )
fi ;;
'grep')
search_opts+=( '-r' '-I' )
if [[ -z "$allfiles" ]]; then
if [[ -r ~/.ignore ]]; then
while read -r line; do
search_opts+=( "--exclude-dir=$line" )
done < ~/.ignore
else
search_opts+=( '--exclude-dir=bower_components' )
search_opts+=( '--exclude-dir=node_modules' )
search_opts+=( '--exclude-dir=jspm_packages' )
search_opts+=( '--exclude-dir=.cvs' )
search_opts+=( '--exclude-dir=.git' )
search_opts+=( '--exclude-dir=.hg' )
search_opts+=( '--exclude-dir=.svn' )
fi
fi
if [[ -z "$search_str" ]]; then
search_opts+=( -F '' )
else
search_opts+=( -P )
fi ;;
esac
if [[ "$search_str" != '' ]]; then
search_opts+=( "$search_str" )
fi
main() {
local highlight
highlight=$(select_from 'bat --color=always --style=header' 'highlight -q --force -O ansi')
choices=$($search_cmd "${search_opts[@]}" 2> /dev/null |
fzf --ansi --multi --preview="[[ \$(file -ib {}) = *text* ]] && ${highlight} {}") || exit 1
if [[ "$search_str" != '' ]]; then
if [[ $search_cmd == 'ag' ]]; then
choices=$(cut -d: -f1 <<< "$choices")
fi
fi
mapfile -t choices <<< "$choices"
if [[ $dtach ]]; then
($cmd "${cmdopts[@]}" "${choices[@]}" &> /dev/null &)
else
$cmd "${cmdopts[@]}" "${choices[@]}"
fi
}
if [[ -n "$loop" ]]; then
while main; do
true
done
else
main
fi