aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2015-05-30 23:56:54 +1200
committerTom Ryder <tom@sanctum.geek.nz>2015-05-30 23:56:54 +1200
commitd20cb23998c672fea3ef344d836102a781517c80 (patch)
treebbec18590b578f7eee131203bf689e0704602df9
parentRemove a few notes about ~/.profile (diff)
downloaddotfiles-d20cb23998c672fea3ef344d836102a781517c80.tar.gz
dotfiles-d20cb23998c672fea3ef344d836102a781517c80.zip
Overhaul grep(1) and ls(1) options and wrappers
-rw-r--r--bash/bashrc.d/grep.bash51
-rw-r--r--bash/bashrc.d/ls.bash37
-rw-r--r--sh/profile.d/grep.sh38
-rw-r--r--sh/profile.d/ls.sh26
4 files changed, 80 insertions, 72 deletions
diff --git a/bash/bashrc.d/grep.bash b/bash/bashrc.d/grep.bash
index 4560c4ac..559b5e09 100644
--- a/bash/bashrc.d/grep.bash
+++ b/bash/bashrc.d/grep.bash
@@ -1,40 +1,13 @@
-# Return appropriate options for grep
-grepopts() {
-
- # Start with ignoring binary files
- local -a grepopts=(-I)
-
- # Snarf the output of `grep --help` into a variable
- local grephelp=$(grep --help 2>/dev/null)
-
- # If the --exclude option is available, exclude some VCS files
- if [[ $grephelp == *--exclude* ]] ; then
- for exclude_file in .gitignore .gitmodules ; do
- grepopts=("${grepopts[@]}" --exclude="$exclude_file")
- done
- fi
-
- # If the --exclude-dir option is available, exclude some VCS dirs
- if [[ $grephelp == *--exclude-dir* ]] ; then
- for exclude_dir in .cvs .git .hg .svn ; do
- grepopts=("${grepopts[@]}" --exclude-dir="$exclude_dir")
- done
- fi
-
- # If the --color option is available and we have a terminal that supports
- # at least eight colors, add --color=auto to the options
- local colors=$(tput colors 2>/dev/null)
- if [[ $grephelp == *--color* ]] && ((colors >= 8)) ; then
- grepopts=("${grepopts[@]}" --color=auto)
- fi
-
- # Print the options as a single string, space-delimited
- printf %s "${grepopts[*]}"
-}
-
-# Alias grep with those options
-alias grep="grep $(grepopts)"
-
-# Unset helper function
-unset -f grepopts
+# Define function wrapper for grep(1) with --color option if GREP_COLORS is
+# set; checks that color is available in the terminal within the function
+if [[ $GREP_COLORS ]] ; then
+ grep() {
+ local -i colors=$(tput colors 2>/dev/null)
+ if ((colors >= 8)) ; then
+ command grep --color "$@"
+ else
+ command grep "$@"
+ fi
+ }
+fi
diff --git a/bash/bashrc.d/ls.bash b/bash/bashrc.d/ls.bash
index a559a44a..322644c5 100644
--- a/bash/bashrc.d/ls.bash
+++ b/bash/bashrc.d/ls.bash
@@ -1,26 +1,13 @@
-# Return appropriate options for ls
-lsopts() {
-
- # Snarf the output of `ls --help` into a variable
- local lshelp=$(ls --help 2>/dev/null)
-
- # Start collecting available options
- local -a lsopts
-
- # If the --color option is available and we have a terminal that supports
- # at least eight colors, add --color=auto to the options
- local colors=$(tput colors 2>/dev/null)
- if [[ $lshelp == *--color* ]] && ((colors >= 8)) ; then
- lsopts=("${lsopts[@]}" --color=auto)
- fi
-
- # Print the options as a single string, space-delimited
- printf %s "${lsopts[*]}"
-}
-
-# Alias ls with these options
-alias ls="ls $(lsopts)"
-
-# Unset helper function
-unset -f lsopts
+# Define function wrapper for ls(1) with --color option if LS_COLORS is set;
+# checks that color is available in the terminal within the function
+if [[ $LS_COLORS ]] ; then
+ ls() {
+ local colors=$(tput colors 2>/dev/null)
+ if ((colors >= 8)) ; then
+ command ls --color=auto "$@"
+ else
+ command ls "$@"
+ fi
+ }
+fi
diff --git a/sh/profile.d/grep.sh b/sh/profile.d/grep.sh
new file mode 100644
index 00000000..e83d6268
--- /dev/null
+++ b/sh/profile.d/grep.sh
@@ -0,0 +1,38 @@
+# Store grep(1)'s --help output in a variable
+grep_help=$(grep --help 2>/dev/null)
+
+# Define and store appropriate colors for grep(1) if applicable
+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
+
+# Use GREP_OPTIONS to add some useful --exclude and --exclude-dir options to
+# grep(1) calls if applicable
+case $grep_help in
+ *--exclude*)
+ for exclude in .gitignore .gitmodules ; do
+ GREP_OPTIONS=${GREP_OPTIONS:+$GREP_OPTIONS }'--exclude='$exclude
+ done
+ unset -v exclude
+ ;;
+esac
+case $grep_help in
+ *--exclude-dir*)
+ for exclude_dir in .cvs .git .hg .svn ; do
+ GREP_OPTIONS=${GREP_OPTIONS:+$GREP_OPTIONS }'--exclude-dir='$exclude_dir
+ done
+ unset -v exclude_dir
+ ;;
+esac
+
+# We're done parsing grep(1)'s --help output now
+unset -v grep_help
+
+# Export the grep(1) options if we decided on any
+if [ -n "$GREP_OPTIONS" ] ; then
+ export GREP_OPTIONS
+fi
+
diff --git a/sh/profile.d/ls.sh b/sh/profile.d/ls.sh
index 25fe4777..3ecaa5fd 100644
--- a/sh/profile.d/ls.sh
+++ b/sh/profile.d/ls.sh
@@ -1,9 +1,19 @@
-# Define and store appropriate colors for ls
-if command -v dircolors >/dev/null 2>&1 ; then
- if [ -r "$HOME"/.dircolors ] ; then
- eval "$(dircolors --sh -- "$HOME"/.dircolors)"
- else
- eval "$(dircolors --sh)"
- fi
-fi
+# Store ls(1)'s --help output in a variable
+lshelp=$(ls --help 2>/dev/null)
+
+# Define and store appropriate colors for ls(1) if applicable
+case $lshelp in
+ *--color*)
+ if command -v dircolors >/dev/null 2>&1 ; then
+ if [ -r "$HOME"/.dircolors ] ; then
+ eval "$(dircolors --sh -- "$HOME"/.dircolors)"
+ else
+ eval "$(dircolors --sh)"
+ fi
+ fi
+ ;;
+esac
+
+# We're done parsing ls(1)'s --help output now
+unset -v lshelp