Re: [dev] [ubase] pager

From: Eric Pruitt <eric.pruitt_AT_gmail.com>
Date: Thu, 9 Feb 2017 23:57:46 -0800

On Fri, Feb 10, 2017 at 08:26:11AM +0100, robin wrote:
> I usually pipe into less whenever something overflows the terminal
> height, but having to type 2>&1 to see stderr is a bit cumbersome. In
> dvtm Shift-PageUp is much easier.

I use a generic wrapper function in Bash:

    # $1 Name or path of the command to execute.
    # $2 White-space separated list of options to pass to the command
    # when stdout is a TTY. If there are no TTY-dependent options
    # this should be "--".
    # $_AT_ Arguments to pass to command.
    #
    function -paginate()
    {
        local errfd=1

        local command="$1"
        local tty_specific_args="$2"
        shift 2

        if [[ -t 1 ]]; then
            test "$tty_specific_args" != "--" || tty_specific_args=""
            test -t 2 || errfd=2
            "$command" $tty_specific_args "$_AT_" 2>&"$errfd" | less -X -F -R
            return "${PIPESTATUS[0]/141/0}" # Ignore SIGPIPE failures.
        fi

        "$command" "$_AT_"
    }

Then I have around 30 aliases for various commands I use like this:

    alias cat='-paginate cat --'
    alias grep='-paginate grep --color=always'
    alias ps='-paginate ps --cols=$COLUMNS --sort=uid,pid -N --ppid 2 -p 2'

Output is only paginated when stdout is a TTY so I can still use pipes,
and the less flags ensure that less will exit if the output fits on one
screen. I also use tmux, but I find less to be less painful to use than
copy mode in tmux when I don't need to actually copy text.

Eric
Received on Fri Feb 10 2017 - 08:57:46 CET

This archive was generated by hypermail 2.3.0 : Fri Feb 10 2017 - 09:00:15 CET