-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathkamp-grep
More file actions
executable file
·82 lines (73 loc) · 2.17 KB
/
kamp-grep
File metadata and controls
executable file
·82 lines (73 loc) · 2.17 KB
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
#!/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() {
# passing -F to rg_options implies it must be last in the sequence;
# otherwise switching between fixed/regex mode will fail
printf "Usage: %s: [-q query] -- [rg_options]\n" "$(basename "$0")" >&2
exit 2
}
qflag=
while getopts hq: OPTION; do
case "$OPTION" in
q)
qflag=1
query="$OPTARG"
;;
h|?)
usage
;;
esac
done
shift $((OPTIND - 1))
ctrlx_bind=
case "$*" in
*-F)
ctrlx_bind="ctrl-x:become:$0 -q {q} -- ${*%-F*}"
;;
*)
ctrlx_bind="ctrl-x:become:$0 -q {q} -- $* -F"
;;
esac
rg_cmd="rg --column --color=always $*"
if [ ! "$qflag" ]; then
query="$(kamp get opt kamp_grep_query)"
fi
if [ -z "$query" ]; then
export FZF_DEFAULT_COMMAND="rg --files $*"
elif printf %q > /dev/null 2>/dev/null; then
export FZF_DEFAULT_COMMAND="$(printf '%s -- %q' "$rg_cmd" "$query")"
else
# printf doesn't support %q fallback instead of failure
qq=$(echo "$query" | sed 's/"/\\"/g')
export FZF_DEFAULT_COMMAND="$rg_cmd -- \"$qq\""
fi
fzf \
--disabled --ansi \
--delimiter : \
--query "$query" \
--prompt "rg($*)> " \
--header '<c-x> toggle fixed/regex mode' \
--bind "$ctrlx_bind" \
--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'