-
Notifications
You must be signed in to change notification settings - Fork 0
/
spot
executable file
·115 lines (95 loc) · 2.09 KB
/
spot
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
#!/usr/bin/env bash
# search directory defaults to current
dir=.
# case sensitive search
sensitive=
# colors enabled by default in ttys
if [ -t 1 ]; then
colors=1
else
colors=
fi
# line numbers shown by default
linenums=1
# ansi colors
cyan=`echo -e '\033[96m'`
reset=`echo -e '\033[39m'`
# usage info
function usage {
cat <<EOF
Usage: spot [options] [directory] [term ...]
Options:
-s, --sensitive Force case sensitive search.
-i, --insensitive Force case insensitive search.
-C, --no-colors Force avoid colors.
-L, --no-linenums Hide line numbers.
-h, --help This message.
EOF
}
# parse options
while [[ "$1" =~ "-" ]]; do
case $1 in
-s | --sensitive )
sensitive=1
;;
-i | --insensitive )
sensitive=
;;
-C | --no-colors )
colors=
;;
-L | --no-linenums )
linenums=
;;
-h | --help )
usage
exit
;;
esac
shift
done
# check for directory as first parameter
if [[ "$1" =~ / ]]; then
if [ -d "$1" ]; then
dir=`echo $1 | sed "s/\/$//"`
shift
fi
fi
# check for empty search
if [[ "" = "$@" ]]; then
echo "(no search term. \`spot -h\` for usage)"
exit 1
fi
# auto-detect case sensitive based on an uppercase letter
if [[ "$@" =~ [A-Z] ]]; then
sensitive=1
fi
# grep default params
grepopt="--binary-files=without-match --devices=skip"
# add case insensitive search
if [ ! $sensitive ]; then
grepopt="$grepopt --ignore-case"
fi
# add line number options
if [ $linenums ]; then
grepopt="$grepopt -n"
fi
# add force colors
if [ $colors ]; then
grepopt="$grepopt --color=always"
fi
# run search
if [ $colors ]; then
find $dir -type f ! -path '*/.git*' ! -path '*/.svn' -print0 \
| GREP_COLOR="1;33;40" xargs -0 grep $grepopt "`echo $@`" \
| sed "s/^\([^:]*:\)\(.*\)/ \\
$cyan\1$reset \\
\2 /"
else
find $dir -type f ! -path '*/.git*' ! -path '*/.svn' -print0 \
| xargs -0 grep $grepopt "$@" \
| sed "s/^\([^:]*:\)\(.*\)/ \\
\1 \\
\2 /"
fi
echo ""