aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-24 11:17:52 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-24 11:17:52 +1200
commitd0c5dfe1570c0b2b6048abe0e45d690a93ef99f6 (patch)
treed3b3b378f32ad76b4656e88b7f1aa46bc915eaa1
parentImprove a comment (diff)
downloaddotfiles-d0c5dfe1570c0b2b6048abe0e45d690a93ef99f6.tar.gz
dotfiles-d0c5dfe1570c0b2b6048abe0e45d690a93ef99f6.zip
Add gress()
-rw-r--r--README.markdown2
-rw-r--r--sh/shrc.d/gress.sh22
2 files changed, 24 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown
index d2ce0efa..cd3305fd 100644
--- a/README.markdown
+++ b/README.markdown
@@ -403,6 +403,8 @@ Installed by the `install-bin` target:
and does up to three silent retries using `try(1)`.
* `grc(1)` quietly tests whether the given directory appears to be a Git
repository with pending changes.
+* `gress(1)` runs the output of `grep(1)` through your pager, using color if
+ it can
* `gscr(1)` scrubs Git repositories.
* `han(1)` provides a `keywordprg` for Vim's Bash script filetype that will
look for `help` topics. You could use it from the shell too.
diff --git a/sh/shrc.d/gress.sh b/sh/shrc.d/gress.sh
new file mode 100644
index 00000000..483198b3
--- /dev/null
+++ b/sh/shrc.d/gress.sh
@@ -0,0 +1,22 @@
+# Pipe grep(1) output through PAGER; mostly to preserve grep colouring
+# interactively through less(1), but it'll work fine with plain grep(1) and
+# more(1)
+gress() {
+
+ # Add --color=always if the terminal has at least 8 colors; we have to add
+ # "always" to coax grep(1) into emitting colors even though it can tell its
+ # stdout isn't a terminal but a pager
+ [ -e "$HOME"/.cache/grep/color ] &&
+ [ "$({ tput colors || tput Co ; } 2>/dev/null)" -ge 8 ] &&
+ set -- --color=always "$@"
+
+ # Run grep; it might be our function wrapper; that's OK
+ grep "$@" |
+
+ # Run the appropriate pager; if it's less(1), we can tack on -R (though my
+ # ~/.lesskey does this anyway)
+ case $PAGER in
+ less) "$PAGER" -R ;;
+ *) "$PAGER" ;;
+ esac
+}