-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrg.sh
More file actions
executable file
·74 lines (58 loc) · 1.89 KB
/
rg.sh
File metadata and controls
executable file
·74 lines (58 loc) · 1.89 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
#!/usr/bin/env bash
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/rg.sh
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/rg.sh
## https://davoudarsalani.ir
## https://github.com/junegunn/fzf/wiki/Examples
source ~/main/scripts/helps.sh
source ~/main/scripts/utils.sh
source ~/main/scripts/utils-color.sh
title="${0##*/}"
function get_opt {
local options
options="$(getopt --longoptions 'help,case-sensitive,directory:' --options 'hcd:' --alternative -- "$@")"
eval set -- "$options"
while true; do
case "$1" in
-h|--help )
rg_help ;;
-c|--case-sensitive )
case_sensitive='--case-sensitive' ;;
-d|--directory )
shift
directory="$1" ;;
-- )
break ;;
esac
shift
done
}
case_sensitive=''
get_opt "$@"
heading "$title"
[ "$directory" ] || {
directory="$(select_directory)" 2>/dev/null || exit 37
}
## TODO find how to pass $directory to RG in JUMP_1 instead of cding
cd "$directory" || exit
short_pwd="$(to_tilda "$PWD")"
[ "$directory" == '.' ] && directory="$short_pwd" ## . -> ~/main/scripts
fzf__title="rg in $(to_tilda "$directory")"
main_item="$(pipe_to_fzf 'all' 'sh' 'python' 'html')" && wrap_fzf_choice "$main_item" || exit 37
case "$main_item" in
sh )
RG+=' --type sh' ;;
python )
RG+=' --type py' ;;
html )
RG+=' --type html' ;;
esac
[ "$case_sensitive" ] && RG+=" $case_sensitive"
## JUMP_1
INITIAL_QUERY=''
eval "$RG '$INITIAL_QUERY'" | \
fzf --preview 'eval head -n 100000 {-1} 2>/dev/null | \
eval "\rg $RG_MATCH_FLAGS" {q} 2>/dev/null' \
--header "rg in $(to_tilda "$PWD")" \
--bind "Return:reload:$RG {q} || true" --phony --query "$INITIAL_QUERY"