-
Notifications
You must be signed in to change notification settings - Fork 0
/
qgrep
executable file
·186 lines (169 loc) · 4.57 KB
/
qgrep
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
#!/bin/bash
# Fast grep tool to use when it's to much of a bother to install rg, ag or pt.
#
# When in git folder, use `git grep` which should be faster than any tool. When
# not in git folder, use `find` with file extensions matching and folder
# prunning to quickly list important files and then process those files in
# parallel with `xargs`.
# disable pathname expansion for 'find -name *.f90' command
set -f
PRUNED_DIR="build doc"
EXCLUDED_EXTENSIONS="mod mod.stamp"
EXTENSIONS="f90 c h cu cuf cpp hpp"
FORCE_FIND=0
VERBOSE=0
PAGER=0
DEBUG=0
COLOR=always
VIM=0
CASE_SENSITIVE="i"
function usage () {
cat <<EOF
Usage:
$(basename $0) [options] pattern
-v Echo the command used to search for pattern
-f Use find command to list files (default when not in git folder)
-l Dump output with the pager less
-nc No color
-vi Format for easy vim copy/paste
-I Case sensitive search pattern
-i LIST File extensions (case-insensitive) included by find (default: "$EXTENSIONS")
-x LIST File extensions (case-insensitive) excluded by find (default: "$EXCLUDED_EXTENSIONS")
-p LIST Folder prefixes (case-insensitive) to prune by find (default: "$PRUNED_DIR")
-ai LIST Append LIST to the file extensions (case-insensitive) included by default
-ax LIST Append LIST to the file extensions (case-insensitive) excluded by default
-ap LIST Append LIST to the folder prefixes (case-insensitive) pruned by default
EOF
}
# -po Prompt to open file
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-i)
EXTENSIONS="$2"
shift
shift
;;
-x)
EXCLUDED_EXTENSIONS="$2"
shift
shift
;;
-p)
PRUNED_DIR="$2"
shift
shift
;;
-ai)
EXTENSIONS="$EXTENSIONS $2"
shift
shift
;;
-ax)
EXCLUDED_EXTENSIONS="$EXCLUDED_EXTENSIONS $2"
shift
shift
;;
-ap)
PRUNED_DIR="$PRUNED_DIR $2"
shift
shift
;;
-v)
VERBOSE=1
shift
;;
-f)
FORCE_FIND=1
shift
;;
-l)
PAGER=1
shift
;;
-nc)
COLOR=never
shift
;;
-vi)
VIM=1
shift
;;
-I)
CASE_SENSITIVE=""
shift
;;
-ddd)
DEBUG=1
shift
;;
-h|-help|--help)
usage
exit 0
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [[ $1 =~ ^-.* ]]; then
>&2 echo "Error. Invalid option: $1."
>&2 usage
exit 1
fi
if [[ $# != 1 ]]; then
>&2 echo "Error. Too many arguments: $@."
>&2 usage
exit 1
fi
dir=.
#dir=${2:-.} TODO
# Check if git repo
if [[ $FORCE_FIND != 1 ]] && (cd ${dir} ; git branch >/dev/null 2>/dev/null); then
#finder="git ls-files -z"
finder="true" # useless command
handler="git grep --color=$COLOR -HIn${CASE_SENSITIVE}"
pfinder="$finder"
else
# List folders to ignore
prune=""
for f in $PRUNED_DIR; do
prune="$prune -iname $f* -prune -or "
done
[[ $prune != "" ]] && prune="-type d ( $(echo $prune | sed 's/-or$//') ) -or"
# List extensions to ignore
eprune=""
for f in $EXCLUDED_EXTENSIONS; do
eprune="$eprune -iname *.$f -prune -or "
done
[[ $eprune != "" ]] && eprune="-type f ( $(echo $eprune | sed 's/-or$//') ) -or"
# List file type to include
extensions=""
for e in $EXTENSIONS; do
extensions="$extensions -iname *.$e -or "
done
[[ $extensions != "" ]] && extensions="-type f ( $(echo $extensions | sed 's/-or$//') )"
finder="find ${dir} $prune $eprune $extensions -type f -print0"
handler="xargs -0 -P 0 -n 40 grep --color=$COLOR -HIn${CASE_SENSITIVE}"
# Replace '(' by '\(' so the echoed command can be used by copy-pasting
pfinder="$(echo $finder | sed 's/(/\\(/g;s/)/\\)/g')"
fi
if [[ $DEBUG == 1 ]]; then
echo "Included: $EXTENSIONS"
echo "Excluded: $EXCLUDED_EXTENSIONS"
echo "Pruned: $PRUNED_DIR"
exit 1
fi
[[ $PAGER == 0 ]] && PAGER="cat" || PAGER="less -nRF"
# List files to look into, search for pattern in parallel, change the matched lines info to be vim friendly
if [[ $VIM == 1 ]]; then
${finder} | LC_ALL=C ${handler} "$1" | sed -e 's/:\([^ ]*\):/ +\1 # /' -e 's/^/vi /' | $PAGER
[[ $VERBOSE == 1 ]] && echo "$pfinder | LC_ALL=C ${handler} \"$1\" | sed -e 's/:\([^ ]*\):/ +\1 # /' -e 's/^/vi /' | $PAGER"
else
${finder} | LC_ALL=C ${handler} "$1" | $PAGER
[[ $VERBOSE == 1 ]] && echo "$pfinder | LC_ALL=C ${handler} \"$1\" | $PAGER"
fi