This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fzftools.bash
504 lines (410 loc) · 13.3 KB
/
fzftools.bash
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
#!/usr/bin/env bash
# fzftools - a collection of fzf scripts
# Copyright © 2017 midchildan
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
fzf-sel() {
(( $# < 1 )) && fzf::_abort "Insufficient number of arguments."
local IFS=' '
local selector
read -ra selector <<< "$*"
if ! fzf::_is_function "fzf::sel::${selector[0]}" ; then
fzf::_abort "Unsupported selector ${selector[0]}"
fi
"fzf::sel::${selector[0]}" "${selector[@]:1}"
}
fzf-run() {
(( $# < 1 )) && fzf::_abort "Insufficient number of arguments."
local cmd="$1"
shift
if fzf::_is_function "fzf::run::$cmd" ; then
"fzf::run::$cmd" "$@"
else
fzf::run::_common "$cmd" "$@"
fi
}
fzf-loop() {
while true; do fzf-run "$@"; done
}
fzf-gen() {
local IFS=','
local conf_dir="${FZF_CONF_DIR:-$HOME/.config/shell}"
local index_path="$conf_dir/templates.csv"
local template_dir="$conf_dir/templates"
local entry
local index="$(fzf::sel::template)"
entry=($(sed "${index}q;d" "$index_path"))
local dst="${entry[0]}"
local src="$template_dir/${entry[1]}"
local type="${entry[3]}"
local permission="${entry[4]}"
local editor="vim"
[[ "$EDITOR" =~ *vim ]] && editor="$EDITOR"
case "$type" in
plain) cp "$src" "$dst" ;;
script) "$src" > "$dst" ;;
snippet) "$editor" "$dst" "+call UltiSnips#Anon(join(readfile(\"$src\"), \"\\n\"))" ;;
*) fzf::_abort "Invalid template type" ;;
esac
[[ "$permission" != "default" ]] && chmod "$permission" "$dst"
true
}
#######################
# utility functions #
#######################
# Print an error message and return to top-level shell
# Arguments:
# error_message [default: abort]
# Returns:
# None
fzf::_abort() {
local message="abort"
[[ -n "$1" ]] && message="$1"
printf "[%s:%s] %s" "${FUNCNAME[1]}" "${BASH_LINENO[0]}" "$message" >&2
kill -INT "$$"
}
# Checks whether a function exisits with the given name
# Arguments:
# name
# Returns:
# 0 if name is a function, 1 otherwise
fzf::_is_function() {
declare -fF "$1" > /dev/null 2>&1
}
# Checks whether a command exisits with the given name
# Arguments:
# name
# Returns:
# 0 if name is a command, 1 otherwise
fzf::_is_command() {
command -v "$1" > /dev/null 2>&1
}
# Checks whether the current directory is a git repository
# Arguments:
# None
# Returns:
# 0 if the current directory is git repository, non-zero otherwise
fzf::_is_git_repo() {
git rev-parse > /dev/null 2>&1
}
# Runs FZF and aborts if selection was canceled
# Arguments:
# args... : Arguemnts to pass to FZF
# Returns:
# None
fzf::_fzf_or_abort() {
fzf-tmux "$@" || fzf::_abort "FZF selection canceled."
}
############################
# common select commands #
############################
fzf::sel::dir() {
find -L "${1:-.}" -mindepth 1 -path '*/\.*' -prune -o -type d -print \
2> /dev/null \
| fzf::_fzf_or_abort -m --preview "ls -l {}" --bind=ctrl-g:toggle-preview \
--header 'Press CTRL-G to toggle preview'
}
fzf::sel::dirstack() {
dirs -l -p \
| fzf::_fzf_or_abort -m --preview "ls -l {}" --bind=ctrl-g:toggle-preview \
--header 'Press CTRL-G to toggle preview'
}
fzf::sel::file() {
find -L "${1:-.}" -mindepth 1 \
-name .git -prune -o -name .svn -prune -o \( -type d -o -type f -o -type l \) \
-a -not -path "$1" -print 2> /dev/null \
| sed 's#^\./##' \
| fzf::_fzf_or_abort -m --preview "head -$LINES {}" \
--bind=ctrl-g:toggle-preview --header 'Press CTRL-G to toggle preview'
}
fzf::sel::fc() {
fc -l 1 \
| fzf::_fzf_or_abort +s --tac -n2..,.. --tiebreak=index \
--bind=ctrl-s:toggle-sort --header 'Press CTRL-S to toggle sort' \
--preview 'echo {}' --preview-window down:3:wrap \
| cut -f1
}
fzf::sel::process() {
ps -ef \
| sed 1d \
| fzf::_fzf_or_abort -m --preview 'echo {}' --preview-window down:3:wrap \
| awk '{print $2}'
}
fzf::sel::template() {
local conf_dir="${FZF_CONF_DIR:-$HOME/.config/shell}"
local index_path="$conf_dir/templates.csv"
local template_dir="$conf_dir/templates"
cat "$index_path" \
| awk -F, \
"{ printf \"%d $(tput setaf 2)%s$(tput sgr0) (%s)\\t%s\\n\", NR, \$1, \$2, \$3 }" \
| fzf::_fzf_or_abort +m --ansi --with-nth=2.. \
| cut -d' ' -f1
}
#########################
# common run commands #
#########################
function fzf::run::fc() {
local IFS=$'\n'
local selected
selected=($(fzf::sel::fc))
if [[ -n "$@" ]]; then
fc "$@" "${selected[@]}"
else
fc "${selected[@]}"
fi
}
# Runs a command on an item selected using FZF
# Arguments:
# cmd : command to execute
# item_type : type of item [file/dir/dirstack/fc/process/...]
# Returns:
# None
fzf::run::_common() {
(( $# < 2 )) && fzf::_abort "Insufficient number of arguments."
local IFS=$'\n'
local cmd="$1"
local item_type="$2"
shift 2
local selected
selected=($(fzf-sel "$item_type"))
[[ -z "$selected" ]] && fzf::_abort
if [[ -n "$@" ]]; then
"$cmd" "$@" "${selected[@]}"
else
"$cmd" "${selected[@]}"
fi
}
##########################
# brew select commands #
##########################
fzf::sel::brew() {
(( $# < 1 )) && fzf::_abort "Insufficient number of arguments."
fzf::_is_command "brew" || fzf::_abort "Homebrew not installed."
local cmd="$1"
shift
if ! fzf::_is_function "fzf::sel::brew::$cmd" ; then
fzf::_abort "Invalid selector $cmd"
fi
"fzf::sel::brew::$cmd" "$@"
}
fzf::sel::brew::formula() {
brew search \
| fzf::_fzf_or_abort -m --preview-window right:70% --preview 'brew info {}'
}
fzf::sel::brew::installed() {
brew ls \
| fzf::_fzf_or_abort -m --preview-window right:70% --preview 'brew info {}'
}
fzf::sel::brew::leaves() {
brew leaves \
| fzf::_fzf_or_abort -m --preview-window right:70% --preview 'brew info {}'
}
fzf::sel::brew::cask() {
(( $# < 1 )) && fzf::_abort "Insufficient number of arguments."
if ! fzf::_is_function "fzf::sel::brew::cask::$1" ; then
fzf::_abort "Invalid selector $1"
fi
"fzf::sel::brew::cask::$1"
}
fzf::sel::brew::cask::cask() {
brew cask search \
| fzf::_fzf_or_abort -m --preview-window right:70% --preview 'brew cask info {}'
}
fzf::sel::brew::cask::installed() {
brew cask ls \
| fzf::_fzf_or_abort -m --preview-window right:70% --preview 'brew cask info {}'
}
#######################
# brew run commands #
#######################
fzf::run::brew() {
(( $# < 1 )) && fzf::_abort "Insufficient number of arguments."
local cmd="$1"
shift
if fzf::_is_function "fzf::run::brew::$cmd" ; then
"fzf::run::brew::$cmd" "$@"
else
fzf::run::brew::_common "$cmd" "$@"
fi
}
fzf::run::brew::cask() {
(( $# < 1 )) && fzf::_abort "Insufficient number of arguments."
local cmd="$1"
shift
if fzf::_is_function "fzf::run::brew::cask::$cmd" ; then
"fzf::run::brew::cask::$cmd" "$@"
else
fzf::run::brew::cask::_common "$cmd" "$@"
fi
}
# Runs a brew command on an item selected using FZF
# Arguments:
# cmd : brew command to execute
# item_type : type of item [formula/installed/leaves]
# flags : additional flags to pass to brew [optional]
# Returns:
# None
fzf::run::brew::_common() {
(( $# < 2 )) && fzf::_abort "Insufficient number of arguments."
local IFS=$'\n'
local cmd="$1"
local item_type="$2"
shift 2
local selected
selected=($(fzf::sel::brew "$item_type"))
brew "$cmd" "$@" "${selected[@]}"
}
# Runs a cask command on an item selected using FZF
# Arguments:
# cmd : cask command to execute
# item_type : type of item [cask/installed]
# flags : additional flags to pass to cask [optional]
# Returns:
# None
fzf::run::brew::cask::_common() {
(( $# < 2 )) && fzf::_abort "Insufficient number of arguments."
local IFS=$'\n'
local cmd="$1"
local item_type="$2"
shift 2
local selected
selected=($(fzf::sel::brew::cask "$item_type"))
brew cask "$cmd" "$@" "${selected[@]}"
}
#########################
# git select commands #
#########################
fzf::sel::git() {
(( $# < 1 )) && fzf::_abort "Insufficient number of arguments."
if ! fzf::_is_function "fzf::sel::git::$1" ; then
fzf::_abort "Invalid selector $1"
fi
"fzf::sel::git::$1"
}
# https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236
fzf::sel::git::branch() {
fzf::_is_git_repo || fzf::_abort "Not a git repository."
local preview
preview='git log --oneline --graph --date=short'
preview+=' --pretty="format:%C(auto)%cd %h%d %s"'
preview+=' $(sed s/^..// <<< {} | cut -d" " -f1) | head -'$LINES
git branch -a --color=always \
| grep -v '/HEAD\s' \
| sort \
| fzf::_fzf_or_abort -m --ansi --tac \
--preview-window right:70% --preview "$preview" \
--bind=ctrl-g:toggle-preview --header 'Press CTRL-G to toggle preview' \
| sed 's/^..//' \
| cut -d' ' -f1 \
| sed 's#^remotes/##'
}
# https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236
fzf::sel::git::commit() {
fzf::_is_git_repo || fzf::_abort "Not a git repository."
local preview
preview='grep -o "[a-f0-9]\{7,\}" <<< {}'
preview+=' | xargs git show --color=always | head -'$LINES
git log --graph --color=always --date=short \
--format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" \
| fzf::_fzf_or_abort -m +s --ansi --reverse --preview "$preview" \
--bind 'ctrl-s:toggle-sort,ctrl-g:toggle-preview' \
--header 'Press CTRL-S to toggle sort, CTRL-G to toggle preview' \
| grep -o "[a-f0-9]\{7,\}"
}
fzf::sel::git::file() {
fzf::_is_git_repo || fzf::_abort "Not a git repository."
# https://github.com/junegunn/fzf#git-ls-tree-for-fast-traversal
(git ls-tree -r --name-only HEAD \
|| find . -path '*/\.*' -prune -o -type f -print -o -type l -print \
| sed 's/\..//' \
) | fzf::_fzf_or_abort -m \
--preview "if [[ -d {} ]]; then ls -l {}; else head -$LINES {}; fi" \
--bind=ctrl-g:toggle-preview --header 'Press CTRL-G to toggle preview'
}
# https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236
fzf::sel::git::remote() {
fzf::_is_git_repo || fzf::_abort "Not a git repository."
local preview
preview='git log --oneline --graph --date=short'
preview+=' --pretty="format:%C(auto)%cd %h%d %s" {1} | head -200'
git remote -v \
| awk '{print $1 "\t" $2}' \
| uniq \
| fzf::_fzf_or_abort --tac --preview="$preview" \
| cut -d$'\t' -f1
}
fzf::sel::git::stash() {
fzf::_is_git_repo || fzf::_abort "Not a git repository."
local preview
preview='grep -o "[a-f0-9]\{7,\}" <<< {}'
preview+=' | xargs git show --color=always | head -'$LINES
git stash list --pretty="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" \
| fzf::_fzf_or_abort -m +s --ansi --reverse --preview="$preview" \
--bind 'ctrl-s:toggle-sort,ctrl-g:toggle-preview' \
--header 'Press CTRL-S to toggle sort, CTRL-G to toggle preview' \
| grep -o "[a-f0-9]\{7,\}"
}
# https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236
fzf::sel::git::status() {
fzf::_is_git_repo || fzf::_abort "Not a git repository."
local preview
preview='(git diff --color=always -- {-1} | sed 1,4d; cat {-1}) | head -500'
git -c color.status=always status --short \
| fzf::_fzf_or_abort -m --ansi --nth 2..,.. --preview="$preview" \
--bind=ctrl-g:toggle-preview --header 'Press CTRL-G to toggle preview' \
| cut -c4- \
| sed 's/.* -> //'
}
# https://gist.github.com/junegunn/8b572b8d4b5eddd8b85e5f4d40f17236
fzf::sel::git::tag() {
fzf::_is_git_repo || fzf::_abort "Not a git repository."
git tag --sort --version:refname \
| fzf::_fzf_or_abort -m --preview-window right:70% \
--preview 'git show --color=always {} | head -'$LINES
}
######################
# git run commands #
######################
fzf::run::git() {
(( $# < 1 )) && fzf::_abort "Insufficient number of arguments."
local cmd="$1"
shift
if fzf::_is_function "fzf::run::git::$cmd" ; then
"fzf::run::git::$cmd" "$@"
else
fzf::run::git::_common "$cmd" "$@"
fi
}
# Runs a git command on an item selected using FZF
# Arguments:
# cmd : git command to execute
# item_type : type of item [branch/commit/file/remote/stash/status/tag]
# flags : additional flags to pass to git [optional]
# Returns:
# None
fzf::run::git::_common() {
(( $# < 2 )) && fzf::_abort "Insufficient number of arguments."
local IFS=$'\n'
local cmd="$1"
local item_type="$2"
shift 2
local selected
selected=($(fzf::sel::git "$item_type"))
git "$cmd" "$@" "${selected[@]}"
}