aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-15 21:38:15 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-15 21:47:11 +1200
commit0664bcb5c8ed61d33ee6bdbc58af3443192cc607 (patch)
treeccafde667d0ccd885fb46a0a68d1dd9ceec209c0
parentCorrect order of tasks in xgo(1) (diff)
downloaddotfiles-0664bcb5c8ed61d33ee6bdbc58af3443192cc607.tar.gz
dotfiles-0664bcb5c8ed61d33ee6bdbc58af3443192cc607.zip
Keep grep(1) option metadata in cache
It's stupid to run `grep --help` once per shell (twice for login shells!) when it's so unlikely to change, and way faster to check for the presence or absence of hint files rather than pattern-match the output with the shell. ls(1) will get the same treatment in a minute.
-rw-r--r--bash/bashrc.d/grep.bash20
-rw-r--r--sh/profile.d/grep.sh37
2 files changed, 37 insertions, 20 deletions
diff --git a/bash/bashrc.d/grep.bash b/bash/bashrc.d/grep.bash
index 11eeb5b0..624baf30 100644
--- a/bash/bashrc.d/grep.bash
+++ b/bash/bashrc.d/grep.bash
@@ -1,31 +1,33 @@
+# Our ~/.profile should already have made a directory with the supported
+# options for us
+[[ -d $HOME/.cache/grep ]] || return
+
# 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 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=()
+if ((colors >= 8)) && [[ -n $GREP_COLORS ]] ; then
GREPOPTS[${#GREPOPTS[@]}]='--color=auto'
fi
-if [[ $grep_help == *--binary-files* ]] ; then
+if [[ -e $HOME/.cache/grep/binary-files ]] ; then
GREPOPTS[${#GREPOPTS[@]}]='--binary-files=without-match'
fi
-if [[ $grep_help == *--exclude* ]] ; then
+if [[ -e $HOME/.cache/grep/exclude ]] ; then
GREPOPTS[${#GREPOPTS[@]}]='--exclude={.gitignore,.gitmodules}'
fi
-if [[ $grep_help == *--exclude-dir* ]] ; then
+if [[ -e $HOME/.cache/grep/exclude-dir ]] ; then
GREPOPTS[${#GREPOPTS[@]}]='--exclude-dir={.cvs,.git,.hg,.svn}'
fi
-# Done, unset helper vars
-unset -v grep_help colors
+# Done, unset color var
+unset -v colors
# Define function proper
grep() {
diff --git a/sh/profile.d/grep.sh b/sh/profile.d/grep.sh
index 87c9a6cc..8c1c5836 100644
--- a/sh/profile.d/grep.sh
+++ b/sh/profile.d/grep.sh
@@ -1,13 +1,28 @@
-# Store grep(1)'s --help output in a variable
-grep_help=$(grep --help 2>/dev/null)
+# Test that we have metadata about what options this system's grep(1) supports,
+# and try to create it if not
+(
+ # Create a directory to hold metadata about grep
+ gcd=$HOME/.cache/grep
+ if ! [ -d "$gcd" ] ; then
+ mkdir -p -- "$gcd" || exit
+ fi
-# Set and export GREP_COLORS if available and appropriate
-case $grep_help in
- *--color*)
- GREP_COLORS='ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'
- export GREP_COLORS
- ;;
-esac
+ # Write grep(1)'s --help output to a file, even if it's empty
+ if ! [ -f "$gcd"/help ] ; then
+ grep --help </dev/null >"$gcd"/help 2>/dev/null || exit
-# We're done parsing grep(1)'s --help output now
-unset -v grep_help
+ # Iterate through some useful options and create files to show they're
+ # available
+ for opt in binary-files color exclude exclude-dir ; do
+ grep -q -- --"$opt" "$gcd"/help || continue
+ touch -- "$gcd"/"$opt" || exit
+ done
+ fi
+) || return
+
+# If one of the available options is --color, set the GREP_COLORS environment
+# variable
+if [ -e "$HOME"/.cache/grep/color ] ; then
+ GREP_COLORS='ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'
+ export GREP_COLORS
+fi