Images: Added two new functions to fallback to ascii/off modes

This commit is contained in:
Dylan Araps 2016-11-13 15:53:52 +11:00
parent e262e30d93
commit 902976e618

View file

@ -1824,24 +1824,19 @@ get_image_backend() {
# Fallback to ascii mode if image isn't a file. # Fallback to ascii mode if image isn't a file.
if [[ ! -f "$image" ]]; then if [[ ! -f "$image" ]]; then
image_backend="ascii" to_ascii "Image: '$image' doesn't exist, falling back to ascii mode."
get_ascii 2>/dev/null
err "Image: '$image', doesn't exist, falling back to ascii mode."
return return
fi fi
# Set image program. get_image_program
if [[ -n "$ITERM_PROFILE" ]]; then get_term_size
image_program="iterm2"
elif [[ "$(tycat 2>/dev/null)" ]]; then # Fallback to ascii mode if terminal size wasn't found.
image_program="tycat" if [[ -z "$term_width" ]] && ((term_width == 0)); then
to_ascii "Image: Failed to find terminal window size"
else return
image_program="w3m"
fi fi
get_term_size
get_image_size get_image_size
make_thumbnail make_thumbnail
@ -1854,11 +1849,23 @@ get_image_backend() {
esac esac
} }
get_image_program() {
if [[ -n "$ITERM_PROFILE" ]]; then
image_program="iterm2"
elif [[ "$(tycat 2>/dev/null)" ]]; then
image_program="tycat"
else
image_program="w3m"
fi
}
get_ascii() { get_ascii() {
if [[ ! -f "$ascii" || "$ascii" == "distro" ]]; then if [[ ! -f "$ascii" || "$ascii" == "distro" ]]; then
# Error message # Error message
[[ "$ascii" != "distro" ]] && \ [[ "$ascii" != "distro" ]] && \
[[ ! -f "$ascii" ]] && err "Ascii: Ascii file not found, using distro ascii" [[ ! -f "$ascii" ]] && err "Ascii: Ascii file not found, using distro ascii."
# Lowercase the distro name # Lowercase the distro name
if (("$version" <= 3)); then if (("$version" <= 3)); then
@ -1889,9 +1896,7 @@ get_ascii() {
ascii="$script_dir/ascii/distro/${ascii/ *}" ascii="$script_dir/ascii/distro/${ascii/ *}"
else else
text_padding="0" to_off "Ascii: Ascii file not found, falling back to text mode."
image_backend="off"
err "Ascii: Ascii file not found, falling back to text mode."
return return
fi fi
fi fi
@ -2074,15 +2079,10 @@ get_term_size() {
fi fi
fi fi
# If $image isn't a file fallback to ascii mode. # If the terminal size was found correctly
if [[ "$term_width" ]] && ((term_width > 0)); then if [[ "$term_width" ]] && ((term_width > 0)); then
clear clear
zws=" " zws=" "
else
image_backend="ascii"
get_ascii
err "Image: Failed to get window size, falling back to ascii mode."
return
fi fi
} }
@ -2211,7 +2211,7 @@ display_image() {
# appearing in specific terminal emulators. # appearing in specific terminal emulators.
sleep 0.05 sleep 0.05
printf "%b\n" "0;1;$xoffset;$yoffset;$width;$height;;;;;$image\n4;\n3;" |\ printf "%b\n" "0;1;$xoffset;$yoffset;$width;$height;;;;;$image\n4;\n3;" |\
"$w3m_img_path" -bg "$background_color" >/dev/null & 2>&1 || text_padding="0" "$w3m_img_path" -bg "$background_color" >/dev/null & 2>&1 || to_off "Images: w3m-img failed to display the image."
;; ;;
"iterm2") "iterm2")
@ -2224,6 +2224,20 @@ display_image() {
esac esac
} }
to_ascii() {
# This function makes neofetch fallback to ascii mode.
image_backend="ascii"
get_ascii 2>/dev/null
err "$1"
}
to_off() {
# This function makes neofetch fallback to off mode.
text_padding="0"
image_backend="off"
err "$1"
}
# SCREENSHOT # SCREENSHOT
take_scrot() { take_scrot() {
@ -2286,7 +2300,7 @@ info() {
# If the output is empty, don't print anything. # If the output is empty, don't print anything.
[[ -z "${output// }" ]] && \ [[ -z "${output// }" ]] && \
err "Info: Couldn't detect $subtitle" && return err "Info: Couldn't detect $subtitle." && return
case "$1" in case "$1" in
title) title)
@ -2674,7 +2688,7 @@ get_default_config() {
fi fi
if source "$default_config"; then if source "$default_config"; then
err "Config: Sourced default config ($default_config)" err "Config: Sourced default config. ($default_config)"
else else
err "Config: Default config not found, continuing..." err "Config: Default config not found, continuing..."
fi fi
@ -2684,12 +2698,12 @@ get_user_config() {
# Check $config_file # Check $config_file
if [[ -f "$config_file" ]]; then if [[ -f "$config_file" ]]; then
source "$config_file" source "$config_file"
err "Config: Sourced user config ($config_file)" err "Config: Sourced user config. ($config_file)"
return return
elif [[ "$config_file" == "travis" ]]; then elif [[ "$config_file" == "travis" ]]; then
source "$travis_config" source "$travis_config"
err "Config: Sourced user config ($travis_config)" err "Config: Sourced user config. ($travis_config)"
return return
fi fi
mkdir -p "$XDG_CONFIG_HOME/neofetch/" mkdir -p "$XDG_CONFIG_HOME/neofetch/"
@ -2715,7 +2729,7 @@ get_user_config() {
fi fi
source "$config_file" source "$config_file"
err "Config: Sourced user config ($config_file)" err "Config: Sourced user config. ($config_file)"
} }
bar() { bar() {