Last active
December 15, 2015 12:49
-
-
Save tomoe-mami/5262557 to your computer and use it in GitHub Desktop.
showing file thumbnails in terminology
This file contains hidden or 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/sh | |
WIDTH=10 | |
HEIGHT=4 | |
LABEL="yes" # show filename under thumbnail | |
VIDEO="no" # don't show video thumbnail. only show generic icon | |
CHAR="." | |
if [ ! "$TERMINOLOGY" = "1" ]; then | |
echo "This script only works in Terminology" >&2 | |
exit 1 | |
fi | |
while getopts ":w:h:nv" opt; do | |
case "$opt" in | |
w) WIDTH="$OPTARG" ;; | |
h) HEIGHT="$OPTARG" ;; | |
n) LABEL="no" ;; | |
v) VIDEO="yes" ;; | |
:) | |
echo "Option -$OPTARG requires a value" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
if [ $WIDTH -lt 1 ]; then | |
WIDTH=10 | |
fi | |
if [ $HEIGHT -lt 1 ]; then | |
HEIGHT=4 | |
fi | |
if [ -z "$*" ]; then | |
PARAM="./*" | |
else | |
PARAM=$* | |
fi | |
function get_info { | |
local ORIG_STTY="$(stty -g)" | |
stty -echo | |
echo -ne "\e}qs\0" | |
read RESPONSE | |
stty "$ORIG_STTY" | |
TERM_WIDTH="$(echo "$RESPONSE" | cut -d';' -f1)" | |
TERM_HEIGHT="$(echo "$RESPONSE" | cut -d';' -f2)" | |
} | |
function print_block { | |
echo -ne "\e}ib\0" | |
for (( c = 0 ; c < $WIDTH ; c++ )); do | |
echo -n "$CHAR" | |
done | |
echo -ne "\e}ie\0" | |
} | |
# $1 = path | |
function declare_inline_media { | |
echo -ne "\e}it$CHAR$WIDTH;$HEIGHT;$1\0" | |
} | |
# $1 = direction (left/right/up/down) | |
# $2 = amount | |
function move { | |
local OP | |
case "$1" in | |
left) OP="D" ;; | |
right) OP="C" ;; | |
up) OP="A" ;; | |
down) OP="B" ;; | |
esac | |
echo -ne "\e[${2:-1}$OP" | |
} | |
# $1 = absolute path | |
function get_thumb { | |
if [ -d "$1" ]; then | |
THUMB="folder" | |
else | |
EXT="${1##*.}" | |
# there are still more file formats supported by terminology. | |
# these are just few i used most | |
case "$EXT" in | |
jpg|jpeg|png|gif|bmp|ico|xcf|svg|pdf) | |
THUMB="$1";; | |
avi|mp4|mkv|asf|wmv|ogv|mpg|rmvb|flv) | |
if [ "$VIDEO" = "yes" ]; then | |
THUMB="$1" | |
else | |
THUMB="video-x-generic" | |
fi | |
;; | |
mp3|aac|flac|ogg|wma|wav|ape) | |
THUMB="audio-x-generic" ;; | |
zip|rar|ace|gz|bz2|tbz2|tgz|cbz|cbr|7z|xz) | |
THUMB="application-x-archive" ;; | |
txt|srt|edc|c|h|sh|py|rb|lua|go|php|cpp|h|hpp|cc|hh|md|vim|conf|ini|am|ac|in|xml|html|xhtml|rss|atom|README|TODO|NOTES|ChangeLog|NEWS|AUTHORS|COPYING|INSTALL) | |
THUMB="text-x-generic" ;; | |
*) THUMB="empty";; | |
esac | |
fi | |
} | |
get_info | |
COL=1 | |
for FILENAME in $PARAM; do | |
ABSPATH="$(realpath "$FILENAME")" | |
BASENAME="$(basename "$ABSPATH")" | |
get_thumb "$ABSPATH" | |
declare_inline_media "$THUMB" | |
for (( ROW = 0 ; ROW < $HEIGHT ; ROW++ )); do | |
print_block | |
move left $WIDTH | |
move down | |
done | |
if [ "$LABEL" = "yes" ]; then | |
CAPTION="${BASENAME:0:$WIDTH}" | |
LEFT_PAD=$(( ($WIDTH + ${#CAPTION}) / 2 )) | |
printf "%*s" $LEFT_PAD "$CAPTION" | |
move left $LEFT_PAD | |
fi | |
COL=$(( $COL + $WIDTH + 3 )); | |
if [ $COL -ge $TERM_WIDTH ]; then | |
COL=1 | |
if [ "$LABEL" = "yes" ]; then | |
echo | |
fi | |
echo | |
else | |
move right $(( $WIDTH + 2 )) | |
move up $HEIGHT | |
fi | |
done | |
if [ $COL -gt $WIDTH -a $COL -lt $TERM_WIDTH ]; then | |
move down $HEIGHT | |
if [ "$LABEL" = "yes" ]; then | |
echo | |
fi | |
fi | |
echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment