Images; Rewrite functions

This commit is contained in:
Dylan Araps 2016-11-13 15:12:22 +11:00
parent 5ccbb39fbf
commit a1b79bd33b

144
neofetch
View file

@ -1791,13 +1791,17 @@ get_cols() {
# Add newlines to the string.
cols="${cols%%'nl'}"
cols="${cols//nl/\\n${padding}${zws}}"
cols="${cols//nl/\\n\\033[${text_padding}C${zws}}"
fi
}
# IMAGES
get_image_backend() {
# This function determines which image backend to use
# by checking for programs and etc.
# Automatically find w3m-img
get_w3m_img_path
# Fallback to ascii mode if imagemagick isn't installed.
@ -1815,11 +1819,18 @@ get_image_backend() {
else
img="$image_source"
fi
[ ! -f "$img" ] && { image_backend="ascii"; get_ascii 2>/dev/null; return; }
;;
esac
# Fallback to ascii mode if image isn't a file.
if [[ ! -f "$img" ]]; then
image_backend="ascii"
get_ascii 2>/dev/null
err "Image: '$img', doesn't exist, falling back to ascii mode."
return
fi
# Set image program.
if [[ -n "$ITERM_PROFILE" ]]; then
image_program="iterm2"
@ -1830,12 +1841,16 @@ get_image_backend() {
image_program="w3m"
fi
get_term_size
get_image_size
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 ;;
"off") ;;
esac
}
@ -1871,7 +1886,7 @@ get_ascii() {
# If the ascii file doesn't exist fallback to text mode.
if [[ ! -f "$script_dir/ascii/distro/${ascii/ *}" ]]; then
padding="\033[0C"
text_padding="0"
image="off"
err "Ascii: Ascii file not found, falling back to text mode."
return
@ -1906,7 +1921,7 @@ get_ascii() {
# Overwrite padding if ascii_length_force is set.
[[ "$ascii_length_force" ]] && ascii_length="$ascii_length_force"
padding="\033[$((ascii_length + gap))C"
text_padding="$((ascii_length + gap))"
printf "%b" "$print"
export LC_ALL=C
}
@ -1985,7 +2000,18 @@ get_wallpaper() {
[[ -z "$img" ]] && err "Image: Wallpaper detection failed, falling back to ascii mode."
}
make_thumbnail() {
get_term_size() {
# This functions gets the current window size in
# pixels.
#
# We first try to use the escape sequence '\044[14t'
# to get the terminal window size in pixels. If this
# fails we then fallback to using 'xdotool' or other
# programs.
# Tmux has a special way of reading escape sequences
# so we have to use a slightly different sequence to
# get the terminal size.
if [[ -n "$TMUX" ]]; then
printf "%b" "\033Ptmux;\033\033[14t\033\033[c\033\\"
read_flags="-d c"
@ -1998,10 +2024,12 @@ make_thumbnail() {
read_flags="-d c"
fi
# The escape code above prints the output AFTER the prompt so this
# The escape codes above print the desired output as
# user input so we have to use read to store the out
# -put as a variable.
builtin read -s -t 1 ${read_flags} -r term_size
# Split the string
# Split the string into height/width.
if [[ "$image_program" == "tycat" ]]; then
term_size=(${term_size//;/ })
term_width="$((term_size[2] * term_size[0]))"
@ -2017,46 +2045,49 @@ make_thumbnail() {
fi
# Get terminal width and height if \033[14t is unsupported.
if (("${#term_size}" <= 5)) && [[ "$image_program" != "tycat" ]]; then
if type -p xdotool >/dev/null 2>&1 && \
[[ "$image_backend" != "iterm2" ]]; then
if (("${#term_size}" <= 5)) && [[ "$image_program" == "w3m" ]]; then
if type -p xdotool >/dev/null 2>&1; then
current_window="$(xdotool getactivewindow)"
source <(xdotool getwindowgeometry --shell "$current_window")
term_height="$HEIGHT"
term_width="$WIDTH"
elif type -p xwininfo >/dev/null 2>&1 && \
type -p xdpyinfo >/dev/null 2>&1 || \
type -p xprop >/dev/null 2>&1 && \
[[ "$image_program" != "iterm2" ]]; then
elif type -p xwininfo >/dev/null 2>&1; then
# Get the focused window's ID.
if type -p xdpyinfo >/dev/null 2>&1; then
current_window="$(xdpyinfo | grep -F "focus" | grep -E -o "0x[0-9a-f]+")"
elif type -p xprop >/dev/null 2>&1; then
current_window="$(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}')"
fi
term_size="$(xwininfo -id "$current_window" | awk -F ': ' '/Width|Height/ {printf $2 " "}')"
term_width="${term_size/ *}"
term_height="${term_size/${term_width}}"
# If the ID was found get the window size.
if [[ "$current_window" ]]; then
term_size="$(xwininfo -id "$current_window" | awk -F ': ' '/Width|Height/ {printf $2 " "}')"
term_width="${term_size/ *}"
term_height="${term_size/${term_width}}"
else
term_width="0"
fi
else
term_width="0"
fi
fi
# If $img isn't a file fallback to ascii mode.
if [[ ! -f "$img" || -z "$term_width" ]] || (("$term_width" <= 10)); then
image="ascii"
get_ascii
# Error messages
[[ ! -f "$img" ]] && err "Image: \$img, isn't a file, falling back to ascii mode."
(("${#term_size}" <= 5)) && err "Image: Your terminal doesn't support \\\033[14t, falling back to ascii mode."
return
else
if [[ "$term_width" ]] && ((term_width > 0)); then
clear
zws=" "
else
image_backend="ascii"
get_ascii
err "Image: Failed to get window size, falling back to ascii mode."
return
fi
}
get_image_size() {
# This functions determines the size to make
# the thumbnail image.
# Get terminal lines and columns
term_blocks="$(stty size)"
@ -2067,7 +2098,6 @@ make_thumbnail() {
font_width="$((term_width / columns))"
font_height="$((term_height / lines))"
# Image size is half of the terminal
case "$image_size" in
"auto")
image_size="$((columns * font_width / 2))"
@ -2096,25 +2126,29 @@ make_thumbnail() {
*) image_size="${image_size/px}" ;;
esac
# Fallback if width / height are empty.
width="${width:-$image_size}"
height="${height:-$image_size}"
# Padding is half the terminal width + gap
padding="\033[$((width / font_width + gap + xoffset/font_width))C"
text_padding="$((width / font_width + gap + xoffset/font_width))"
}
# Make the directory if it doesn't exist
mkdir -p "$thumbnail_dir"
make_thumbnail() {
# Name the thumbnail using variables so we can
# use it later.
imgname="$crop_mode-$crop_offset-$width-$height"
# Check to see if the image has a file extension, if it doesn't
# then add one.
# Check to see if the image has a file extension,
# if it doesn't then add one.
case "${img##*/}" in
*"."*) imgname="$crop_mode-$crop_offset-$width-$height-${img##*/}" ;;
*) imgname="$crop_mode-$crop_offset-$width-$height-${img##*/}.jpg" ;;
*"."*) imgname="${imgname}-${img##*/}" ;;
*) imgname="${imgname}-${img##*/}.jpg" ;;
esac
# Check to see if the thumbnail exists before we do any cropping.
if [[ ! -f "$thumbnail_dir/$imgname" ]]; then
# Create the thumbnail dir if it doesn't exist.
mkdir -p "$thumbnail_dir"
# Get image size so that we can do a better crop
if [[ -z "$size" ]]; then
size="$(identify -format "%w %h" "$img")"
@ -2128,7 +2162,7 @@ make_thumbnail() {
fi
case "$crop_mode" in
fit)
"fit")
c="$(convert "$img" \
-colorspace srgb \
-format "%[pixel:p{0,0}]" info:)"
@ -2143,7 +2177,7 @@ make_thumbnail() {
"$thumbnail_dir/$imgname"
;;
fill)
"fill")
convert \
"$img" \
-trim +repage \
@ -2152,7 +2186,7 @@ make_thumbnail() {
"$thumbnail_dir/$imgname"
;;
none) cp "$img" "$thumbnail_dir/$imgname" ;;
"none") cp "$img" "$thumbnail_dir/$imgname" ;;
*)
convert \
"$img" \
@ -2176,7 +2210,7 @@ display_image() {
# appearing in specific terminal emulators.
sleep 0.05
printf "%b\n" "0;1;$xoffset;$yoffset;$width;$height;;;;;$img\n4;\n3;" |\
"$w3m_img_path" -bg "$background_color" >/dev/null & 2>&1 || padding="\033[0C"
"$w3m_img_path" -bg "$background_color" >/dev/null & 2>&1 || text_padding="0"
;;
"iterm2")
@ -2273,7 +2307,7 @@ info() {
[[ -z "$2" ]] && string="${string/*: }"
# Print the string
printf "%b\n" "${padding}${zws}${string}${reset} "
printf "%b\n" "\033[${text_padding}C${zws}${string}${reset} "
# Calculate info height
info_height="$((info_height+=1))"
@ -2281,10 +2315,8 @@ info() {
# Fix rendering issues with w3m and lines that
# wrap to the next line by adding a max line
# length.
if [[ "$image" != "off" && "$image" != "ascii" && "$1" != "cols" ]]; then
padding_num="${padding/\\033\[}"
output="$(printf "%.$((columns - ${padding_num/C} - gap - ${#subtitle}))s" "$output")"
fi
[[ "$image_backend" == "image" ]] && \
string="$(printf "%.$((columns - text_padding - gap))s" "$string")"
}
prin() {
@ -2301,7 +2333,7 @@ prin() {
string="$(trim "$string")"
# Print the info
printf "%b\n" "${padding}${zws}${string}${reset} "
printf "%b\n" "\033[${text_padding}C${zws}${string}${reset} "
# Calculate info height
info_height="$((info_height+=1))"
@ -2309,10 +2341,8 @@ prin() {
# Fix rendering issues with w3m and lines that
# wrap to the next line by adding a max line
# length.
if [[ "$image" != "off" && "$image" != "ascii" ]]; then
padding_num="${padding/\\033\[}"
string="$(printf "%.$((columns - ${padding_num/C} - gap))s" "$string")"
fi
[[ "$image_backend" == "image" ]] && \
string="$(printf "%.$((columns - text_padding - gap))s" "$string")"
# Tell info() that prin() was used.
prin=1
@ -3137,7 +3167,7 @@ main() {
# w3m-img: Draw the image a second time to fix
# rendering issues in specific terminal emulators.
[[ "$image_program" == "w3m" ]] && display_image
[[ "$image_backend" == "image" && "$image_program" == "w3m" ]] && display_image
fi
# Re-enable line wrap