grep.zsh 1.15 KB
Newer Older
mattmc3's avatar
mattmc3 committed
1
2
3
4
5
6
7
8
9
# see if we already cached the grep alias in past day
_grep_alias_cache=("$ZSH_CACHE_DIR"/grep_alias.zsh(Nm-24))
if (( $#_grep_alias_cache )); then
    source "$ZSH_CACHE_DIR"/grep_alias.zsh
else
    # is x grep argument available?
    grep-flag-available() {
        echo | grep $1 "" >/dev/null 2>&1
    }
nXqd's avatar
nXqd committed
10

mattmc3's avatar
mattmc3 committed
11
    GREP_OPTIONS=""
12

mattmc3's avatar
mattmc3 committed
13
14
15
16
    # color grep results
    if grep-flag-available --color=auto; then
        GREP_OPTIONS+=" --color=auto"
    fi
17

mattmc3's avatar
mattmc3 committed
18
19
    # ignore these folders (if the necessary grep flags are available)
    EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}"
20

mattmc3's avatar
mattmc3 committed
21
22
23
24
25
    if grep-flag-available --exclude-dir=.cvs; then
        GREP_OPTIONS+=" --exclude-dir=$EXC_FOLDERS"
    elif grep-flag-available --exclude=.cvs; then
        GREP_OPTIONS+=" --exclude=$EXC_FOLDERS"
    fi
26

mattmc3's avatar
mattmc3 committed
27
28
29
30
31
32
    {
        # export grep, egrep and fgrep settings
        echo alias grep="'grep $GREP_OPTIONS'"
        echo alias egrep="'egrep $GREP_OPTIONS'"
        echo alias fgrep="'fgrep $GREP_OPTIONS'"
    } > "$ZSH_CACHE_DIR/grep_alias.zsh"
Marc Cornellà's avatar
Marc Cornellà committed
33

mattmc3's avatar
mattmc3 committed
34
35
36
37
38
39
40
    source "$ZSH_CACHE_DIR/grep_alias.zsh"

    # clean up
    unset GREP_OPTIONS EXC_FOLDERS
    unfunction grep-flag-available
fi
unset _grep_alias_cache