Last active
February 1, 2020 16:33
-
-
Save bagage/c010bda7201a1a8c0ff309703ad8e9eb to your computer and use it in GitHub Desktop.
.lessfilter for various input files (code highlighting, image and binary files display, …).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -x | |
# input preprocessor for less | |
function cmdexist() { | |
which "$1" >/dev/null | |
} | |
function colorize() { | |
if which pygmentize >/dev/null ; then | |
pygmentize -g "$1" | |
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 | |
# Image | |
*image[[:space:]]data*) | |
width=$(stty size | cut -d ' ' -f 2) | |
if cmdexist termpix; then | |
# https://github.com/hopey-dishwasher/termpix | |
termpix "$1" --width $width --true-color | |
elif cmdexist terminal-image-viewer; then | |
# Wrapper around this https://github.com/stefanhaustein/TerminalImageViewer.git | |
terminal-image-viewer -w $width "$1" | |
elif cmdexist img2txt; then | |
# libcaca (caca-utils package) | |
img2txt -W $width -f utf8 "$1" | |
fi | |
;; | |
# 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 ;; | |
# text | |
*text*|HTML*) | |
if [[ "$1" =~ [.]*md$ ]] ; then | |
mdv "$1" | |
else | |
colorize "$1" | |
fi ;; | |
esac | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment