Skip to content

Instantly share code, notes, and snippets.

@dvejmz
Created October 17, 2017 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvejmz/662ef4e12cc00c2eb1d495f1720d6de8 to your computer and use it in GitHub Desktop.
Save dvejmz/662ef4e12cc00c2eb1d495f1720d6de8 to your computer and use it in GitHub Desktop.
Wrapper script to use w3mimgdisplay to render images on a terminal
#!/bin/bash
#
# z3bra -- 2014-01-21
if [[ $# != 2 ]]; then
echo "Insufficient arguments."
exit 1
fi
W3MIMGDISPLAY="/usr/libexec/w3m/w3mimgdisplay"
FILENAME="${2}"
FONTH=14 # Size of one terminal row
FONTW=8 # Size of one terminal column
COLUMNS=`tput cols`
LINES=`tput lines`
read width height <<< `echo -e "5;$FILENAME" | $W3MIMGDISPLAY`
max_width=$(($FONTW * $COLUMNS))
max_height=$(($FONTH * $(($LINES - 2)))) # substract one line for prompt
if test $width -gt $max_width; then
height=$(($height * $max_width / $width))
width=$max_width
fi
if test $height -gt $max_height; then
width=$(($width * $max_height / $height))
height=$max_height
fi
while getopts ":c:d" COMMAND;
do
case ${COMMAND} in
c) w3m_command="6;0;0;$width;$height;\n4;\n3;"
;; # Clear image
d) w3m_command="0;1;0;0;$width;$height;;;;;$FILENAME\n4;\n3;"
;; # Draw image
esac
done
tput cup $(($height/$FONTH)) 0
echo -e $w3m_command|$W3MIMGDISPLAY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment