[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [lesspipe]



On 26 Jan 1998, Ben Pfaff wrote:

> lesspipe is in less version 332-2, at least.  Perhaps it is only in
> hamm versions of less.

yes, it is only in hamm....but it will work fine with the bo version
of less. 

i have attached a copy to this message. to install it, save it into
/usr/bin. then edit /etc/profile or ~/.bashrc and add the following line: 

	eval $(/usr/bin/lesspipe)

craig

--
craig sanders
#!/bin/bash
#
# lessfile/lesspipe
#
# Less filter for viewing non text files.
#
# Written by: Behan Webster <behanw@pobox.com>
# Many Modifications by Darren Stalder
#
# combined lessfile and lesspipe to avoid duplication of decode stage
# shell is sure icky.  I'm real tempted to rewrite the whole thing in Perl 
#
# Unfortunately, this means that I have filename dependencies sprinkled
# throughout the code.  If you don't want lessfile to be called that,
# you'll need to change the LESSFILE envar below.
#
# Usage: eval $(lessfile)  or eval $(lesspipe)
#
# less passes in:
#    $1  filename to be viewed with less  (used by LESSOPEN)
# and possibly (if used by lessfile)
#    $2  filename that was created during LESSOPEN

TMPDIR=${TMPDIR:-/tmp}
BASENAME=$(basename $0)
LESSFILE=lessfile

if [ $# -eq 1 ] ; then
    # we were called as LESSOPEN

    # generate filename for possible use by lesspipe
    umask 077
    TMPFILE=$TMPDIR/$LESSFILE.$$

      # possibly redirect stdout to a file for lessfile
    ( if [ $BASENAME = $LESSFILE ]; then exec > $TMPFILE; fi

      # Decode file for less
      case "$1" in
	*.arj|*.ARJ)
	    if [ -x /usr/bin/unarj ]; then unarj -v $1; else echo "No unarj available"; fi ;;

        *.deb)
	    echo "$1:"; dpkg --info $1; /bin/echo -e '\n*** Contents:'; dpkg-deb --contents $1 ;;

	*.gif|*.jpeg|*.jpg|*.pcd|*.png|*.tga|*.tiff|*.tif)
	    if [ -x /usr/X11R6/bin/identify ]; then /usr/X11R6/bin/identify $1;
	    else /bin/echo -e "No identify available\nInstall ImageMagick to browse images"; fi ;;

        *.tar.gz|*.tgz|*.tar.Z)
	    tar tzvf $1 ;;

	*.gz|*.Z|*.z)
	    gzip -dc $1 ;;

        *.tar)
	    tar tvf $1 ;;

        *.zip|*.ZIP)
	    if [ -x /usr/bin/unzip ]; then unzip -v $1; else echo "No unzip available"; fi ;;

    esac ) 2>/dev/null

    if [ $BASENAME = $LESSFILE ]; then
	if [ -s $TMPFILE ]; then echo $TMPFILE; fi
    fi

elif [ $# -eq 2 ] ; then
    #
    # we were called as LESSCLOSE
    # delete the file created if we were lessfile
    #
    if [ $BASENAME = $LESSFILE ]; then
	if [ -f $2 -a -O $2 ]; then rm -f $2; else echo "Error in deleting $2" > /dev/tty; fi
    fi

elif [ $# -eq 0 ] ; then
    #
    # must setup shell to use LESSOPEN/LESSCLOSE
    #
    # I have no idea how some of the more esoteric shells (es, rc) do things.
    # If they don't do things in a Bourne manner, send me a patch and I'll incorporate it.
    #

    # first determine the full path of lessfile/lesspipe
    # if you can determine a better way to do this, send me a patch, I've not shell-scripted for many a year
    FULLPATH=$(FOO=$(command -v $0); cd $(dirname $FOO);pwd)/$BASENAME
    

    case "$SHELL" in
        *csh)
	    if [ $BASENAME = $LESSFILE ]; then
		echo "setenv LESSOPEN \"$FULLPATH '%s'\";"
		echo "setenv LESSCLOSE \"$FULLPATH '%s' '%s'\";"
	    else
		echo "setenv LESSOPEN \"| $FULLPATH '%s'\";"
		echo "setenv LESSCLOSE \"$FULLPATH '%s' '%s'\";"
	    fi
            ;;
        *)
	    if [ $(basename $0) = $LESSFILE ]; then
		echo "export LESSOPEN=\"$FULLPATH '%s'\";"
		echo "export LESSCLOSE=\"$FULLPATH '%s' '%s'\";"
	    else
		echo "export LESSOPEN=\"| $FULLPATH '%s'\";"
		echo "export LESSCLOSE=\"$FULLPATH '%s' '%s'\";"
	    fi
            ;;
    esac

else
    echo "Usage: eval \$($BASENAME)"
    exit
fi


Reply to: