forked from vbauerster/kamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkamp-grep
executable file
·58 lines (50 loc) · 1.48 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
#!/bin/sh
#
# Open files by content.
#
# Usage:
#
# kamp-grep [<query=...>] [rg_options]
# 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
query="$(kamp get opt kamp_grep_query)"
if [ $# -ne 0 ]; then
case "$1" in
query=*)
query="${1#*=}"
shift
;;
esac
fi
rg_cmd="rg $*"
if [ -z "$query" ]; then
export FZF_DEFAULT_COMMAND="$rg_cmd --files"
else
export FZF_DEFAULT_COMMAND="$rg_cmd --color always --column -- '$query'"
fi
fzf \
--phony \
--query "${query:-}" \
--delimiter ':' \
--ansi \
--bind "change:reload($rg_cmd --color always --column -- {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> '