• # lesspipe

    Posté par . En réponse au journal Back to basics : avoir un excellent pager avec less. Évalué à 10.

    Tout d'abord, je signale que ~/.lessfilter ça ne fonctionne que sous debian.

    lesspipe regarde le contenu de la variable LESSOPEN. Logiquement, on créé un script lesspipe qui renvoie ses variables lorsqu'on l'exécute sans argument :

    $ lesspipe
    export LESSOPEN="| /usr/bin/lesspipe %s";
    export LESSCLOSE="/usr/bin/lesspipe %s %s";
    

    D'où le eval $(lesspipe) pour l'activer. Il se trouve que le lesspipe fourni par debian cherche un fichier ~/.lessfilter, ce qui permet à chaque utilisateur d'avoir son script perso. Mais ce n'est pas le cas sous toutes les distribs.

    Pour ma part, j'ai refait un lesspipe à ma sauce, d'abord inspiré de ceux trouvés dans les distribs que j'ai utilisés, puis alimenté au fur et à mesure des fichiers que j'ai voulu lire avec less. Le voici aujourd'hui :

    #!/bin/sh
    # input preprocessor for less
    # print LESSOPEN variable, when called without args
    # (use it with absolute path)
    if [ $# -eq 0 ]; then
     echo "LESSOPEN=\"|0ドル %s\""
     echo "LESSCHARSET=utf-8"
     if env | grep -q '^LESS=' ; then
     if ! echo $LESS | grep -q '\-R' ; then
     echo "LESS=\"-R $LESS\""
     fi
     else
     echo "LESS=-R"
     fi
     echo "export LESS LESSOPEN LESSCHARSET"
     exit
    fi
    # Check usage
    if [ $# -gt 1 ]; then
     echo "Usage: Don't use it directly"
     echo 'Launch "eval $(/path/to/lesspipe.sh)" to use it with less'
    fi
    # We just determine file type of 1ドル
    # and determine which viewer is appropriate
    case "$(file -Lb "1ドル")" in
    # Folder
    directory)
     ls -la "1ドル" 2>/dev/null ;;
    # Archive
    POSIX[[:space:]]tar[[:space:]]archive*)
     tar tvvf "1ドル" 2>/dev/null ;;
    gzip[[:space:]]compressed[[:space:]]data*) # Test if tar or man 
     if gzip -dc "1ドル" | file - | grep -q 'tar archive' ; then
     tar tvvf "1ドル" 2>/dev/null
     elif gzip -dc "1ドル" | file - | grep -q 'roff' ; then
     gzip -dc "1ドル" | nroff -S -mandoc -
     else
     gzip -dc "1ドル" 2>/dev/null
     fi ;;
    bzip2[[:space:]]compressed[[:space:]]data*)
     if bzip2 -dc "1ドル" | file - | grep -q 'tar archive' ; then
     tar tvvf "1ドル" 2>/dev/null
     elif bzip2 -dc "1ドル" | file - | grep -q 'roff' ; then
     bzip2 -dc "1ドル" | nroff -S -mandoc -
     else
     bzip2 -dc "1ドル" 2>/dev/null
     fi ;;
    cpio[[:space:]]archive)
     which cpio >/dev/null && cpio -it < "1ドル" 2>/dev/null ;;
    [Xx][Zz][[:space:]]compressed[[:space:]]data*)
     if xz -dc "1ドル" | file - | grep 'tar archive' ; then
     xz -dc "1ドル" | tar tvvf - 2>/dev/null
     else
     xz -dc "1ドル" 2>/dev/null
     fi ;;
    data) # Test if lzma archive
     if xz -t "1ドル" ; then
     if xz -F lzma -dc "1ドル" | file - | grep 'tar archive' ; then
     xz -F lzma -dc "1ドル" | tar tvvf - 2>/dev/null
     else
     xz -F lzma -dc "1ドル" 2>/dev/null
     fi
     else
     echo "Unrecognized file"
     fi ;;
    ISO[[:space:]]9660[[:space:]]CD-ROM[[:space:]]filesystem[[:space:]]data*)
     if which isoinfo >/dev/null ; then
     echo "1ドル:" ; isoinfo -d -i "1ドル"
     echo
     echo '***Contents:' ; isoinfo -f -i "1ドル"
     fi ;;
    Zip[[:space:]]archive[[:space:]]data*)
     unzip -l "1ドル" 2>/dev/null ;;
    RAR[[:space:]]archive[[:space:]]data*)
     which unrar >/dev/null && unrar l "1ドル" 2>/dev/null ;;
    # Distributions Packages
    Debian[[:space:]]binary[[:space:]]package*)
     if which dpkg >/dev/null ; then
     echo "1ドル:" ; dpkg --info "1ドル"
     echo
     echo '*** Contents:' ; dpkg-deb --contents "1ドル"
     else
     ar p "1ドル" data.tar.gz | tar tzvf -
     fi ;;
    RPM*)
     if which rpm >/dev/null ; then
     echo "1ドル:" ; rpm -q -i -p "1ドル"
     echo
     echo '*** Contents:' ; rpm -q -l -p "1ドル"
     elif which rpm2cpio >/dev/null ; then
     rpm2cpio "1ドル" | cpio -it
     elif which rpm2tar >/dev/null ; then
     rpm2tar -O "1ドル" | tar tvf -
     fi ;;
    # Printable documents
    PDF[[:space:]]document*)
     which pdftotext >/dev/null && pdftotext "1ドル" - 2>/dev/null ;;
    PostScript[[:space:]]document*)
     if which pdftotext >/dev/null && which ps2pdf >/dev/null ; then
     ps2pdf "1ドル" > /tmp/less-$$ 2>/dev/null
     pdftotext /tmp/less-$$ - 2>/dev/null
     rm -f /tmp/less-$$
     fi ;;
    TeX[[:space:]]DVI[[:space:]]file*)
     which dvi2tty >/dev/null && dvi2tty "1ドル" >/dev/null ;;
    # Editable documents
    LaTeX*document[[:space:]]text)
     which highlight >/dev/null && highlight -S tex -O ansi "1ドル" 2>/dev/null ;;
    HTML[[:space:]]document[[:space:]]text)
     which highlight >/dev/null && highlight -S html -O ansi "1ドル" 2>/dev/null ;;
    OpenDocument[[:space:]]Text)
     which odt2txt >/dev/null && odt2txt "1ドル" 2>/dev/null ;;
    CDF[[:space:]]V2[[:space:]]Document*) # MS Office document
     if echo "1ドル" | grep -q '\.doc$' ; then
     which catdoc >/dev/null && catdoc "1ドル" 2>/dev/null
     elif echo "1ドル" | grep -q '\.xls$' ; then
     which xls2csv >/dev/null && xls2csv "1ドル" 2>/dev/null
     fi ;;
    troff*)
     nroff -S -mandoc "1ドル" 2>/dev/null ;;
    # text
    *[Pp]erl*script*text*)
     if which pygmentize >/dev/null ; then
     pygmentize -l pl "1ドル" 2>/dev/null
     elif which highlight >/dev/null ; then
     highlight -S perl -O ansi "1ドル" 2>/dev/null
     fi ;;
    *[Pp]ython*script*text*)
     if which pygmentize >/dev/null ; then
     pygmentize -l py "1ドル" 2>/dev/null
     elif which highlight >/dev/null ; then
     highlight -S python -O ansi "1ドル" 2>/dev/null
     fi ;;
    *sh*script*text*)
     if which pygmentize >/dev/null ; then
     pygmentize -l sh "1ドル" 2>/dev/null
     elif which highlight >/dev/null ; then
     highlight -S Bash -O ansi "1ドル" 2>/dev/null
     fi ;;
    *[Pp]ython*script*text*)
     if which pygmentize >/dev/null ; then
     pygmentize -l py "1ドル" 2>/dev/null
     elif which highlight >/dev/null ; then
     highlight -S Bash -O ansi "1ドル" 2>/dev/null
     fi ;;
    *[pP][hH][pP]*script*text*)
     if which pygmentize >/dev/null ; then
     pygmentize -l php "1ドル" 2>/dev/null
     elif which highlight >/dev/null ; then
     highlight -S php -O ansi "1ドル" 2>/dev/null
     fi ;;
    *C*program*text)
     if which pygmentize >/dev/null ; then
     pygmentize -l c "1ドル" 2>/dev/null
     elif which highlight >/dev/null ; then
     highlight -S c -O ansi "1ドル" 2>/dev/null
     fi ;;
    *text)
     if echo "1ドル" | grep -q 'Makefile' ; then
     if which pygmentize >/dev/null ; then
     pygmentize -l make "1ドル" 2>/dev/null
     elif which highlight >/dev/null ; then
     highlight -S make -O ansi "1ドル" 2>/dev/null
     fi
     else
     cat "1ドル" 2>/dev/null
     fi ;;
    # Images
    JPEG[[:space]]image[[:space:]]data*)
     which identify >/dev/null && identify "1ドル" 2>/dev/null
     which jp2a >/dev/null && jp2a --color "1ドル" 2>/dev/null ;;
    *image[[:space:]]data*)
     which identify >/dev/null && identify "1ドル" 2>/dev/null
     which jp2a >/dev/null && convert "1ドル" jpg:- 2>/dev/null | jp2a --color - 2>/dev/null ;;
    # Others
    # *)
    # cat "1ドル" ;;
    esac