dc190d872aa37dc0440284bee6c6bd7744007bf0

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Refactor grep.zsh file

- Move grep-alias path to variable.
- Use <<< "" instead of piped echo to check grep flags.
- Remove check for --color only since it's the same release as --exclude.

Diff

 1diff --git a/lib/grep.zsh b/lib/grep.zsh
 2index 933de2990eb7992932871d00e792cc45a2a1d21f..1d94eb97895bbf94f567c2f164d16caac921fe34 100644
 3--- a/lib/grep.zsh
 4+++ b/lib/grep.zsh
 5@@ -1,39 +1,37 @@
 6-# see if we already cached the grep alias in past day
 7-_grep_alias_cache=("$ZSH_CACHE_DIR"/grep_alias.zsh(Nm-24))
 8-if (( $#_grep_alias_cache )); then
 9-    source "$ZSH_CACHE_DIR"/grep_alias.zsh
10-else
11-    # is x grep argument available?
12+__GREP_CACHE_FILE="$ZSH_CACHE_DIR"/grep-alias
13+
14+# See if there's a cache file modified in the last day
15+__GREP_ALIAS_CACHES=("$__GREP_CACHE_FILE"(Nm-1))
16+if [[ -z "$__GREP_ALIAS_CACHES" ]]; then
17     grep-flags-available() {
18-        echo | grep "$@" "" >/dev/null 2>&1
19+        command grep "$@" "" &>/dev/null <<< ""
20     }
21 
22-    GREP_OPTIONS=""
23-
24-    # ignore these folders (if the necessary grep flags are available)
25+    # Ignore these folders (if the necessary grep flags are available)
26     EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}"
27 
28+    # Check for --exclude-dir, otherwise check for --exclude. If --exclude
29+    # isn't available, --color won't be either (they were released at the same
30+    # time (v2.5): http://git.savannah.gnu.org/cgit/grep.git/tree/NEWS?id=1236f007
31     if grep-flags-available --color=auto --exclude-dir=.cvs; then
32-        GREP_OPTIONS+="--color=auto --exclude-dir=$EXC_FOLDERS"
33+        GREP_OPTIONS="--color=auto --exclude-dir=$EXC_FOLDERS"
34     elif grep-flags-available --color=auto --exclude=.cvs; then
35-        GREP_OPTIONS+="--color=auto --exclude=$EXC_FOLDERS"
36-    elif grep-flags-available --color=auto; then
37-        GREP_OPTIONS+="--color=auto"
38+        GREP_OPTIONS="--color=auto --exclude=$EXC_FOLDERS"
39     fi
40 
41     {
42         if [[ -n "$GREP_OPTIONS" ]]; then
43             # export grep, egrep and fgrep settings
44-            echo alias grep="'grep $GREP_OPTIONS'"
45-            echo alias egrep="'egrep $GREP_OPTIONS'"
46-            echo alias fgrep="'fgrep $GREP_OPTIONS'"
47+            echo "alias grep='grep $GREP_OPTIONS'"
48+            echo "alias egrep='egrep $GREP_OPTIONS'"
49+            echo "alias fgrep='fgrep $GREP_OPTIONS'"
50         fi
51-    } > "$ZSH_CACHE_DIR/grep_alias.zsh"
52+    } > "$__GREP_CACHE_FILE"
53 
54-    source "$ZSH_CACHE_DIR/grep_alias.zsh"
55-
56-    # clean up
57+    # Clean up
58     unset GREP_OPTIONS EXC_FOLDERS
59     unfunction grep-flags-available
60 fi
61-unset _grep_alias_cache
62+
63+source "$__GREP_CACHE_FILE"
64+unset __GREP_CACHE_FILE __GREP_ALIAS_CACHES