blob: a242603d96cac882c9fef66acc21436c86a484d0 (
plain) (
blame)
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
|
# Store grep(1)'s --help output in a variable
grep_help=$(grep --help 2>/dev/null)
# Use GREP_OPTIONS to add some useful options to grep(1) calls if applicable
GREP_OPTIONS=
case $grep_help in
*--binary-files*)
GREP_OPTIONS=${GREP_OPTIONS:+$GREP_OPTIONS }'--binary-files=without-match'
;;
esac
case $grep_help in
*--exclude*)
for exclude in .gitignore .gitmodules ; do
GREP_OPTIONS=${GREP_OPTIONS:+$GREP_OPTIONS }'--exclude='$exclude
done
unset -v exclude
;;
esac
case $grep_help in
*--exclude-dir*)
for exclude_dir in .cvs .git .hg .svn ; do
GREP_OPTIONS=${GREP_OPTIONS:+$GREP_OPTIONS }'--exclude-dir='$exclude_dir
done
unset -v exclude_dir
;;
esac
# We're done parsing grep(1)'s --help output now
unset -v grep_help
# Export the grep(1) options if we decided on any
if [ -n "$GREP_OPTIONS" ] ; then
export GREP_OPTIONS
fi
# Define function wrapper for grep(1) with --color option if GREP_COLORS is
# set; checks that color is available in the terminal within the function
if [[ ! -n $GREP_COLORS ]] ; then
return
fi
# Define function proper
grep() {
local -i colors
colors=$( {
tput Co || tput colors
} 2>/dev/null )
if ((colors >= 8)) ; then
command grep --color=auto "$@"
else
command grep "$@"
fi
}
|