aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/grep.bash
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2015-06-24 18:10:44 +1200
committerTom Ryder <tom@sanctum.geek.nz>2015-06-24 18:10:44 +1200
commitd184357767cfa266a6d8a32016f66e8e6bd1f17f (patch)
tree973f67cd0b5dbead3bcd9020820b6b5952577a7a /bash/bashrc.d/grep.bash
parentUse Bashy syntax to build GREP_OPTIONS more nicely (diff)
downloaddotfiles-d184357767cfa266a6d8a32016f66e8e6bd1f17f.tar.gz
dotfiles-d184357767cfa266a6d8a32016f66e8e6bd1f17f.zip
Undo hare-brained last few commits
GREP_OPTIONS doesn't work if it's not exported, which ought to have been painfully obvious. Oh well.
Diffstat (limited to 'bash/bashrc.d/grep.bash')
-rw-r--r--bash/bashrc.d/grep.bash43
1 files changed, 19 insertions, 24 deletions
diff --git a/bash/bashrc.d/grep.bash b/bash/bashrc.d/grep.bash
index 3605a925..3f8e44e0 100644
--- a/bash/bashrc.d/grep.bash
+++ b/bash/bashrc.d/grep.bash
@@ -1,39 +1,34 @@
+# Store whether we have colors in a variable
+declare -i colors
+colors=$( {
+ tput Co || tput colors
+} 2>/dev/null )
+
# 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
-declare -a grep_options
+# Use GREPOPTS to add some useful options to grep(1) calls if applicable; we
+# use a function wrapper to do this, rather than GREP_OPTIONS as we don't want
+# to change grep(1)'s actual behaviour inside scripts
+declare -a GREPOPTS
+if [[ -n $GREP_COLORS ]] && ((colors >= 8)) ; then
+ GREPOPTS=("${GREPOPTS[@]}" --color=auto)
+fi
if [[ $grep_help == *--binary-files* ]] ; then
- grep_options=("${grep_options[@]}" --binary-files=without-match)
+ GREPOPTS=("${GREPOPTS[@]}" --binary-files=without-match)
fi
if [[ $grep_help == *--exclude* ]] ; then
- grep_options=("${grep_options[@]}" --exclude={.gitignore,.gitmodules})
+ GREPOPTS=("${GREPOPTS[@]}" --exclude={.gitignore,.gitmodules})
fi
if [[ $grep_help == *--exclude-dir* ]] ; then
- grep_options=("${grep_options[@]}" --exclude-dir={.cvs,.git,.hg,.svn})
+ GREPOPTS=("${GREPOPTS[@]}" --exclude-dir={.cvs,.git,.hg,.svn})
fi
-GREP_OPTIONS=${grep_options[*]}
-unset -v grep_options
-# We're done parsing grep(1)'s --help output now
-unset -v grep_help
-
-# 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
+# Done, unset helper vars
+unset -v grep_help colors
# 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
+ command grep "${GREPOPTS[@]}" "$@"
}