aboutsummaryrefslogtreecommitdiff
path: root/sh
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-18 10:21:43 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-18 10:21:43 +1200
commitb84fd8bdafcbc56dfe480b6b7946a8911cc5ead6 (patch)
tree74d9dcce65c585441b12be755c27929949de3e59 /sh
parentReplace Bashism "hash" with POSIX sh "command" (diff)
downloaddotfiles-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')
-rw-r--r--sh/profile.d/grep.sh9
-rw-r--r--sh/shrc.d/grep.sh35
-rw-r--r--sh/shrc.d/ls.sh15
3 files changed, 51 insertions, 8 deletions
diff --git a/sh/profile.d/grep.sh b/sh/profile.d/grep.sh
index 8c1c5836..892351ca 100644
--- a/sh/profile.d/grep.sh
+++ b/sh/profile.d/grep.sh
@@ -18,11 +18,4 @@
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
+)
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 "$@"
+}
diff --git a/sh/shrc.d/ls.sh b/sh/shrc.d/ls.sh
new file mode 100644
index 00000000..eec25eb7
--- /dev/null
+++ b/sh/shrc.d/ls.sh
@@ -0,0 +1,15 @@
+# Our ~/.profile should already have made a directory with the supported
+# options for us; if not, we won't be wrapping ls(1) with a function at all
+[ -d "$HOME"/.cache/ls ] || return
+
+# Define function proper
+ls() {
+
+ # Add --color if the terminal has at least 8 colors
+ [ -e "$HOME"/.cache/ls/color ] &&
+ [ "$({ tput colors || tput Co ; } 2>/dev/null)" -ge 8 ] &&
+ set -- --color=auto "$@"
+
+ # Run ls(1) with the concluded arguments
+ command ls "$@"
+}