ohmyzsh/lib/grep.zsh
Eric D. Kjeldergaard c21685c13e Grep version check for exclude argument format
Make some updates inspired by ncancelll's comments.
2014-04-18 11:07:24 -05:00

39 lines
1.1 KiB
Bash

#
# Color grep results
# Examples: http://rubyurl.com/ZXv
#
GREP_OPTIONS=
# returns 0 if the first version number is greater than or equal to the second.
# returns 1 if the first version number is greater than or equal to the second.
function versionCompare() {
local LEAST_VERSION=$(echo -e "$1\n$2" | sort -t '.' -g | head -n 1)
if [[ $LEAST_VERSION = $1 ]]; then
return 0
fi
return 1
}
# avoid VCS folders. This chooses a different argument based on grep version.
# grep version 2.5+ is required for any support of this feature.
function ignoreVCS() {
local GREP_VERSION=$(grep --version | head -n 1 | sed -e 's/^.* \([0-9]\{1,\}\(\.[0-9a-z]\{1,\}\)\{1,\}\)-.*/\1/')
for PATTERN in .cvs .git .hg .svn; do
# logically, if(GREP_VERSION >= 2.5.3)
if versionCompare "2.5.3" $GREP_VERSION; then
GREP_OPTIONS+="--exclude-dir=$PATTERN "
# logically, if(GREP_VERSION >= 2.5)
elif versionCompare "2.5" $GREP_VERSION; then
GREP_OPTIONS+="--exclude=$PATTERN "
fi
done
}
ignoreVCS;
unfunction versionCompare
unfunction ignoreVCS
GREP_OPTIONS+="--color=auto"
export GREP_OPTIONS="$GREP_OPTIONS"
export GREP_COLOR='1;32'