Image: Rewrite image backends

This commit is contained in:
Dylan Araps 2017-01-14 20:59:12 +11:00
parent 1444bb10d9
commit 1c2569327f
2 changed files with 78 additions and 83 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

155
neofetch
View file

@ -1941,54 +1941,45 @@ get_cols() {
# IMAGES # IMAGES
get_image_backend() { image_backend() {
# This function determines which image backend to use if [[ ! "${image_backend}" =~ (off|ascii) ]]; then
# by checking for programs and etc. # Fallback to ascii mode if imagemagick isn't installed.
type -p convert >/dev/null 2>&1 || image_backend="ascii"
# If the image source isn't "ascii" or "off".
if [[ ! "${image_source}" =~ ^(off|ascii)$ ]]; then
# If X isn't running force ascii mode here. # If X isn't running force ascii mode here.
[[ -z "$DISPLAY" ]] && image_source="ascii" [[ -z "$DISPLAY" ]] && image_source="ascii"
# Fallback to ascii mode if imagemagick isn't installed.
type -p convert >/dev/null 2>&1 || image_source="ascii"
fi fi
# Get the image program. image_backend="off"
get_image_program
# If image source is ascii fallback to ascii. case "${image_backend:-off}" in
if [[ "$image_source" == "ascii" ]]; then "ascii") get_ascii ;;
to_ascii "Image: \$image_source set to 'ascii', falling back to ascii mode." "off") image_backend="off" ;;
err "Image: Change \$image_source to another value to use image mode."
"iterm2" | "w3m-img" | "tycat")
get_image_source
if [[ ! -f "$image" ]]; then
to_ascii "Image: \'$image_source\' doesn't exist, falling back to ascii mode."
return return
fi fi
case "${image_backend:=image}" in get_image_program
"image")
case "$image_source" in
"wall"*) get_wallpaper 2>/dev/null ;;
"off") image_backend="off"; return ;;
*)
if [[ -d "$image_source" ]]; then
files=("${image_source%/}"/*.{png,jpg,jpeg})
printf -v image "%s" "${files[RANDOM % (${#files[@]} - 1)]}"
else
image="$image_source"
fi
;;
esac
# Fallback to ascii mode if image isn't a file. if type -p "$image_program" >/dev/null 2>&1; then
if [[ ! -f "$image" ]]; then err "Image: Drawing images using $image_program"
to_ascii "Image: '$image' doesn't exist, falling back to ascii mode." else
to_ascii "Image: Failed to find image program. ($image_program)"
err "Image: Falling back to ascii mode."
return return
fi fi
get_term_size get_term_size
# Fallback to ascii mode if terminal size wasn't found. if [[ "$term_width" ]] && ((term_width >= 1)); then
if [[ -z "$term_width" ]] || ((term_width == 0)); then clear
zws=" "
else
to_ascii "Image: Failed to find terminal window size" to_ascii "Image: Failed to find terminal window size"
err "Image: Check the 'Images in the terminal' wiki page for more info" err "Image: Check the 'Images in the terminal' wiki page for more info"
return return
@ -1996,16 +1987,18 @@ get_image_backend() {
get_image_size get_image_size
make_thumbnail make_thumbnail
display_image
# If the backend is still set to "image" after
# make_thumbnail(), then display the image.
[[ "$image_backend" == "image" ]] && display_image
;; ;;
"ascii") get_ascii 2>/dev/null ;; *)
image_backend="off"
err "Image: Unknown image backend specified. ($image_backend)"
err "Image: Valid backends are: 'iterm2', 'w3m-img'. 'tycat', 'ascii', 'off'"
err "Image: Falling back to off mode."
;;
esac esac
# Set cursor position next to ascii art. # Set cursor position next image/ascii.
[[ "$image_backend" != "off" ]] && printf "%b" "\033[${lines:-0}A\033[9999999D" [[ "$image_backend" != "off" ]] && printf "%b" "\033[${lines:-0}A\033[9999999D"
} }
@ -2066,39 +2059,22 @@ get_ascii() {
export LC_ALL=C export LC_ALL=C
} }
get_image_program() { get_image_source() {
if [[ -n "$ITERM_PROFILE" ]]; then case "$image_source" in
image_program="iterm2" "wall"*)
get_wallpaper 2>/dev/null
;;
elif [[ "$(tycat 2>/dev/null)" ]]; then *)
image_program="tycat" if [[ -d "$image_source" ]]; then
files=("${image_source%/}"/*.{png,jpg,jpeg,jpe,gif})
printf -v image "%s" "${files[RANDOM % (${#files[@]} - 1)]}"
else else
image_program="w3m" image="$image_source"
get_w3m_img_path
fi
}
get_w3m_img_path() {
if [[ -x "$w3m_img_path" ]]; then
return
elif [[ -x "/usr/lib/w3m/w3mimgdisplay" ]]; then
w3m_img_path="/usr/lib/w3m/w3mimgdisplay"
elif [[ -x "/usr/libexec/w3m/w3mimgdisplay" ]]; then
w3m_img_path="/usr/libexec/w3m/w3mimgdisplay"
elif [[ -x "/usr/lib64/w3m/w3mimgdisplay" ]]; then
w3m_img_path="/usr/lib64/w3m/w3mimgdisplay"
elif [[ -x "/usr/libexec64/w3m/w3mimgdisplay" ]]; then
w3m_img_path="/usr/libexec64/w3m/w3mimgdisplay"
else
image_backend="ascii"
err "Image: w3m-img wasn't found on your system, falling back to ascii mode."
fi fi
;;
esac
} }
get_wallpaper() { get_wallpaper() {
@ -2162,6 +2138,33 @@ get_wallpaper() {
[[ "${image/*\./}" == "xml" ]] && image="" [[ "${image/*\./}" == "xml" ]] && image=""
} }
get_image_program() {
if [[ -n "$ITERM_PROFILE" ]]; then
image_program="iterm2"
elif [[ "$(tycat 2>/dev/null)" ]]; then
image_program="tycat"
else
# Find w3m-img path.
if [[ -x "$w3m_img_path" ]]; then
image_program="$w3m_img_path"
elif [[ -x "/usr/lib/w3m/w3mimgdisplay" ]]; then
image_program="/usr/lib/w3m/w3mimgdisplay"
elif [[ -x "/usr/libexec/w3m/w3mimgdisplay" ]]; then
image_program="/usr/libexec/w3m/w3mimgdisplay"
elif [[ -x "/usr/lib64/w3m/w3mimgdisplay" ]]; then
image_program="/usr/lib64/w3m/w3mimgdisplay"
elif [[ -x "/usr/libexec64/w3m/w3mimgdisplay" ]]; then
image_program="/usr/libexec64/w3m/w3mimgdisplay"
fi
fi
}
get_term_size() { get_term_size() {
# This functions gets the current window size in # This functions gets the current window size in
# pixels. # pixels.
@ -2230,12 +2233,6 @@ get_term_size() {
term_width=0 term_width=0
fi fi
fi fi
# If the terminal size was found correctly.
if [[ "$term_width" ]] && ((term_width >= 1)); then
clear
zws=" "
fi
} }
get_image_size() { get_image_size() {
@ -2358,7 +2355,7 @@ make_thumbnail() {
display_image() { display_image() {
case "$image_program" in case "$image_program" in
"w3m") *"w3m"*)
# Add a tiny delay to fix issues with images not # Add a tiny delay to fix issues with images not
# appearing in specific terminal emulators. # appearing in specific terminal emulators.
sleep 0.05 sleep 0.05
@ -2383,9 +2380,6 @@ to_ascii() {
# Print the ascii art. # Print the ascii art.
get_ascii 2>/dev/null get_ascii 2>/dev/null
# Move cursor next to ascii art.
printf "%b" "\033[${lines:-0}A\033[9999999D"
# Log the error. # Log the error.
err "$1" err "$1"
} }
@ -3404,7 +3398,7 @@ get_term_padding() {
dynamic_prompt() { dynamic_prompt() {
case "$image_backend" in case "$image_backend" in
"image") "iterm2" | "w3m-img" | "tycat")
get_term_padding 2>/dev/null get_term_padding 2>/dev/null
# Calculate image height in terminal cells. # Calculate image height in terminal cells.
@ -4001,7 +3995,8 @@ main() {
printf "\033[?25l\033[?7l" printf "\033[?25l\033[?7l"
fi fi
get_image_backend # get_image_backend
image_backend
old_functions old_functions
get_cache_dir get_cache_dir
print_info 2>/dev/null print_info 2>/dev/null