-
-
Save quirinpa/ea0d7df5d6bf2aae1ef7199360d6df53 to your computer and use it in GitHub Desktop.
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 | |
# iv - a bash image viewer that uses w3mimgdisplay | |
# Usage: iv [filename] | |
# Licence: GPL - quirinpa@gmail.com | |
# Inspired by - | |
# blog.z3bra.org/2014/01/images-in-terminal.html | |
# wiki.vifm.info/index.php?title=How_to_preview_images | |
# Saitoha sensei... Gomen nasai. Ore wa boke desu. | |
# Works best with xterm | |
printf '' "$( | |
fsize=(14 7) | |
# get number of cols / lines | |
read -a tsize <<< $(stty size) | |
# get terminal size in pixels | |
ssize=($((${fsize[0]} * (${tsize[0]} - 1))) $((${fsize[1]} * ${tsize[1]}))) | |
# get image dimensions | |
read -a isize <<< $(identify -format '%h %w' $1) | |
# find ratios | |
sratio=$((${ssize[0]} * 100 / ${ssize[1]})) | |
iratio=$((${isize[0]} * 100 / ${isize[1]})) | |
# decide how the image will spread | |
if [[ "$sratio" -gt "$iratio" ]]; then | |
# fit horizontally, spread vertically | |
[[ "${ssize[1]}" -gt "${isize[1]}" ]] && tw=${isize[1]} || tw=${ssize[1]} | |
dsize=($(($tw * $iratio / 100)) $tw) | |
else | |
# fit vertically, spread horizontally | |
echo fit vertically > /dev/tty | |
[[ "${ssize[0]}" -gt "${isize[0]}" ]] && th=${isize[0]} || th=${ssize[0]} | |
dsize=($th $(($th * 100 / $iratio))) | |
fi | |
# from Dennis Williamson's answer on | |
# stackoverflow.com/questions/2575037/how-to-get-the-cursor-position-in-bash | |
get_current_line() { | |
exec < /dev/tty | |
oldstty=$(stty -g) | |
stty raw -echo min 0 | |
echo -en "\033[6n" > /dev/tty | |
IFS=';' read -r -d R -a pos | |
stty $oldstty | |
echo ${pos[0]:2} | |
} | |
# get number of lines that the image will occupy | |
b=$((${dsize[0]} / ${fsize[0]})) | |
# get starting line | |
sl=$((`get_current_line` - 1)) | |
# get number of available lines | |
al="$((${tsize[0]} - 1 - $sl))" | |
[[ "$al" -ge "$b" ]] && al=$b | |
# move down visible lines | |
printf "\033["$(($b + 1))"B" > /dev/tty | |
# find lines which need to be added | |
il=$(($b - $al)) | |
# print them | |
for i in `seq $il`; do printf "\n" > /dev/tty; done | |
printf "0;1;0;$((($sl - $il) * ${fsize[0]}));${dsize[1]};${dsize[0]};;;${isize[1]};${isize[0]};$1\\n4;\n3;" | /usr/lib/w3m/w3mimgdisplay | |
)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment