Skip to content

Instantly share code, notes, and snippets.

@tomoe-mami
Last active December 15, 2015 12:49
Show Gist options
  • Save tomoe-mami/5262557 to your computer and use it in GitHub Desktop.
Save tomoe-mami/5262557 to your computer and use it in GitHub Desktop.
showing file thumbnails in terminology
#!/bin/sh
WIDTH=6
HEIGHT=3
CHAR="."
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 = amount
function move_left {
echo -ne "\e[${1}D"
}
# $1 = amount
function move_right {
echo -ne "\e[${1}C"
}
# $1 = amount
function move_up {
echo -ne "\e[${1}A"
}
# $1 = amount
function move_down {
echo -ne "\e[${1}B"
}
get_info
COL=1
for FILENAME in $PARAM; do
ABSPATH="$(realpath "$FILENAME")"
if [ -d "$ABSPATH" ]; then
THUMB="folder"
else
BASENAME="$(basename "$ABSPATH")"
EXT="${BASENAME##*.}"
case "$EXT" in
jpg|jpeg|png|gif|bmp|tif|ico|svg|pdf)
THUMB="$ABSPATH";;
*) THUMB="empty";;
esac
fi
declare_inline_media "$THUMB"
for (( ROW = 0 ; ROW < $HEIGHT ; ROW++ )); do
print_block
move_left $WIDTH
move_down 1
done
COL=$(( $COL + $WIDTH + 2 ));
if [ $COL -ge $TERM_WIDTH ]; then
COL=1
echo
else
move_right $(( $WIDTH + 1))
move_up $HEIGHT
fi
done
for (( ROW = 0 ; ROW < $HEIGHT ; ROW++)); do echo; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment