You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hace 4 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/bin/bash
  2. VER="1.2"
  3. LAST_UPDATED="December 5, 2014"
  4. # This is a wrapper of the wrapper that z3bra made for w3mimgdisplay ( http://blog.z3bra.org/2014/01/images-in-terminal.html ).
  5. # Keybinds
  6. KILL="q" # Quit out from termage
  7. PREV="n" # Go to the previous image
  8. NEXT="o" # Go to the next image
  9. REDRAW="r" # Redraw the current image
  10. INFO="i" # Print the information of an image
  11. TOGDUAL="d" # Toggle dual image mode.
  12. # SETTINGS
  13. FONTH=13 # Size of one terminal row
  14. FONTW=8 # Size of one terminal column
  15. IMGDISPLAY="/usr/lib/w3m/w3mimgdisplay" # The location of w3mimgdisplay
  16. # Colours
  17. BLACK="\033[0m\033[30m"
  18. RED="\033[0m\033[31m"
  19. GREEN="\033[0m\033[32m"
  20. YELLOW="\033[0m\033[33m"
  21. BLUE="\033[0m\033[34m"
  22. MAGENTA="\033[0m\033[35m"
  23. CYAN="\033[0m\033[36m"
  24. WHITE="\033[0m\033[37m"
  25. # Test for optional params
  26. DUAL=0
  27. QUIT=0
  28. if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
  29. # Help command
  30. echo -en "$WHITE"
  31. echo "termage Version $VER"
  32. echo "Last updated ${LAST_UPDATED}."
  33. echo ""
  34. echo "USE"
  35. echo " termage FILE: Display FILE in the terminal."
  36. echo " termage [-h | --help]: Display this message"
  37. echo " termage [-d | --dual] FILE: Displays two images next to each other."
  38. echo " * If there is only one image selected, this command does nothing."
  39. echo " termage [-q | --quit] FILE: End Termage after printing the first image."
  40. echo ""
  41. echo "HOTKEYS"
  42. echo " $KILL: Kill termage"
  43. echo " $PREV: Go to the previous image (when more then one image is loaded)."
  44. echo " $NEXT: Go to the next image (when more then one image is loaded)."
  45. echo " $REDRAW: Redraw the current image."
  46. echo " $INFO: Display information about the picture."
  47. echo " $TOGDUAL: Toggle dual image view."
  48. exit
  49. elif [[ "$1" == "-d" ]] || [[ "$1" == "--dual" ]]; then
  50. DUAL=1
  51. shift
  52. elif [[ "$1" == "-q" ]] || [[ "$1" == "--quit" ]]; then
  53. QUIT=1
  54. shift
  55. fi
  56. # Test the params for valid files.
  57. IMG=0
  58. FILE=()
  59. k=0
  60. echo -e "${WHITE}Loading selected files..."
  61. for arg in "$@"; do
  62. read h i <<< `echo -e "5;$arg" | $IMGDISPLAY`
  63. echo $h | grep -Eq '[0-9]' || echo -e "$RED$arg"
  64. echo $h | grep -Eq '[0-9]' || continue
  65. echo -e "$GREEN$arg"
  66. FILE[k]=$arg
  67. k=`expr $k + 1`
  68. done
  69. echo -e "${WHITE}Loaded all files."
  70. if [[ ${FILE[$IMG]} == "" ]]; then
  71. echo -e "${RED}Error: No valid files."
  72. exit
  73. fi
  74. # Functions
  75. fnDispImg()
  76. {
  77. W=$1 # width
  78. H=$2 # height
  79. MW=$3 # max width
  80. MH=$4 # max height
  81. FN=$5 # font height
  82. ID=$6 # image display
  83. D=$7 # number
  84. FH=$8 # Font height
  85. if test $W -gt $MW; then
  86. H=$(($H * $MW / $W))
  87. W=$MW
  88. fi
  89. if test $H -gt $MH; then
  90. W=$(($W * $MH / $H))
  91. H=$MH
  92. fi
  93. COMMANDS="0;1;$((MW * D));0;$W;$H;;;;;$FN\n4;\n3;"
  94. tput cup $(($MH/$FH)) 0
  95. echo -en $COMMANDS|$ID
  96. }
  97. # Begin Image display loop
  98. while true; do
  99. clear
  100. FILENAME=${FILE[$IMG]}
  101. COLUMNS=`tput cols`
  102. LINES=`tput lines`
  103. read width height <<< `echo -e "5;$FILENAME" | $IMGDISPLAY`
  104. im_width=$width
  105. im_height=$height
  106. max_width=$(($FONTW * $COLUMNS))
  107. max_height=$(($FONTH * $(($LINES - 2))))
  108. # if dual mode enabled split the maximum width in half
  109. if [[ "$DUAL" == 1 ]]; then
  110. max_width=$(($max_width / 2))
  111. fi
  112. # display the image
  113. fnDispImg $width $height $max_width $max_height $FILENAME $IMGDISPLAY 0 $FONTH
  114. #if dual mode is enabled display the second image
  115. if [[ "$DUAL" == 1 ]]; then
  116. IMG2=`expr $IMG + 1`
  117. IMG2=`expr $IMG2 % ${#FILE[@]}`
  118. FILENAME2=${FILE[IMG2]}
  119. read width2 height2 <<< `echo -e "5;$FILENAME2" | $IMGDISPLAY`
  120. im_width2=$width2
  121. im_height2=$height2
  122. fnDispImg $width2 $height2 $max_width $max_height $FILENAME2 $IMGDISPLAY 1 $FONTH
  123. fi
  124. # If the -q param was passed quit out of termage
  125. if [[ "$QUIT" == 1 ]]; then
  126. exit
  127. fi
  128. # Wait for and respond to key strokes
  129. if [ -t 0 ]; then stty -echo -icanon time 0 min 0; fi
  130. KEYPRESS=''
  131. while true; do
  132. read KEYPRESS
  133. case $KEYPRESS in
  134. $KILL) # Kill termage
  135. exit;;
  136. $PREV) # Previous image (changes to deal with dual image)
  137. if [[ "$DUAL" = 0 ]]; then
  138. IMG=`expr $IMG - 1`
  139. else
  140. IMG=`expr $IMG - 2`
  141. fi; break;;
  142. $NEXT) # Next image (changes to deal with dual image)
  143. if [[ "$DUAL" = 0 ]]; then
  144. IMG=`expr $IMG + 1`
  145. else
  146. IMG=`expr $IMG + 2`
  147. fi; break;;
  148. $REDRAW) # redraws the current image
  149. break;;
  150. $INFO) # Prints the info on the image (changes to deal with dual image)
  151. clear
  152. echo -en "$WHITE"
  153. echo "$FILENAME"
  154. echo "${im_width}x${im_height}"
  155. if [[ "$DUAL" = 1 ]]; then
  156. echo "-----------------------------------------"
  157. echo "$FILENAME2"
  158. echo "${im_width2}x${im_height2}"
  159. fi;;
  160. $TOGDUAL) # Toggles dual mode
  161. if [[ "$DUAL" = 0 ]]; then
  162. DUAL=1
  163. else
  164. DUAL=0
  165. fi; break;;
  166. *);;
  167. esac
  168. done
  169. IMG=`expr $IMG % ${#FILE[@]}`
  170. done