57b178102c0016f5c433054ce981a80dd4b4b73f

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

Message

Performance enhancement for lib/grep

- Use $ZSH_CACHE_DIR to store the grep alias with all the right features
- Expire the cache after 24 hours
- See issue #8444

Diff

 1diff --git a/lib/grep.zsh b/lib/grep.zsh
 2index 09042e13b13b300c00e3fd0942a7c71af5c9d8c8..df9146aa4b355e703020b183c2e4fed7f19bbbb8 100644
 3--- a/lib/grep.zsh
 4+++ b/lib/grep.zsh
 5@@ -1,29 +1,40 @@
 6-# is x grep argument available?
 7-grep-flag-available() {
 8-    echo | grep $1 "" >/dev/null 2>&1
 9-}
10+# see if we already cached the grep alias in past day
11+_grep_alias_cache=("$ZSH_CACHE_DIR"/grep_alias.zsh(Nm-24))
12+if (( $#_grep_alias_cache )); then
13+    source "$ZSH_CACHE_DIR"/grep_alias.zsh
14+else
15+    # is x grep argument available?
16+    grep-flag-available() {
17+        echo | grep $1 "" >/dev/null 2>&1
18+    }
19 
20-GREP_OPTIONS=""
21+    GREP_OPTIONS=""
22 
23-# color grep results
24-if grep-flag-available --color=auto; then
25-    GREP_OPTIONS+=" --color=auto"
26-fi
27+    # color grep results
28+    if grep-flag-available --color=auto; then
29+        GREP_OPTIONS+=" --color=auto"
30+    fi
31 
32-# ignore these folders (if the necessary grep flags are available)
33-EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}"
34+    # ignore these folders (if the necessary grep flags are available)
35+    EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}"
36 
37-if grep-flag-available --exclude-dir=.cvs; then
38-    GREP_OPTIONS+=" --exclude-dir=$EXC_FOLDERS"
39-elif grep-flag-available --exclude=.cvs; then
40-    GREP_OPTIONS+=" --exclude=$EXC_FOLDERS"
41-fi
42+    if grep-flag-available --exclude-dir=.cvs; then
43+        GREP_OPTIONS+=" --exclude-dir=$EXC_FOLDERS"
44+    elif grep-flag-available --exclude=.cvs; then
45+        GREP_OPTIONS+=" --exclude=$EXC_FOLDERS"
46+    fi
47 
48-# export grep, egrep and fgrep settings
49-alias grep="grep $GREP_OPTIONS"
50-alias egrep="egrep $GREP_OPTIONS"
51-alias fgrep="fgrep $GREP_OPTIONS"
52+    {
53+        # export grep, egrep and fgrep settings
54+        echo alias grep="'grep $GREP_OPTIONS'"
55+        echo alias egrep="'egrep $GREP_OPTIONS'"
56+        echo alias fgrep="'fgrep $GREP_OPTIONS'"
57+    } > "$ZSH_CACHE_DIR/grep_alias.zsh"
58 
59-# clean up
60-unset GREP_OPTIONS EXC_FOLDERS
61-unfunction grep-flag-available
62+    source "$ZSH_CACHE_DIR/grep_alias.zsh"
63+
64+    # clean up
65+    unset GREP_OPTIONS EXC_FOLDERS
66+    unfunction grep-flag-available
67+fi
68+unset _grep_alias_cache