Fallback to ascii mode instead of hanging if the terminal emulator doesn't support xterm escape sequences

This commit is contained in:
Dylan 2016-02-19 09:54:16 +11:00
parent 6bb05edf86
commit 43239ecdc1
2 changed files with 20 additions and 19 deletions

View File

@ -262,8 +262,8 @@ crop_offset="center"
# Image size # Image size
# The image is half the terminal width by default. # The image is half the terminal width by default.
# --size half, px # --size auto, px
image_size="half" image_size="auto"
# Right gap between image and text # Right gap between image and text
# --gap num # --gap num

35
fetch
View File

@ -280,8 +280,8 @@ crop_offset="center"
# Image size # Image size
# The image is half the terminal width by default. # The image is half the terminal width by default.
# --size half, px # --size auto, px
image_size="half" image_size="auto"
# Right gap between image and text # Right gap between image and text
# --gap num # --gap num
@ -1851,8 +1851,21 @@ getimage () {
;; ;;
esac esac
# If $img isn't a file, fallback to ascii mode. # Get terminal width and height
if [ ! -f "$img" ]; then printf "%b%s" '\033[14t'
index=0
while IFS= read -s -r -n 1 -t 0.25 char; do
case "$index" in
"0") [ "$char" == ";" ] && index=$((index + 1)) ;;
"1") [ "$char" == ";" ] && index=$((index + 1)) || term_height="${term_height}${char}" ;;
"2") [ "$char" == "t" ] && break || term_width="${term_width}${char}"
esac
done
# If $img isn't a file or the terminal doesn't support xterm escape sequences,
# fallback to ascii mode.
if [ ! -f "$img" ] || [ -z "$term_height" ]; then
# Fallback to ascii mode # Fallback to ascii mode
image="ascii" image="ascii"
getascii getascii
@ -1864,24 +1877,12 @@ getimage () {
columns=$(tput cols) columns=$(tput cols)
lines=$(tput lines) lines=$(tput lines)
# Get terminal width and height
printf "%b%s" '\033[14t'
index=0
while IFS= read -s -r -n 1 char; do
case "$index" in
"0") [ "$char" == ";" ] && index=$((index + 1)) ;;
"1") [ "$char" == ";" ] && index=$((index + 1)) || term_height="${term_height}${char}" ;;
"2") [ "$char" == "t" ] && break || term_width="${term_width}${char}"
esac
done
# Calculate font size # Calculate font size
font_width=$((term_width / columns)) font_width=$((term_width / columns))
font_height=$((term_height / lines)) font_height=$((term_height / lines))
# Image size is half of the terminal # Image size is half of the terminal
if [ "$image_size" == "half" ]; then if [ "$image_size" == "auto" ]; then
image_size=$((columns * font_width / 2)) image_size=$((columns * font_width / 2))
[ "$((term_height - term_height / 4))" -lt "$image_size" ] && \ [ "$((term_height - term_height / 4))" -lt "$image_size" ] && \