-
Notifications
You must be signed in to change notification settings - Fork 4
/
kamp-grep
executable file
·67 lines (59 loc) · 1.67 KB
/
kamp-grep
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/sh
#
# Open files by content.
#
# requires:
# – fzf (https://github.com/junegunn/fzf)
# https://github.com/junegunn/fzf/blob/master/CHANGELOG.md#0190
# – ripgrep (https://github.com/BurntSushi/ripgrep)
# 'rg --no-heading --with-filename' are defaults when not printing to a terminal
# 'rg --column' implies --line-number
# - bat (https://github.com/sharkdp/bat)
set -euf
# define SHELL so --preview arguments do not error if current SHELL is not POSIX
SHELL=/bin/sh
usage() {
printf "Usage: %s: [-q query] -- [rg_options]\n" "$(basename "$0")" >&2
exit 2
}
qflag=
while getopts 'q:h' OPTION; do
case $OPTION in
q)
qflag=1
query="$OPTARG"
;;
h|?)
usage
;;
esac
done
shift $((OPTIND - 1))
rg_cmd="rg --color always --column $*"
if [ ! "$qflag" ]; then
query="$(kamp get opt kamp_grep_query)"
fi
if [ -z "$query" ]; then
export FZF_DEFAULT_COMMAND="rg --files $*"
else
pattern=$(printf %s "$query" | sed 's/"/\\"/')
export FZF_DEFAULT_COMMAND="$rg_cmd -- \"$pattern\""
fi
fzf \
--phony \
--query "$query" \
--delimiter ':' \
--ansi \
--bind "change:reload($rg_cmd -- {q} || true)" \
--bind 'enter:execute-silent(kamp send set global kamp_grep_query {q})+become(kamp edit {1} +{2}:{3})' \
--preview '
highlight_line={2}
line_range_begin=$((highlight_line - FZF_PREVIEW_LINES / 2))
bat \
--terminal-width $FZF_PREVIEW_COLUMNS \
--style=numbers \
--color=always \
--line-range "$((line_range_begin < 0 ? 1 : line_range_begin)):+$FZF_PREVIEW_LINES" \
--highlight-line {2} {1} 2> /dev/null' \
--header 'type to grep' \
--prompt 'grep> '