Images; Rewrite functions
This commit is contained in:
parent
5ccbb39fbf
commit
a1b79bd33b
1 changed files with 87 additions and 57 deletions
138
neofetch
138
neofetch
|
@ -1791,13 +1791,17 @@ get_cols() {
|
||||||
|
|
||||||
# Add newlines to the string.
|
# Add newlines to the string.
|
||||||
cols="${cols%%'nl'}"
|
cols="${cols%%'nl'}"
|
||||||
cols="${cols//nl/\\n${padding}${zws}}"
|
cols="${cols//nl/\\n\\033[${text_padding}C${zws}}"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# IMAGES
|
# IMAGES
|
||||||
|
|
||||||
get_image_backend() {
|
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
|
get_w3m_img_path
|
||||||
|
|
||||||
# Fallback to ascii mode if imagemagick isn't installed.
|
# Fallback to ascii mode if imagemagick isn't installed.
|
||||||
|
@ -1815,11 +1819,18 @@ get_image_backend() {
|
||||||
else
|
else
|
||||||
img="$image_source"
|
img="$image_source"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
[ ! -f "$img" ] && { image_backend="ascii"; get_ascii 2>/dev/null; return; }
|
|
||||||
;;
|
;;
|
||||||
esac
|
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
|
if [[ -n "$ITERM_PROFILE" ]]; then
|
||||||
image_program="iterm2"
|
image_program="iterm2"
|
||||||
|
|
||||||
|
@ -1830,12 +1841,16 @@ get_image_backend() {
|
||||||
image_program="w3m"
|
image_program="w3m"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
get_term_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 ;;
|
"ascii") get_ascii 2>/dev/null ;;
|
||||||
"off") ;;
|
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1871,7 +1886,7 @@ get_ascii() {
|
||||||
|
|
||||||
# If the ascii file doesn't exist fallback to text mode.
|
# If the ascii file doesn't exist fallback to text mode.
|
||||||
if [[ ! -f "$script_dir/ascii/distro/${ascii/ *}" ]]; then
|
if [[ ! -f "$script_dir/ascii/distro/${ascii/ *}" ]]; then
|
||||||
padding="\033[0C"
|
text_padding="0"
|
||||||
image="off"
|
image="off"
|
||||||
err "Ascii: Ascii file not found, falling back to text mode."
|
err "Ascii: Ascii file not found, falling back to text mode."
|
||||||
return
|
return
|
||||||
|
@ -1906,7 +1921,7 @@ get_ascii() {
|
||||||
# Overwrite padding if ascii_length_force is set.
|
# Overwrite padding if ascii_length_force is set.
|
||||||
[[ "$ascii_length_force" ]] && ascii_length="$ascii_length_force"
|
[[ "$ascii_length_force" ]] && ascii_length="$ascii_length_force"
|
||||||
|
|
||||||
padding="\033[$((ascii_length + gap))C"
|
text_padding="$((ascii_length + gap))"
|
||||||
printf "%b" "$print"
|
printf "%b" "$print"
|
||||||
export LC_ALL=C
|
export LC_ALL=C
|
||||||
}
|
}
|
||||||
|
@ -1985,7 +2000,18 @@ get_wallpaper() {
|
||||||
[[ -z "$img" ]] && err "Image: Wallpaper detection failed, falling back to ascii mode."
|
[[ -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
|
if [[ -n "$TMUX" ]]; then
|
||||||
printf "%b" "\033Ptmux;\033\033[14t\033\033[c\033\\"
|
printf "%b" "\033Ptmux;\033\033[14t\033\033[c\033\\"
|
||||||
read_flags="-d c"
|
read_flags="-d c"
|
||||||
|
@ -1998,10 +2024,12 @@ make_thumbnail() {
|
||||||
read_flags="-d c"
|
read_flags="-d c"
|
||||||
fi
|
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
|
builtin read -s -t 1 ${read_flags} -r term_size
|
||||||
|
|
||||||
# Split the string
|
# Split the string into height/width.
|
||||||
if [[ "$image_program" == "tycat" ]]; then
|
if [[ "$image_program" == "tycat" ]]; then
|
||||||
term_size=(${term_size//;/ })
|
term_size=(${term_size//;/ })
|
||||||
term_width="$((term_size[2] * term_size[0]))"
|
term_width="$((term_size[2] * term_size[0]))"
|
||||||
|
@ -2017,46 +2045,49 @@ make_thumbnail() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get terminal width and height if \033[14t is unsupported.
|
# Get terminal width and height if \033[14t is unsupported.
|
||||||
if (("${#term_size}" <= 5)) && [[ "$image_program" != "tycat" ]]; then
|
if (("${#term_size}" <= 5)) && [[ "$image_program" == "w3m" ]]; then
|
||||||
if type -p xdotool >/dev/null 2>&1 && \
|
if type -p xdotool >/dev/null 2>&1; then
|
||||||
[[ "$image_backend" != "iterm2" ]]; then
|
|
||||||
|
|
||||||
current_window="$(xdotool getactivewindow)"
|
current_window="$(xdotool getactivewindow)"
|
||||||
source <(xdotool getwindowgeometry --shell "$current_window")
|
source <(xdotool getwindowgeometry --shell "$current_window")
|
||||||
term_height="$HEIGHT"
|
term_height="$HEIGHT"
|
||||||
term_width="$WIDTH"
|
term_width="$WIDTH"
|
||||||
|
|
||||||
elif type -p xwininfo >/dev/null 2>&1 && \
|
elif type -p xwininfo >/dev/null 2>&1; then
|
||||||
type -p xdpyinfo >/dev/null 2>&1 || \
|
# Get the focused window's ID.
|
||||||
type -p xprop >/dev/null 2>&1 && \
|
|
||||||
[[ "$image_program" != "iterm2" ]]; then
|
|
||||||
|
|
||||||
if type -p xdpyinfo >/dev/null 2>&1; then
|
if type -p xdpyinfo >/dev/null 2>&1; then
|
||||||
current_window="$(xdpyinfo | grep -F "focus" | grep -E -o "0x[0-9a-f]+")"
|
current_window="$(xdpyinfo | grep -F "focus" | grep -E -o "0x[0-9a-f]+")"
|
||||||
elif type -p xprop >/dev/null 2>&1; then
|
elif type -p xprop >/dev/null 2>&1; then
|
||||||
current_window="$(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}')"
|
current_window="$(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}')"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# 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_size="$(xwininfo -id "$current_window" | awk -F ': ' '/Width|Height/ {printf $2 " "}')"
|
||||||
term_width="${term_size/ *}"
|
term_width="${term_size/ *}"
|
||||||
term_height="${term_size/${term_width}}"
|
term_height="${term_size/${term_width}}"
|
||||||
|
else
|
||||||
|
term_width="0"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
term_width="0"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# If $img isn't a file fallback to ascii mode.
|
# If $img isn't a file fallback to ascii mode.
|
||||||
if [[ ! -f "$img" || -z "$term_width" ]] || (("$term_width" <= 10)); then
|
if [[ "$term_width" ]] && ((term_width > 0)); 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
|
|
||||||
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
|
||||||
|
}
|
||||||
|
|
||||||
|
get_image_size() {
|
||||||
|
# This functions determines the size to make
|
||||||
|
# the thumbnail image.
|
||||||
|
|
||||||
# Get terminal lines and columns
|
# Get terminal lines and columns
|
||||||
term_blocks="$(stty size)"
|
term_blocks="$(stty size)"
|
||||||
|
@ -2067,7 +2098,6 @@ make_thumbnail() {
|
||||||
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
|
|
||||||
case "$image_size" in
|
case "$image_size" in
|
||||||
"auto")
|
"auto")
|
||||||
image_size="$((columns * font_width / 2))"
|
image_size="$((columns * font_width / 2))"
|
||||||
|
@ -2096,25 +2126,29 @@ make_thumbnail() {
|
||||||
*) image_size="${image_size/px}" ;;
|
*) image_size="${image_size/px}" ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Fallback if width / height are empty.
|
|
||||||
width="${width:-$image_size}"
|
width="${width:-$image_size}"
|
||||||
height="${height:-$image_size}"
|
height="${height:-$image_size}"
|
||||||
|
|
||||||
# Padding is half the terminal width + gap
|
text_padding="$((width / font_width + gap + xoffset/font_width))"
|
||||||
padding="\033[$((width / font_width + gap + xoffset/font_width))C"
|
}
|
||||||
|
|
||||||
# Make the directory if it doesn't exist
|
make_thumbnail() {
|
||||||
mkdir -p "$thumbnail_dir"
|
# 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
|
# Check to see if the image has a file extension,
|
||||||
# then add one.
|
# if it doesn't then add one.
|
||||||
case "${img##*/}" in
|
case "${img##*/}" in
|
||||||
*"."*) imgname="$crop_mode-$crop_offset-$width-$height-${img##*/}" ;;
|
*"."*) imgname="${imgname}-${img##*/}" ;;
|
||||||
*) imgname="$crop_mode-$crop_offset-$width-$height-${img##*/}.jpg" ;;
|
*) imgname="${imgname}-${img##*/}.jpg" ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Check to see if the thumbnail exists before we do any cropping.
|
# Check to see if the thumbnail exists before we do any cropping.
|
||||||
if [[ ! -f "$thumbnail_dir/$imgname" ]]; then
|
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
|
# Get image size so that we can do a better crop
|
||||||
if [[ -z "$size" ]]; then
|
if [[ -z "$size" ]]; then
|
||||||
size="$(identify -format "%w %h" "$img")"
|
size="$(identify -format "%w %h" "$img")"
|
||||||
|
@ -2128,7 +2162,7 @@ make_thumbnail() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
case "$crop_mode" in
|
case "$crop_mode" in
|
||||||
fit)
|
"fit")
|
||||||
c="$(convert "$img" \
|
c="$(convert "$img" \
|
||||||
-colorspace srgb \
|
-colorspace srgb \
|
||||||
-format "%[pixel:p{0,0}]" info:)"
|
-format "%[pixel:p{0,0}]" info:)"
|
||||||
|
@ -2143,7 +2177,7 @@ make_thumbnail() {
|
||||||
"$thumbnail_dir/$imgname"
|
"$thumbnail_dir/$imgname"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
fill)
|
"fill")
|
||||||
convert \
|
convert \
|
||||||
"$img" \
|
"$img" \
|
||||||
-trim +repage \
|
-trim +repage \
|
||||||
|
@ -2152,7 +2186,7 @@ make_thumbnail() {
|
||||||
"$thumbnail_dir/$imgname"
|
"$thumbnail_dir/$imgname"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
none) cp "$img" "$thumbnail_dir/$imgname" ;;
|
"none") cp "$img" "$thumbnail_dir/$imgname" ;;
|
||||||
*)
|
*)
|
||||||
convert \
|
convert \
|
||||||
"$img" \
|
"$img" \
|
||||||
|
@ -2176,7 +2210,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;;;;;$img\n4;\n3;" |\
|
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")
|
"iterm2")
|
||||||
|
@ -2273,7 +2307,7 @@ info() {
|
||||||
[[ -z "$2" ]] && string="${string/*: }"
|
[[ -z "$2" ]] && string="${string/*: }"
|
||||||
|
|
||||||
# Print the string
|
# Print the string
|
||||||
printf "%b\n" "${padding}${zws}${string}${reset} "
|
printf "%b\n" "\033[${text_padding}C${zws}${string}${reset} "
|
||||||
|
|
||||||
# Calculate info height
|
# Calculate info height
|
||||||
info_height="$((info_height+=1))"
|
info_height="$((info_height+=1))"
|
||||||
|
@ -2281,10 +2315,8 @@ info() {
|
||||||
# Fix rendering issues with w3m and lines that
|
# Fix rendering issues with w3m and lines that
|
||||||
# wrap to the next line by adding a max line
|
# wrap to the next line by adding a max line
|
||||||
# length.
|
# length.
|
||||||
if [[ "$image" != "off" && "$image" != "ascii" && "$1" != "cols" ]]; then
|
[[ "$image_backend" == "image" ]] && \
|
||||||
padding_num="${padding/\\033\[}"
|
string="$(printf "%.$((columns - text_padding - gap))s" "$string")"
|
||||||
output="$(printf "%.$((columns - ${padding_num/C} - gap - ${#subtitle}))s" "$output")"
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prin() {
|
prin() {
|
||||||
|
@ -2301,7 +2333,7 @@ prin() {
|
||||||
string="$(trim "$string")"
|
string="$(trim "$string")"
|
||||||
|
|
||||||
# Print the info
|
# Print the info
|
||||||
printf "%b\n" "${padding}${zws}${string}${reset} "
|
printf "%b\n" "\033[${text_padding}C${zws}${string}${reset} "
|
||||||
|
|
||||||
# Calculate info height
|
# Calculate info height
|
||||||
info_height="$((info_height+=1))"
|
info_height="$((info_height+=1))"
|
||||||
|
@ -2309,10 +2341,8 @@ prin() {
|
||||||
# Fix rendering issues with w3m and lines that
|
# Fix rendering issues with w3m and lines that
|
||||||
# wrap to the next line by adding a max line
|
# wrap to the next line by adding a max line
|
||||||
# length.
|
# length.
|
||||||
if [[ "$image" != "off" && "$image" != "ascii" ]]; then
|
[[ "$image_backend" == "image" ]] && \
|
||||||
padding_num="${padding/\\033\[}"
|
string="$(printf "%.$((columns - text_padding - gap))s" "$string")"
|
||||||
string="$(printf "%.$((columns - ${padding_num/C} - gap))s" "$string")"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Tell info() that prin() was used.
|
# Tell info() that prin() was used.
|
||||||
prin=1
|
prin=1
|
||||||
|
@ -3137,7 +3167,7 @@ main() {
|
||||||
|
|
||||||
# w3m-img: Draw the image a second time to fix
|
# w3m-img: Draw the image a second time to fix
|
||||||
# rendering issues in specific terminal emulators.
|
# rendering issues in specific terminal emulators.
|
||||||
[[ "$image_program" == "w3m" ]] && display_image
|
[[ "$image_backend" == "image" && "$image_program" == "w3m" ]] && display_image
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Re-enable line wrap
|
# Re-enable line wrap
|
||||||
|
|
Loading…
Reference in a new issue