• [^] # Re: lesspipe

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

    Merci pour les précisions et l'idée de pygmentize :)

    À ta place au lieu d'avoir des 2>/dev/null, je mettrais au début du script (en changeant le shabang pour bash) :

    exec 2<&-
    exec 2<>/dev/null

    Tu as la gestion des scripts python en double :)

    Dernière petite remarque à ta place je ferrais une fonction colorize qui lance pygmentize ou highlight en fonction qui prend en paramètre le lexer pygmentize et le fichier et qui a une table de correspondance entre le lexer pygmentize et l'équivalent highlight. C'est pratique à faire en bash et tu gagne quelques lignes.

    Je me suis permis de le reprendre en bash parce que je pense que je vais l'utiliser si ça ne te dérange pas :)

    #!/bin/bash
    # input preprocessor for less
    function cmdexist() {
     which "1ドル" >/dev/null
    }
    declare -A lexers
    lexers["c"]="c"
    lexers["pl"]="perl"
    lexers["py"]="python"
    lexers["sh"]="Bash"
    lexers["php"]="php"
    lexers["make"]="make"
    function colorize() {
     if which pygmentize >/dev/null ; then
     pygmentize -l "1ドル" "2ドル"
     elif which highlight >/dev/null ; then
     highlight -S "${lexers[1ドル]}" -O ansi "2ドル"
     fi
    }
    # 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ドル" ;;
    # Archive
    POSIX[[:space:]]tar[[:space:]]archive*)
     tar tvvf "1ドル" ;;
    gzip[[:space:]]compressed[[:space:]]data*) # Test if tar or man
     if gzip -dc "1ドル" | file - | grep -q 'tar archive' ; then
     tar tvvf "1ドル"
     elif gzip -dc "1ドル" | file - | grep -q 'roff' ; then
     gzip -dc "1ドル" | nroff -S -mandoc -
     else
     gzip -dc "1ドル"
     fi ;;
    bzip2[[:space:]]compressed[[:space:]]data*)
     if bzip2 -dc "1ドル" | file - | grep -q 'tar archive' ; then
     tar tvvf "1ドル"
     elif bzip2 -dc "1ドル" | file - | grep -q 'roff' ; then
     bzip2 -dc "1ドル" | nroff -S -mandoc -
     else
     bzip2 -dc "1ドル"
     fi ;;
    cpio[[:space:]]archive)
     cmdexist cpio && cpio -it < "1ドル" ;;
    [Xx][Zz][[:space:]]compressed[[:space:]]data*)
     if xz -dc "1ドル" | file - | grep 'tar archive' ; then
     xz -dc "1ドル" | tar tvvf -
     else
     xz -dc "1ドル"
     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 -
     else
     xz -F lzma -dc "1ドル"
     fi
     else
     echo "Unrecognized file"
     fi ;;
    ISO[[:space:]]9660[[:space:]]CD-ROM[[:space:]]filesystem[[:space:]]data*)
     if cmdexist isoinfo ; then
     echo "1ドル:" ; isoinfo -d -i "1ドル"
     echo
     echo '***Contents:' ; isoinfo -f -i "1ドル"
     fi ;;
    Zip[[:space:]]archive[[:space:]]data*)
     unzip -l "1ドル" ;;
    RAR[[:space:]]archive[[:space:]]data*)
     cmdexist unrar && unrar l "1ドル" ;;
    # Distributions Packages
    Debian[[:space:]]binary[[:space:]]package*)
     if cmdexist dpkg ; 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 cmdexist rpm ; then
     echo "1ドル:" ; rpm -q -i -p "1ドル"
     echo
     echo '*** Contents:' ; rpm -q -l -p "1ドル"
     elif cmdexist rpm2cpio ; then
     rpm2cpio "1ドル" | cpio -it
     elif cmdexist rpm2tar ; then
     rpm2tar -O "1ドル" | tar tvf -
     fi ;;
    # Printable documents
    PDF[[:space:]]document*)
     cmdexist pdftotext && pdftotext "1ドル" - ;;
    PostScript[[:space:]]document*)
     if cmdexist pdftotext && cmdexist ps2pdf ; then
     ps2pdf "1ドル" > /tmp/less-$$
     pdftotext /tmp/less-$$ -
     rm -f /tmp/less-$$
     fi ;;
    TeX[[:space:]]DVI[[:space:]]file*)
     cmdexist dvi2tty && dvi2tty "1ドル" >/dev/null ;;
    # Editable documents
    LaTeX*document[[:space:]]text)
     cmdexist highlight && highlight -S tex -O ansi "1ドル" ;;
    HTML[[:space:]]document[[:space:]]text)
     cmdexist highlight && highlight -S html -O ansi "1ドル" ;;
    OpenDocument[[:space:]]Text)
     cmdexist odt2txt && odt2txt "1ドル" ;;
    CDF[[:space:]]V2[[:space:]]Document*) # MS Office document
     if [[ "1ドル" =~ .doc$ ]] ; then
     cmdexist catdoc && catdoc "1ドル"
     elif [[ "1ドル" =~ .xls$ ]] ; then
     cmdexist xls2csv && xls2csv "1ドル"
     fi ;;
    troff*)
     nroff -S -mandoc "1ドル" ;;
    # text
    *[Pp]erl*script*text*) colorize pl "1ドル" ;;
    *[Pp]ython*script*text*) colorize py "1ドル" ;;
    *sh*script*text*) colorize sh "1ドル" ;;
    *[pP][hH][pP]*script*text*) colorize php "1ドル" ;;
    *C*program*text) colorize php "1ドル" ;;
    *text)
     if [[ "1ドル" =~ ^[mM]akefile$ ]] ; then
     colorize make "1ドル"
     else
     cat "1ドル"
     fi ;;
    # Images
    JPEG[[:space]]image[[:space:]]data*)
     cmdexist identify && identify "1ドル"
     cmdexist jp2a && jp2a --color "1ドル" ;;
    *image[[:space:]]data*)
     cmdexist identify && identify "1ドル"
     cmdexist jp2a && convert "1ドル" jpg:- | jp2a --color - ;;
    # Others
    # *)
    # cat "1ドル" ;;
    esac

    J'ai remarqué la commande : dvi2tty "1ドル" >/dev/null il écris sur sa sortie standard ? (je ne connais pas du tout l'outil)

    Tous les contenus que j'écris ici sont sous licence CC0 (j'abandonne autant que possible mes droits d'auteur sur mes écrits)