123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- #!/bin/bash
- VER="1.2"
- LAST_UPDATED="December 5, 2014"
- # This is a wrapper of the wrapper that z3bra made for w3mimgdisplay ( http://blog.z3bra.org/2014/01/images-in-terminal.html ).
-
- # Keybinds
- KILL="q" # Quit out from termage
- PREV="n" # Go to the previous image
- NEXT="o" # Go to the next image
- REDRAW="r" # Redraw the current image
- INFO="i" # Print the information of an image
- TOGDUAL="d" # Toggle dual image mode.
-
- # SETTINGS
- FONTH=13 # Size of one terminal row
- FONTW=8 # Size of one terminal column
- IMGDISPLAY="/usr/lib/w3m/w3mimgdisplay" # The location of w3mimgdisplay
-
-
- # Colours
- BLACK="\033[0m\033[30m"
- RED="\033[0m\033[31m"
- GREEN="\033[0m\033[32m"
- YELLOW="\033[0m\033[33m"
- BLUE="\033[0m\033[34m"
- MAGENTA="\033[0m\033[35m"
- CYAN="\033[0m\033[36m"
- WHITE="\033[0m\033[37m"
-
- # Test for optional params
- DUAL=0
- QUIT=0
- if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
- # Help command
- echo -en "$WHITE"
- echo "termage Version $VER"
- echo "Last updated ${LAST_UPDATED}."
- echo ""
- echo "USE"
- echo " termage FILE: Display FILE in the terminal."
- echo " termage [-h | --help]: Display this message"
- echo " termage [-d | --dual] FILE: Displays two images next to each other."
- echo " * If there is only one image selected, this command does nothing."
- echo " termage [-q | --quit] FILE: End Termage after printing the first image."
- echo ""
- echo "HOTKEYS"
- echo " $KILL: Kill termage"
- echo " $PREV: Go to the previous image (when more then one image is loaded)."
- echo " $NEXT: Go to the next image (when more then one image is loaded)."
- echo " $REDRAW: Redraw the current image."
- echo " $INFO: Display information about the picture."
- echo " $TOGDUAL: Toggle dual image view."
- exit
- elif [[ "$1" == "-d" ]] || [[ "$1" == "--dual" ]]; then
- DUAL=1
- shift
- elif [[ "$1" == "-q" ]] || [[ "$1" == "--quit" ]]; then
- QUIT=1
- shift
- fi
-
- # Test the params for valid files.
- IMG=0
- FILE=()
- k=0
- echo -e "${WHITE}Loading selected files..."
- for arg in "$@"; do
- read h i <<< `echo -e "5;$arg" | $IMGDISPLAY`
- echo $h | grep -Eq '[0-9]' || echo -e "$RED$arg"
- echo $h | grep -Eq '[0-9]' || continue
- echo -e "$GREEN$arg"
- FILE[k]=$arg
- k=`expr $k + 1`
- done
- echo -e "${WHITE}Loaded all files."
-
- if [[ ${FILE[$IMG]} == "" ]]; then
- echo -e "${RED}Error: No valid files."
- exit
- fi
-
-
- # Functions
- fnDispImg()
- {
-
- W=$1 # width
- H=$2 # height
- MW=$3 # max width
- MH=$4 # max height
- FN=$5 # font height
- ID=$6 # image display
- D=$7 # number
- FH=$8 # Font height
- if test $W -gt $MW; then
- H=$(($H * $MW / $W))
- W=$MW
- fi
- if test $H -gt $MH; then
- W=$(($W * $MH / $H))
- H=$MH
- fi
-
- COMMANDS="0;1;$((MW * D));0;$W;$H;;;;;$FN\n4;\n3;"
- tput cup $(($MH/$FH)) 0
- echo -en $COMMANDS|$ID
- }
-
-
- # Begin Image display loop
- while true; do
- clear
- FILENAME=${FILE[$IMG]}
- COLUMNS=`tput cols`
- LINES=`tput lines`
-
- read width height <<< `echo -e "5;$FILENAME" | $IMGDISPLAY`
- im_width=$width
- im_height=$height
- max_width=$(($FONTW * $COLUMNS))
- max_height=$(($FONTH * $(($LINES - 2))))
-
- # if dual mode enabled split the maximum width in half
- if [[ "$DUAL" == 1 ]]; then
- max_width=$(($max_width / 2))
- fi
-
- # display the image
- fnDispImg $width $height $max_width $max_height $FILENAME $IMGDISPLAY 0 $FONTH
- #if dual mode is enabled display the second image
- if [[ "$DUAL" == 1 ]]; then
- IMG2=`expr $IMG + 1`
- IMG2=`expr $IMG2 % ${#FILE[@]}`
- FILENAME2=${FILE[IMG2]}
- read width2 height2 <<< `echo -e "5;$FILENAME2" | $IMGDISPLAY`
- im_width2=$width2
- im_height2=$height2
- fnDispImg $width2 $height2 $max_width $max_height $FILENAME2 $IMGDISPLAY 1 $FONTH
- fi
-
- # If the -q param was passed quit out of termage
- if [[ "$QUIT" == 1 ]]; then
- exit
- fi
-
- # Wait for and respond to key strokes
- if [ -t 0 ]; then stty -echo -icanon time 0 min 0; fi
- KEYPRESS=''
- while true; do
- read KEYPRESS
- case $KEYPRESS in
- $KILL) # Kill termage
- exit;;
- $PREV) # Previous image (changes to deal with dual image)
- if [[ "$DUAL" = 0 ]]; then
- IMG=`expr $IMG - 1`
- else
- IMG=`expr $IMG - 2`
- fi; break;;
- $NEXT) # Next image (changes to deal with dual image)
- if [[ "$DUAL" = 0 ]]; then
- IMG=`expr $IMG + 1`
- else
- IMG=`expr $IMG + 2`
- fi; break;;
- $REDRAW) # redraws the current image
- break;;
- $INFO) # Prints the info on the image (changes to deal with dual image)
- clear
- echo -en "$WHITE"
- echo "$FILENAME"
- echo "${im_width}x${im_height}"
- if [[ "$DUAL" = 1 ]]; then
- echo "-----------------------------------------"
- echo "$FILENAME2"
- echo "${im_width2}x${im_height2}"
- fi;;
- $TOGDUAL) # Toggles dual mode
- if [[ "$DUAL" = 0 ]]; then
- DUAL=1
- else
- DUAL=0
- fi; break;;
- *);;
- esac
- done
- IMG=`expr $IMG % ${#FILE[@]}`
- done
|