r/ScriptSwap • Bash • Jan 14 '15

[bash] pg() a simple paging function

Unlike simply using less or cat, this function decides between the two based on how many lines of output there will be and takes into account the number of screen lines taken by the PS1 prompt. It uses fmt to wrap long lines without re-flowing paragraphs.

#======================================================================
# Format a file and display it or page it if necessary
#======================================================================
pg () {
declare promptLines=$(echo -e "$PS1" | wc -l)
fmt -s -w $COLUMNS "$1" |
if [[ $(fmt -s -w $COLUMNS "$1" | wc -l) -gt $(( LINES - promptLines )) ]]; then
    less
else
    cat
fi
} 2>/dev/null
4 Upvotes

1 comment sorted by

View all comments

1

u/sbicknel Bash Jan 17 '15

Final version.

#======================================================================
# Format a file (or standard input) and display it or page it if
# necessary
#     pg [-n] [file]
#        -n add line numbers to output
#======================================================================
pg () {

    local    ncols=0				                # columns used for line numbering
    local -r promptLines=$(echo -e "$PS1" | wc -l)  # number of lines taken by PS1 prompt

    # capture standard input
    # huge files can, of course, fill all available memory
    while IFS='' read -r -t 0.1 line; do
	    if ! [[ -v pipeFile ]]; then
		    if [[ "$1" = '-n' ]]; then
			    ncols=8
		    fi
		    local -a pipeFile	# array of lines from piped file
		    local inc		    # amount to increment lineCount each pass through loop
		    local lineCount	    # total number of formatted lines of output
	    fi

	    # add current text line to list of lines, determine how many formatted lines
	    # will result from it, and increment the total number of lines by that many
	    pipeFile=( "${pipeFile[@]}" "$line" )
	    inc=$(fmt -s -w $(( COLUMNS - ncols )) <<< $line | wc -l)
	    lineCount=$(( lineCount + inc ))
    done

    # set up line numbering
    if [[ "$1" = '-n' ]]; then
	    ncols=8
	    local -A num
	    num[cat]='-n'	# parameter for turning on line numbering in cat
	    num[less]='-N'	# parameter for turning on line numbering in less
	    shift
    fi

    # if taking input from a file
    if [[ $# -gt 0 ]]; then	# is there a file names to process?
	    if [[ -f "$1" ]]; then	# and is it a regular file?

		    # format file and feed it to either less or cat
		    fmt -s -w $(( COLUMNS - ncols )) "$1" |
		    if [[ $(fmt -s -w $(( COLUMNS - ncols )) "$1" | wc -l) \
			    -gt $(( LINES - promptLines )) ]]; then
			    command less -R ${num[less]}
		    else
			    command cat ${num[cat]}
		    fi
	    elif [[ -d "$1" ]]; then
		    printf '%s: %s: Is a directory\n' 'pg' "$1" >&2
	    else
		    printf '%s: %s: No such file or directory\n' 'pg' "$1" >&2
	    fi
    elif [[ -v pipeFile ]]; then # taking input from standard input
	    for line in "${pipeFile[@]}"; do
		    echo "$line"
	    done |
	    fmt -s -w $(( COLUMNS - ncols )) |
	    if [[ lineCount -gt $(( LINES - promptLines )) ]]; then
		    command less -R ${num[less]}
	    elif [[ lineCount -gt 0 ]]; then
		    command cat ${num[cat]}
	    else
		    :
	    fi
    fi
}