aboutsummaryrefslogtreecommitdiff
path: root/sh/shrc.d/gress.sh
blob: 483198b333ff6bafbc81a7da742e24faa5b1decf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
}