less/lesspipe.sh

128 lines
3.2 KiB
Bash
Raw Normal View History

#!/bin/sh -
#
# To use this filter with less, define LESSOPEN:
# export LESSOPEN="|/usr/bin/lesspipe.sh %s"
# Helper function to list contents of ISO files (CD images)
iso_list() {
isoinfo -d -i "$1"
isoinfo -d -i "$1" | grep -q ^Rock\.Ridge && iiopts="$iiopts -R"
isoinfo -d -i "$1" | grep -q ^Joliet && iiopts="$iiopts -J"
echo
isoinfo -f $iiopts -i "$1"
}
lesspipe() {
case `echo "$1" | tr '[:upper:]' '[:lower:]'` in
*.[1-9n]|*.man|*.[1-9n].bz2|*.man.bz2|*.[1-9].gz|*.[1-9]x.gz|*.man.gz|*.[1-9].xz|*.[1-9]x.xz|*.man.xz)
case `echo "$1" | tr '[:upper:]' '[:lower:]'` in
*.gz) DECOMPRESSOR="gunzip -c" ;;
*.bz2) DECOMPRESSOR="bunzip2 -c" ;;
*.xz) DECOMPRESSOR="unxz -c" ;;
*) DECOMPRESSOR="cat" ;;
esac
if $DECOMPRESSOR -- "$1" | file - | grep -q troff; then
if echo "$1" | grep -q ^/; then #absolute path
man -- "$1" | cat -s
else
man -- "./$1" | cat -s
fi
else
$DECOMPRESSOR -- "$1"
fi
;;
*.tar.gz|*.tgz|*.tar.z|*.tar.dz)
tar tzvf "$1" ;;
*.tar.xz)
tar Jtvvf "$1" ;;
*.xz)
if [ -x "`which xz`" ]; then xz -dc -- "$1"
else echo "No xz available"; fi ;;
*.tar.bz2|*.tbz2)
if [ -x "`which bunzip2`" ]; then bunzip2 -dc "$1" | tar tvvf -
else echo "No bunzip2 available"; fi ;;
*.bin|*.raw)
if [ -x "`which isoinfo`" ]; then
file "$1" | grep -q ISO\.9660 && iso_list "$1"
else
echo "No isoinfo available"
echo "Install mkisofs to view ISO images"
fi ;;
*.bz2)
if [ -x "`which bunzip2`" ]; then bunzip2 -dc "$1"
else echo "No bunzip2 available"; fi ;;
*.cpi|*.cpio)
cpio -itv < "$1" ;;
*.gz|*.z|*.dz)
gzip -dc "$1" ;;
*.gif|*.jpeg|*.jpg|*.pcd|*.png|*.tga|*.tiff|*.tif)
if [ -x "`which identify`" ]; then
identify "$1"
else
echo "No identify available"
echo "Install ImageMagick to browse images"
fi ;;
*.iso)
if [ -x "`which isoinfo`" ]; then iso_list "$1"
else
echo "No isoinfo available"
echo "Install cdrtools to view ISO images"
fi ;;
*.lha|*.lzh)
if [ -x "`which lha`" ]; then lha v "$1"
else echo "No lha available"; fi ;;
*.pdf)
if [ -x "`which pdftotext`" ]; then pdftotext "$1" -
else echo "No pdftotext available"; fi ;;
*.rar|*.r[0-9][0-9])
if [ -x "`which unrar`" ]; then unrar v "$1"
else echo "No rar or unrar available"; fi ;;
*.rpm)
if [ -x "`which rpm`" ]; then
echo "$1:"; rpm -q -i -p "$1"
echo
echo '*** Contents:'
rpm -qlp "$1"
else
echo "rpm isn't available, no query on rpm package possible"
fi ;;
*.tar)
tar tvvf "$1" ;;
*.jar|*.war|*.ear|*.xpi)
if [ -x "`which unzip`" ]; then unzip -v "$1";
else echo "No unzip available"; fi ;;
*.zip)
zipinfo -- "$1" ;;
esac
}
if [ -d "$1" ] ; then
/bin/ls -alF -- "$1"
else
# Allow for user defined filters
if [ -x ~/.lessfilter ]; then
~/.lessfilter "$1"
exit 0
else
lesspipe "$1" 2>/dev/null
fi
fi