Merge pull request #616 from dylanaraps/image-rewrite

Image: Rewrite backend handling.
This commit is contained in:
Dylan Araps
2017-01-18 09:43:24 +11:00
committed by GitHub
3 changed files with 272 additions and 243 deletions

291
neofetch
View File

@@ -1948,78 +1948,62 @@ get_cols() {
# IMAGES
get_image_backend() {
# This function determines which image backend to use
# by checking for programs and etc.
image_backend() {
if [[ ! "$image_backend" =~ (off|ascii) ]]; then
if ! type -p convert >/dev/null 2>&1; then
image_backend="ascii"
err "Image: Imagemagick not found, falling back to ascii mode."
fi
# If the image source isn't "ascii" or "off".
if [[ ! "${image_source}" =~ ^(off|ascii)$ ]]; then
# If X isn't running force ascii mode here.
[[ -z "$DISPLAY" ]] && image_source="ascii"
# Fallback to ascii mode if imagemagick isn't installed.
type -p convert >/dev/null 2>&1 || image_source="ascii"
[[ -z "$DISPLAY" ]] && image_backend="ascii"
fi
# Get the image program.
get_image_program
case "${image_backend:-off}" in
"ascii") get_ascii ;;
"off") image_backend="off" ;;
# If image source is ascii fallback to ascii.
if [[ "$image_source" == "ascii" ]]; then
to_ascii "Image: \$image_source set to 'ascii', falling back to ascii mode."
err "Image: Change \$image_source to another value to use image mode."
return
fi
"caca" | "catimg" | "jp2a" | "iterm2" | "tycat" | "w3m")
get_image_source
case "${image_backend:=image}" in
"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 [[ ! -f "$image" ]]; then
to_ascii "Image: '$image' doesn't exist, falling back to ascii mode."
to_ascii "Image: '$image_source' doesn't exist, falling back to ascii mode."
return
fi
get_term_size
# Fallback to ascii mode if terminal size wasn't found.
if [[ -z "$term_width" ]] || ((term_width == 0)); then
to_ascii "Image: Failed to find terminal window size"
err "Image: Check the 'Images in the terminal' wiki page for more info"
if [[ "$term_width" ]] && ((term_width >= 1)); then
clear
zws=" "
else
unset image_program
to_ascii "Image: Failed to find terminal window size."
err "Image: Check the 'Images in the terminal' wiki page for more info,"
return
fi
get_image_size
make_thumbnail
# If the backend is still set to "image" after
# make_thumbnail(), then display the image.
[[ "$image_backend" == "image" ]] && display_image
display_image 2>/dev/null
;;
"ascii") get_ascii 2>/dev/null ;;
*)
err "Image: Unknown image backend specified '$image_backend'."
err "Image: Valid backends are: 'ascii', 'caca', 'catimg', 'jp2a', 'iterm2', 'off', 'tycat', 'w3m')"
err "Image: Falling back to ascii mode."
get_ascii
;;
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"
}
get_ascii() {
if [[ ! -f "$ascii" || "$ascii" == "distro" ]]; then
if [[ ! -f "$image_source" || "$image_source" =~ (auto|ascii) ]]; then
# Fallback to distro ascii mode if custom ascii isn't found.
[[ "$ascii" != "distro" && ! -f "$ascii" ]] && \
[[ ! "$image_source" =~ (auto|ascii) ]] && \
err "Ascii: Ascii file not found, using distro ascii."
if [[ -d "/usr/share/neofetch/ascii/distro" ]]; then
@@ -2036,10 +2020,10 @@ get_ascii() {
ascii_dir="${script_dir}/ascii/distro"
fi
ascii="${ascii_dir}/${ascii_file}"
image_source="${ascii_dir}/${ascii_file}"
# Fallback to no ascii mode if distro ascii isn't found.
[[ ! -f "$ascii" ]] && \
[[ ! -f "$image_source" ]] && \
{ to_off "Ascii: Failed to find distro ascii, falling back to no ascii mode."; return; }
fi
@@ -2055,7 +2039,7 @@ get_ascii() {
line="${line//\\\\/\\}"
((${#line} > ascii_length)) && ascii_length="${#line}"
((++lines))
done < "$ascii"
done < "$image_source"
# Colors.
print="${print//'${c1}'/$c1}"
@@ -2073,39 +2057,26 @@ get_ascii() {
export LC_ALL=C
}
get_image_program() {
if [[ -n "$ITERM_PROFILE" ]]; then
image_program="iterm2"
get_image_source() {
case "$image_source" in
"auto" | "wall"*)
get_wallpaper 2>/dev/null
;;
elif [[ "$(tycat 2>/dev/null)" ]]; then
image_program="tycat"
*)
if [[ -d "$image_source" ]]; then
shopt -s nullglob
files=("${image_source%/}"/*.{png,jpg,jpeg,jpe,gif})
shopt -u nullglob
image="${files[RANDOM % ${#files[@]}]}"
else
image_program="w3m"
get_w3m_img_path
fi
}
else
image="$image_source"
fi
;;
esac
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
err "Image: Using image $image"
}
get_wallpaper() {
@@ -2169,6 +2140,25 @@ get_wallpaper() {
[[ "${image/*\./}" == "xml" ]] && image=""
}
get_w3m_img_path() {
# Find w3m-img path.
if [[ -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
err "Image: w3m-img wasn't found on your system"
fi
}
get_term_size() {
# This functions gets the current window size in
# pixels.
@@ -2237,12 +2227,6 @@ get_term_size() {
term_width=0
fi
fi
# If the terminal size was found correctly.
if [[ "$term_width" ]] && ((term_width >= 1)); then
clear
zws=" "
fi
}
get_image_size() {
@@ -2364,13 +2348,20 @@ make_thumbnail() {
}
display_image() {
case "$image_program" in
"w3m")
# Add a tiny delay to fix issues with images not
# appearing in specific terminal emulators.
sleep 0.05
printf "%b\n" "0;1;$xoffset;$yoffset;$width;$height;;;;;$image\n4;\n3;" |\
"$w3m_img_path" -bg "$background_color" >/dev/null & 2>&1 || to_off "Images: w3m-img failed to display the image."
case "$image_backend" in
"caca")
img2txt -W "$((width / font_width)))" -H "$((height / font_height))" --gamma=0.6 "$image" || to_off "Image: catimg failed to display the image."
zws=
;;
"catimg")
catimg -w "$((width * 2 / font_width))" -r 0 "$image" || to_off "Image: catimg failed to display the image."
zws=
;;
"jp2a")
jp2a --width="$((width / font_width))" --colors "$image" || to_off "Image: jp2a failed to display the image."
zws=
;;
"iterm2")
@@ -2378,31 +2369,41 @@ display_image() {
;;
"tycat")
tycat "$image" || to_off "Images: tycat failed to display the image."
tycat "$image" || to_off "Image: tycat failed to display the image."
;;
"w3m")
get_w3m_img_path
# Add a tiny delay to fix issues with images not
# appearing in specific terminal emulators.
sleep 0.05
printf "%b\n" "0;1;$xoffset;$yoffset;$width;$height;;;;;$image\n4;\n3;" |\
"${w3m_img_path:-false}" -bg "$background_color" >/dev/null & 2>&1 || to_off "Image: w3m-img failed to display the image."
;;
esac
}
to_ascii() {
# Log the error.
err "$1"
# This function makes neofetch fallback to ascii mode.
image_backend="ascii"
# Print the ascii art.
get_ascii 2>/dev/null
# Move cursor next to ascii art.
# Set cursor position next image/ascii.
printf "%b" "\033[${lines:-0}A\033[9999999D"
# Log the error.
err "$1"
}
to_off() {
# This function makes neofetch fallback to off mode.
err "$1"
image_backend="off"
text_padding=
zws=
err "$1"
}
# SCREENSHOT
@@ -2555,7 +2556,7 @@ get_underline() {
get_line_break() {
# Print it directly.
printf "%s\n" "${zws} "
printf "%s\n" "${zws}"
# Calculate info height.
((++info_height))
@@ -3411,13 +3412,14 @@ get_term_padding() {
dynamic_prompt() {
case "$image_backend" in
"image")
"ascii") ;;
"off") return ;;
*)
get_term_padding 2>/dev/null
# Calculate image height in terminal cells.
lines="$(((border * 2 + height + yoffset) / font_height))"
;;
"off") return ;;
esac
# If the info is higher than the ascii/image place the prompt
@@ -3714,33 +3716,23 @@ BARS:
Possible values: bar, infobar, barinfo, off
--disk_display mode Bar mode.
Possible values: bar, infobar, barinfo, off
IMAGE:
--image type Image source. Where and what image we display.
Possible values: wall, ascii,
/path/to/img, /path/to/dir/, off
--size 00px | --size 00% How to size the image.
Possible values: auto, 00px, 00%, none
--crop_mode mode Which crop mode to use
Possible values: normal, fit, fill
--crop_offset value Change the crop offset for normal mode.
Possible values: northwest, north, northeast,
west, center, east, southwest, south, southeast
--xoffset px How close the image will be to the left edge of the
window. This only works with w3m.
--yoffset px How close the image will be to the top edge of the
window. This only works with w3m.
--bg_color color Background color to display behind transparent image.
This only works with w3m.
--gap num Gap between image and text.
IMAGE BACKEND:
--backend backend Which image backend to use.
Possible values: 'ascii', 'caca', 'catimg', 'jp2a', 'iterm2', 'off', 'tycat', 'w3m'
--source source Which image or ascii file to use.
Possible values: 'auto', 'ascii', 'wallpaper', '/path/to/img', '/path/to/ascii', '/path/to/dir/'
--ascii source Shortcut to use 'ascii' backend.
--caca source Shortcut to use 'caca' backend.
--catimg source Shortcut to use 'catimg' backend.
--iterm2 source Shortcut to use 'iterm2' backend.
--tycat source Shortcut to use 'tycat' backend.
--w3m source Shortcut to use 'w3m' backend.
--off Shortcut to use 'off' backend.
NOTE: --gap can take a negative value which will move the text closer to the left side.
--clean Delete cached files and thumbnails.
NOTE: 'source; can be any of the following: 'auto', 'ascii', 'wallpaper', '/path/to/img', '/path/to/ascii', '/path/to/dir/'
ASCII:
--ascii value Where to get the ascii from, Possible values:
distro, /path/to/ascii
--ascii_colors x x x x x x Colors to print the ascii art
--ascii_distro distro Which Distro's ascii art to print
@@ -3759,6 +3751,29 @@ ASCII:
--ascii_bold on/off Whether or not to bold the ascii logo.
-L, --logo Hide the info text and only show the ascii logo.
Possible values: bar, infobar, barinfo, off
IMAGE:
--size 00px | --size 00% How to size the image.
Possible values: auto, 00px, 00%, none
--crop_mode mode Which crop mode to use
Takes the values: normal, fit, fill
--crop_offset value Change the crop offset for normal mode.
Possible values: northwest, north, northeast,
west, center, east, southwest, south, southeast
--xoffset px How close the image will be to the left edge of the
window. This only works with w3m.
--yoffset px How close the image will be to the top edge of the
window. This only works with w3m.
--bg_color color Background color to display behind transparent image.
This only works with w3m.
--gap num Gap between image and text.
NOTE: --gap can take a negative value which will move the text closer to the left side.
--clean Delete cached files and thumbnails.
SCREENSHOT:
-s, --scrot /path/to/img Take a screenshot, if path is left empty the screen-
shot function will use \$scrot_dir and \$scrot_name.
@@ -3814,7 +3829,7 @@ exit 1
get_args() {
# Check the commandline flags early for '--config'.
[[ "$@" =~ --config ]] || get_user_config 2>/dev/null
[[ "$@" != *--config* ]] && get_user_config 2>/dev/null
while [[ "$1" ]]; do
case "$1" in
@@ -3910,12 +3925,18 @@ get_args() {
"--battery_display") battery_display="$2" ;;
"--disk_display") disk_display="$2" ;;
# Image
"--image")
image_source="$2"
case "$2" in "-"* | "" | "ascii") image_backend="ascii" ;; esac
# Image backend
"--backend") image_backend="$2" ;;
"--source") image_source="$2" ;;
"--ascii" | "--caca" | "--catimg" | "--jp2a" | "--iterm2" | "--off" | "--tycat" | "--w3m")
image_backend="${1/--}"
case "$2" in
"-"* | "") ;;
*) image_source="$2" ;;
esac
;;
# Image options
"--image_size" | "--size") image_size="$2" ;;
"--crop_mode") crop_mode="$2" ;;
"--crop_offset") crop_offset="$2" ;;
@@ -3930,13 +3951,6 @@ get_args() {
exit
;;
# Ascii
"--ascii")
image_backend="ascii"
ascii="$2"
case "$2" in "-"* | "") ascii="distro" ;; esac
;;
"--ascii_colors")
unset ascii_colors
for arg in "$2" "$3" "$4" "$5" "$6" "$7"; do
@@ -4008,7 +4022,8 @@ main() {
printf "\033[?25l\033[?7l"
fi
get_image_backend
# get_image_backend
image_backend
old_functions
get_cache_dir
print_info 2>/dev/null
@@ -4016,7 +4031,7 @@ main() {
# w3m-img: Draw the image a second time to fix
# rendering issues in specific terminal emulators.
[[ "$image_backend" == "image" && "$image_program" == "w3m" ]] && display_image
[[ "$image_program" == *w3m* ]] && display_image 2>/dev/null
# Take a screenshot.
[[ "$scrot" == "on" ]] && take_scrot