diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-18 10:21:43 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-18 10:21:43 +1200 |
commit | b84fd8bdafcbc56dfe480b6b7946a8911cc5ead6 (patch) | |
tree | 74d9dcce65c585441b12be755c27929949de3e59 /sh/shrc.d/grep.sh | |
parent | Replace Bashism "hash" with POSIX sh "command" (diff) | |
download | dotfiles-b84fd8bdafcbc56dfe480b6b7946a8911cc5ead6.tar.gz dotfiles-b84fd8bdafcbc56dfe480b6b7946a8911cc5ead6.zip |
Port grep() and ls() to POSIX sh
Check capabilities of wrapped programs at runtime, not declaration time.
Also do away with the silly GREP_COLORS and GREP_OPTS variables.
Considering doing the same with LS_COLORS.
Diffstat (limited to 'sh/shrc.d/grep.sh')
-rw-r--r-- | sh/shrc.d/grep.sh | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/sh/shrc.d/grep.sh b/sh/shrc.d/grep.sh new file mode 100644 index 00000000..df2101aa --- /dev/null +++ b/sh/shrc.d/grep.sh @@ -0,0 +1,35 @@ +# Our ~/.profile should already have made a directory with the supported +# options for us; if not, we won't be wrapping grep(1) with a function at all +[ -d "$HOME"/.cache/grep ] || return + +# Define function proper +grep() { + + # Add --color if the terminal has at least 8 colors + [ -e "$HOME"/.cache/grep/color ] && + [ "$({ tput colors || tput Co ; } 2>/dev/null)" -ge 8 ] && + set -- --color=auto "$@" + + # Add --binary-files=without-match to gracefully skip binary files + [ -e "$HOME"/.cache/grep/binary-files ] && + set -- --binary-files=without-match "$@" + + # Add --exclude to ignore .gitignore and .gitmodules files + [ -e "$HOME"/.cache/grep/exclude ] && + set -- \ + --exclude=.gitignore \ + --exclude=.gitmodules \ + "$@" + + # Add --exclude-dir to ignore version control dot-directories + [ -e "$HOME"/.cache/grep/exclude-dir ] && + set -- \ + --exclude-dir=.cvs \ + --exclude-dir=.git \ + --exclude-dir=.hg \ + --exclude-dir=.svn \ + "$@" + + # Run grep(1) with the concluded arguments + command grep "$@" +} |