Merge pull request #436 from dylanaraps/image
Images: Rewrite image functions
This commit is contained in:
commit
89c43b1f07
|
@ -287,8 +287,8 @@ disk_display="off"
|
|||
|
||||
|
||||
# Image Source
|
||||
# --image wall, ascii, /path/to/img, /path/to/dir/, off
|
||||
image="wall"
|
||||
# --image wallpaper, /path/to/img, /path/to/dir/, off
|
||||
image_source="wallpaper"
|
||||
|
||||
# Thumbnail directory
|
||||
thumbnail_dir="$HOME/.cache/thumbnails/neofetch"
|
||||
|
|
677
neofetch
677
neofetch
|
@ -1464,7 +1464,7 @@ get_term_font() {
|
|||
|
||||
case "$term" in
|
||||
"urxvt" | "urxvtd" | "xterm")
|
||||
term_font="$(grep -i -F "${term/d}*font" <<< "$(xrdb -query)")"
|
||||
term_font="$(grep -i -F "${term/d}*font" < <(xrdb -query))"
|
||||
term_font="${term_font/*font:}"
|
||||
term_font="$(trim "$term_font")"
|
||||
|
||||
|
@ -1791,69 +1791,81 @@ 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_wallpaper() {
|
||||
case "$os" in
|
||||
"Linux" | "BSD")
|
||||
if type -p feh >/dev/null && [[ -f "$HOME/.fehbg" ]]; then
|
||||
img="$(awk -F\' '/feh/ {printf $2}' "$HOME/.fehbg")"
|
||||
get_image_backend() {
|
||||
# This function determines which image backend to use
|
||||
# by checking for programs and etc.
|
||||
|
||||
elif type -p nitrogen >/dev/null; then
|
||||
img="$(awk -F'=' '/file/ {printf $2;exit;}' "$XDG_CONFIG_HOME/nitrogen/bg-saved.cfg")"
|
||||
# Automatically find w3m-img
|
||||
get_w3m_img_path
|
||||
|
||||
elif type -p gsettings >/dev/null; then
|
||||
# Get DE if user has disabled the function.
|
||||
[[ -z "$de" ]] && get_de
|
||||
# Fallback to ascii mode if imagemagick isn't installed.
|
||||
type -p convert >/dev/null 2>&1 || image_backend="ascii"
|
||||
|
||||
case "$de" in
|
||||
"MATE"*) img="$(gsettings get org.mate.background picture-filename)" ;;
|
||||
*) img="$(gsettings get org.gnome.desktop.background picture-uri)" ;;
|
||||
esac
|
||||
|
||||
# Strip quotes etc from the path.
|
||||
img="${img/'file://'}"
|
||||
img="${img//\'}"
|
||||
img="${img//\%20/ }"
|
||||
fi
|
||||
;;
|
||||
|
||||
"Mac OS X")
|
||||
img="$(osascript -e 'tell application "System Events" to picture of current desktop')"
|
||||
;;
|
||||
|
||||
"Windows")
|
||||
case "$distro" in
|
||||
"Windows XP")
|
||||
img="/cygdrive/c/Documents and Settings/${USER}"
|
||||
img+="/Local Settings/Application Data/Microsoft"
|
||||
img+="/Wallpaper1.bmp"
|
||||
;;
|
||||
|
||||
"Windows"*)
|
||||
img="$APPDATA/Microsoft/Windows/Themes"
|
||||
img+="/TranscodedWallpaper.jpg"
|
||||
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})
|
||||
image="$(printf "%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."
|
||||
return
|
||||
fi
|
||||
|
||||
get_image_program
|
||||
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"
|
||||
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
|
||||
;;
|
||||
|
||||
"ascii") get_ascii 2>/dev/null ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# If img is an xml file don't use it.
|
||||
[[ "${img/*\./}" == "xml" ]] && img=""
|
||||
get_image_program() {
|
||||
if [[ -n "$ITERM_PROFILE" ]]; then
|
||||
image_program="iterm2"
|
||||
|
||||
# Error msg
|
||||
[[ -z "$img" ]] && err "Image: Wallpaper detection failed, falling back to ascii mode."
|
||||
elif [[ "$(tycat 2>/dev/null)" ]]; then
|
||||
image_program="tycat"
|
||||
|
||||
else
|
||||
image_program="w3m"
|
||||
fi
|
||||
}
|
||||
|
||||
get_ascii() {
|
||||
if [[ ! -f "$ascii" || "$ascii" == "distro" ]]; then
|
||||
# Error message
|
||||
[[ "$ascii" != "distro" ]] && \
|
||||
[[ ! -f "$ascii" ]] && err "Ascii: Ascii file not found, using distro ascii"
|
||||
[[ "$ascii" != "distro" ]] && [[ ! -f "$ascii" ]] && \
|
||||
err "Ascii: Ascii file not found, using distro ascii."
|
||||
|
||||
# Lowercase the distro name
|
||||
if (("$version" <= 3)); then
|
||||
|
@ -1880,14 +1892,13 @@ get_ascii() {
|
|||
get_script_dir 2>/dev/null
|
||||
|
||||
# If the ascii file doesn't exist fallback to text mode.
|
||||
if [[ ! -f "$script_dir/ascii/distro/${ascii/ *}" ]]; then
|
||||
padding="\033[0C"
|
||||
image="off"
|
||||
err "Ascii: Ascii file not found, falling back to text mode."
|
||||
if [[ -f "$script_dir/ascii/distro/${ascii/ *}" ]]; then
|
||||
ascii="$script_dir/ascii/distro/${ascii/ *}"
|
||||
|
||||
else
|
||||
to_off "Ascii: Ascii file not found, falling back to text mode."
|
||||
return
|
||||
fi
|
||||
|
||||
ascii="$script_dir/ascii/distro/${ascii/ *}"
|
||||
fi
|
||||
fi
|
||||
|
||||
|
@ -1916,211 +1927,11 @@ 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
|
||||
}
|
||||
|
||||
get_image() {
|
||||
# Fallback to ascii mode if imagemagick isn't installed.
|
||||
type -p convert >/dev/null 2>&1 || image="ascii"
|
||||
|
||||
case "$image" in
|
||||
"wall") get_wallpaper 2>/dev/null ;;
|
||||
"ascii") get_ascii; return ;;
|
||||
*)
|
||||
if [[ -d "$image" ]]; then
|
||||
files=("${image%/}"/*.{png,jpg,jpeg})
|
||||
img="$(printf "%s" "${files[RANDOM % (${#files[@]} - 1)]}")"
|
||||
else
|
||||
img="$image"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
if [[ -n "$TMUX" ]]; then
|
||||
printf "%b" "\033Ptmux;\033\033[14t\033\033[c\033\\"
|
||||
read_flags="-d c"
|
||||
|
||||
elif [[ "$image_backend" == "tycat" ]]; then
|
||||
printf "%b" "\033}qs\000"
|
||||
|
||||
else
|
||||
printf "%b" "\033[14t\033[c"
|
||||
read_flags="-d c"
|
||||
fi
|
||||
|
||||
# The escape code above prints the output AFTER the prompt so this
|
||||
builtin read -s -t 1 ${read_flags} -r term_size
|
||||
|
||||
# Split the string
|
||||
if [[ "$image_backend" == "tycat" ]]; then
|
||||
term_size=(${term_size//;/ })
|
||||
term_width="$((term_size[2] * term_size[0]))"
|
||||
term_height="$((term_size[3] * term_size[1]))"
|
||||
|
||||
else
|
||||
term_size="${term_size//'['}"
|
||||
term_size="${term_size/';'}"
|
||||
term_size="${term_size/$'\E4'}"
|
||||
term_size="${term_size/t*}"
|
||||
term_height="${term_size/';'*}"
|
||||
term_width="${term_size/*';'}"
|
||||
fi
|
||||
|
||||
# Get terminal width and height if \033[14t is unsupported.
|
||||
if (("${#term_size}" <= 5)) && [[ "$image_backend" != "tycat" ]]; then
|
||||
if type -p xdotool >/dev/null 2>&1 && \
|
||||
[[ "$image_backend" != "iterm2" ]]; 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_backend" != "iterm2" ]]; then
|
||||
|
||||
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}}"
|
||||
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
|
||||
clear
|
||||
zws=" "
|
||||
fi
|
||||
|
||||
# Get terminal lines and columns
|
||||
term_blocks="$(stty size)"
|
||||
columns="${term_blocks/* }"
|
||||
lines="${term_blocks/ *}"
|
||||
|
||||
# Calculate font size
|
||||
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))"
|
||||
term_height="$((term_height - term_height / 4))"
|
||||
|
||||
(("$term_height" < "$image_size")) && \
|
||||
image_size="$term_height"
|
||||
;;
|
||||
|
||||
*"%")
|
||||
percent="${image_size/\%}"
|
||||
image_size="$((percent * term_width / 100))"
|
||||
|
||||
(("$((percent * term_height / 50))" < "$image_size")) && \
|
||||
image_size="$((percent * term_height / 100))"
|
||||
;;
|
||||
|
||||
"none")
|
||||
# Get image size so that we can do a better crop
|
||||
size="$(identify -format "%w %h" "$img")"
|
||||
width="${size%% *}"
|
||||
height="${size##* }"
|
||||
crop_mode="none"
|
||||
;;
|
||||
|
||||
*) 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"
|
||||
|
||||
# Make the directory if it doesn't exist
|
||||
mkdir -p "$thumbnail_dir"
|
||||
|
||||
# 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" ;;
|
||||
esac
|
||||
|
||||
# Check to see if the thumbnail exists before we do any cropping.
|
||||
if [[ ! -f "$thumbnail_dir/$imgname" ]]; then
|
||||
# Get image size so that we can do a better crop
|
||||
if [[ -z "$size" ]]; then
|
||||
size="$(identify -format "%w %h" "$img")"
|
||||
og_width="${size%% *}"
|
||||
og_height="${size##* }"
|
||||
|
||||
# This checks to see if height is geater than width
|
||||
# so we can do a better crop of portrait images.
|
||||
size="$og_height"
|
||||
(("$og_height" > "$og_width")) && size="$og_width"
|
||||
fi
|
||||
|
||||
case "$crop_mode" in
|
||||
fit)
|
||||
c="$(convert "$img" \
|
||||
-colorspace srgb \
|
||||
-format "%[pixel:p{0,0}]" info:)"
|
||||
|
||||
convert \
|
||||
"$img" \
|
||||
-trim +repage \
|
||||
-gravity south \
|
||||
-background "$c" \
|
||||
-extent "$size"x"$size" \
|
||||
-scale "$width"x"$height" \
|
||||
"$thumbnail_dir/$imgname"
|
||||
;;
|
||||
|
||||
fill)
|
||||
convert \
|
||||
"$img" \
|
||||
-trim +repage \
|
||||
-scale "$width"x"$height"^ \
|
||||
-extent "$width"x"$height" \
|
||||
"$thumbnail_dir/$imgname"
|
||||
;;
|
||||
|
||||
none) cp "$img" "$thumbnail_dir/$imgname" ;;
|
||||
*)
|
||||
convert \
|
||||
"$img" \
|
||||
-gravity "$crop_offset" \
|
||||
-crop "$size"x"$size"+0+0 \
|
||||
-quality 95 \
|
||||
-scale "$width"x"$height" \
|
||||
"$thumbnail_dir/$imgname"
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# The final image
|
||||
img="$thumbnail_dir/$imgname"
|
||||
}
|
||||
|
||||
get_w3m_img_path() {
|
||||
if [[ -x "$w3m_img_path" ]]; then
|
||||
return
|
||||
|
@ -2138,43 +1949,290 @@ get_w3m_img_path() {
|
|||
w3m_img_path="/usr/libexec64/w3m/w3mimgdisplay"
|
||||
|
||||
else
|
||||
image="ascii"
|
||||
image_backend="ascii"
|
||||
err "Image: w3m-img wasn't found on your system, falling back to ascii mode."
|
||||
fi
|
||||
}
|
||||
|
||||
get_image_backend() {
|
||||
if [[ -n "$ITERM_PROFILE" ]]; then
|
||||
image_backend="iterm2"
|
||||
get_wallpaper() {
|
||||
case "$os" in
|
||||
"Linux" | "BSD")
|
||||
if type -p feh >/dev/null && [[ -f "$HOME/.fehbg" ]]; then
|
||||
image="$(awk -F\' '/feh/ {printf $2}' "$HOME/.fehbg")"
|
||||
|
||||
elif [[ "$(tycat 2>/dev/null)" ]]; then
|
||||
image_backend="tycat"
|
||||
elif type -p nitrogen >/dev/null; then
|
||||
image="$(awk -F'=' '/file/ {printf $2;exit;}' "$XDG_CONFIG_HOME/nitrogen/bg-saved.cfg")"
|
||||
|
||||
elif type -p gsettings >/dev/null; then
|
||||
# Get DE if user has disabled the function.
|
||||
[[ -z "$de" ]] && get_de
|
||||
|
||||
case "$de" in
|
||||
"MATE"*) image="$(gsettings get org.mate.background picture-filename)" ;;
|
||||
*) image="$(gsettings get org.gnome.desktop.background picture-uri)" ;;
|
||||
esac
|
||||
|
||||
# Strip quotes etc from the path.
|
||||
image="${image/'file://'}"
|
||||
image="${image//\'}"
|
||||
image="${image//\%20/ }"
|
||||
fi
|
||||
;;
|
||||
|
||||
"Mac OS X")
|
||||
image="$(osascript -e 'tell application "System Events" to picture of current desktop')"
|
||||
;;
|
||||
|
||||
"Windows")
|
||||
case "$distro" in
|
||||
"Windows XP")
|
||||
image="/cygdrive/c/Documents and Settings/${USER}"
|
||||
image+="/Local Settings/Application Data/Microsoft"
|
||||
image+="/Wallpaper1.bmp"
|
||||
;;
|
||||
|
||||
"Windows"*)
|
||||
image="$APPDATA/Microsoft/Windows/Themes"
|
||||
image+="/TranscodedWallpaper.jpg"
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
|
||||
# If image is an xml file, don't use it.
|
||||
[[ "${image/*\./}" == "xml" ]] && image=""
|
||||
}
|
||||
|
||||
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"
|
||||
|
||||
elif [[ "$image_program" == "tycat" ]]; then
|
||||
printf "%b" "\033}qs\000"
|
||||
|
||||
else
|
||||
image_backend="w3m"
|
||||
printf "%b" "\033[14t\033[c"
|
||||
read_flags="-d c"
|
||||
fi
|
||||
|
||||
# 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 into height/width.
|
||||
if [[ "$image_program" == "tycat" ]]; then
|
||||
term_size=(${term_size//;/ })
|
||||
term_width="$((term_size[2] * term_size[0]))"
|
||||
term_height="$((term_size[3] * term_size[1]))"
|
||||
|
||||
else
|
||||
term_size="${term_size//'['}"
|
||||
term_size="${term_size/';'}"
|
||||
term_size="${term_size/$'\E4'}"
|
||||
term_size="${term_size/t*}"
|
||||
term_height="${term_size/';'*}"
|
||||
term_width="${term_size/*';'}"
|
||||
fi
|
||||
|
||||
# Get terminal width and height if \033[14t is unsupported.
|
||||
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; 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
|
||||
|
||||
# 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 the terminal size was found correctly
|
||||
if [[ "$term_width" ]] && ((term_width > 0)); then
|
||||
clear
|
||||
zws=" "
|
||||
fi
|
||||
}
|
||||
|
||||
display_image() {
|
||||
if [[ "$image" != "ascii" ]]; then
|
||||
case "$image_backend" 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;;;;;$img\n4;\n3;" |\
|
||||
"$w3m_img_path" -bg "$background_color" >/dev/null & 2>&1 || padding="\033[0C"
|
||||
get_image_size() {
|
||||
# This functions determines the size to make
|
||||
# the thumbnail image.
|
||||
|
||||
# Get terminal lines and columns
|
||||
term_blocks="$(stty size)"
|
||||
columns="${term_blocks/* }"
|
||||
lines="${term_blocks/ *}"
|
||||
|
||||
# Calculate font size
|
||||
font_width="$((term_width / columns))"
|
||||
font_height="$((term_height / lines))"
|
||||
|
||||
case "$image_size" in
|
||||
"auto")
|
||||
image_size="$((columns * font_width / 2))"
|
||||
term_height="$((term_height - term_height / 4))"
|
||||
|
||||
(("$term_height" < "$image_size")) && \
|
||||
image_size="$term_height"
|
||||
;;
|
||||
|
||||
*"%")
|
||||
percent="${image_size/\%}"
|
||||
image_size="$((percent * term_width / 100))"
|
||||
|
||||
(("$((percent * term_height / 50))" < "$image_size")) && \
|
||||
image_size="$((percent * term_height / 100))"
|
||||
;;
|
||||
|
||||
"none")
|
||||
# Get image size so that we can do a better crop
|
||||
size="$(identify -format "%w %h" "$image")"
|
||||
width="${size%% *}"
|
||||
height="${size##* }"
|
||||
crop_mode="none"
|
||||
;;
|
||||
|
||||
*) image_size="${image_size/px}" ;;
|
||||
esac
|
||||
|
||||
width="${width:-$image_size}"
|
||||
height="${height:-$image_size}"
|
||||
|
||||
text_padding="$((width / font_width + gap + xoffset/font_width))"
|
||||
}
|
||||
|
||||
make_thumbnail() {
|
||||
# Name the thumbnail using variables so we can
|
||||
# use it later.
|
||||
image_name="$crop_mode-$crop_offset-$width-$height"
|
||||
|
||||
# Check to see if the image has a file extension,
|
||||
# if it doesn't then add one.
|
||||
case "${image##*/}" in
|
||||
*"."*) image_name="${image_name}-${image##*/}" ;;
|
||||
*) image_name="${image_name}-${image##*/}.jpg" ;;
|
||||
esac
|
||||
|
||||
# Check to see if the thumbnail exists before we do any cropping.
|
||||
if [[ ! -f "$thumbnail_dir/$image_name" ]]; 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" "$image")"
|
||||
og_width="${size%% *}"
|
||||
og_height="${size##* }"
|
||||
|
||||
# This checks to see if height is geater than width
|
||||
# so we can do a better crop of portrait images.
|
||||
size="$og_height"
|
||||
(("$og_height" > "$og_width")) && size="$og_width"
|
||||
fi
|
||||
|
||||
case "$crop_mode" in
|
||||
"fit")
|
||||
c="$(convert "$image" \
|
||||
-colorspace srgb \
|
||||
-format "%[pixel:p{0,0}]" info:)"
|
||||
|
||||
convert \
|
||||
"$image" \
|
||||
-trim +repage \
|
||||
-gravity south \
|
||||
-background "$c" \
|
||||
-extent "$size"x"$size" \
|
||||
-scale "$width"x"$height" \
|
||||
"$thumbnail_dir/$image_name"
|
||||
;;
|
||||
|
||||
"iterm2")
|
||||
printf "%b\a\n" "\033]1337;File=width=${width}px;height=${height}px;inline=1:$(base64 < "$img")"
|
||||
"fill")
|
||||
convert \
|
||||
"$image" \
|
||||
-trim +repage \
|
||||
-scale "$width"x"$height"^ \
|
||||
-extent "$width"x"$height" \
|
||||
"$thumbnail_dir/$image_name"
|
||||
;;
|
||||
|
||||
"tycat")
|
||||
tycat "$img"
|
||||
"none") cp "$image" "$thumbnail_dir/$image_name" ;;
|
||||
*)
|
||||
convert \
|
||||
"$image" \
|
||||
-gravity "$crop_offset" \
|
||||
-crop "$size"x"$size"+0+0 \
|
||||
-quality 95 \
|
||||
-scale "$width"x"$height" \
|
||||
"$thumbnail_dir/$image_name"
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# The final image
|
||||
image="$thumbnail_dir/$image_name"
|
||||
}
|
||||
|
||||
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."
|
||||
;;
|
||||
|
||||
"iterm2")
|
||||
printf "%b\a\n" "\033]1337;File=width=${width}px;height=${height}px;inline=1:$(base64 < "$image")"
|
||||
;;
|
||||
|
||||
"tycat")
|
||||
tycat "$image"
|
||||
;;
|
||||
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
|
||||
|
@ -2239,7 +2297,7 @@ info() {
|
|||
|
||||
# If the output is empty, don't print anything.
|
||||
[[ -z "${output// }" ]] && \
|
||||
err "Info: Couldn't detect $subtitle" && return
|
||||
err "Info: Couldn't detect $subtitle." && return
|
||||
|
||||
case "$1" in
|
||||
title)
|
||||
|
@ -2261,7 +2319,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))"
|
||||
|
@ -2269,10 +2327,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() {
|
||||
|
@ -2289,7 +2345,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))"
|
||||
|
@ -2297,10 +2353,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
|
||||
|
@ -2593,13 +2647,6 @@ err() {
|
|||
"
|
||||
}
|
||||
|
||||
check_old_flags() {
|
||||
[[ -n "$osx_buildversion" ]] && err "Config: \$osx_buildversion is deprecated, use \$distro_shorthand instead."
|
||||
[[ -n "$osx_codename" ]] && err "Config: \$osx_codename is deprecated, use \$distro_shorthand instead."
|
||||
[[ -n "$progress_char" ]] && err "Config: \$progress_char is deprecated, use \$progress_char_elapsed and \$progress_char_total instead."
|
||||
[[ "$cpu_cores" == "on" ]] && err "Config: \$cpu_cores='on' is deprecated, use \$cpu_cores='logical|physical|off' instead."
|
||||
}
|
||||
|
||||
get_script_dir() {
|
||||
[[ "$script_dir" ]] && return
|
||||
|
||||
|
@ -2635,7 +2682,7 @@ get_default_config() {
|
|||
fi
|
||||
|
||||
if source "$default_config"; then
|
||||
err "Config: Sourced default config ($default_config)"
|
||||
err "Config: Sourced default config. ($default_config)"
|
||||
else
|
||||
err "Config: Default config not found, continuing..."
|
||||
fi
|
||||
|
@ -2645,12 +2692,12 @@ get_user_config() {
|
|||
# Check $config_file
|
||||
if [[ -f "$config_file" ]]; then
|
||||
source "$config_file"
|
||||
err "Config: Sourced user config ($config_file)"
|
||||
err "Config: Sourced user config. ($config_file)"
|
||||
return
|
||||
|
||||
elif [[ "$config_file" == "travis" ]]; then
|
||||
source "$travis_config"
|
||||
err "Config: Sourced user config ($travis_config)"
|
||||
err "Config: Sourced user config. ($travis_config)"
|
||||
return
|
||||
fi
|
||||
mkdir -p "$XDG_CONFIG_HOME/neofetch/"
|
||||
|
@ -2676,7 +2723,7 @@ get_user_config() {
|
|||
fi
|
||||
|
||||
source "$config_file"
|
||||
err "Config: Sourced user config ($config_file)"
|
||||
err "Config: Sourced user config. ($config_file)"
|
||||
}
|
||||
|
||||
bar() {
|
||||
|
@ -2746,7 +2793,7 @@ get_term_padding() {
|
|||
|
||||
dynamic_prompt() {
|
||||
# Calculate image height in terminal cells.
|
||||
if [[ "$image" != "ascii" ]]; then
|
||||
if [[ "$image_backend" != "ascii" ]]; then
|
||||
get_term_padding 2>/dev/null
|
||||
lines="$(((height + (${border:-0} * 2) + ${yoffset:-0}) / font_height))"
|
||||
fi
|
||||
|
@ -2786,6 +2833,14 @@ old_functions() {
|
|||
fi
|
||||
}
|
||||
|
||||
old_flags() {
|
||||
[[ -n "$osx_buildversion" ]] && err "Config: \$osx_buildversion is deprecated, use \$distro_shorthand instead."
|
||||
[[ -n "$osx_codename" ]] && err "Config: \$osx_codename is deprecated, use \$distro_shorthand instead."
|
||||
[[ -n "$progress_char" ]] && err "Config: \$progress_char is deprecated, use \$progress_char_elapsed and \$progress_char_total instead."
|
||||
[[ "$cpu_cores" == "on" ]] && err "Config: \$cpu_cores='on' is deprecated, use \$cpu_cores='logical|physical|off' instead."
|
||||
[[ -n "$image" ]] && { err "Config: \$image is deprecated, use \$image_source instead."; image_source="$image"; }
|
||||
}
|
||||
|
||||
cache_uname() {
|
||||
# Cache the output of uname so we don't
|
||||
# have to spawn it multiple times.
|
||||
|
@ -3011,8 +3066,8 @@ get_args() {
|
|||
|
||||
# Image
|
||||
--image)
|
||||
image="$2"
|
||||
case "$2" in "-"* | "") image="ascii" ;; esac
|
||||
image_source="$2"
|
||||
case "$2" in "-"* | "") image_backend="ascii" ;; esac
|
||||
;;
|
||||
|
||||
--image_size | --size) image_size="$2" ;;
|
||||
|
@ -3031,7 +3086,7 @@ get_args() {
|
|||
|
||||
# Ascii
|
||||
--ascii)
|
||||
image="ascii"
|
||||
image_backend="ascii"
|
||||
ascii="$2"
|
||||
case "$2" in "-"* | "") ascii="distro" ;; esac
|
||||
;;
|
||||
|
@ -3048,7 +3103,7 @@ get_args() {
|
|||
;;
|
||||
|
||||
--ascii_distro)
|
||||
image="ascii"
|
||||
image_backend="ascii"
|
||||
ascii_distro="$2"
|
||||
case "$2" in "-"* | "") ascii_distro="$distro" ;; esac
|
||||
;;
|
||||
|
@ -3056,7 +3111,7 @@ get_args() {
|
|||
--ascii_logo_size) ascii_logo_size="$2" ;;
|
||||
--ascii_bold) ascii_bold="$2" ;;
|
||||
--logo | -L)
|
||||
image="ascii"
|
||||
image_backend="ascii"
|
||||
print_info() { info line_break; }
|
||||
;;
|
||||
|
||||
|
@ -3094,7 +3149,7 @@ main() {
|
|||
cache_uname
|
||||
get_os
|
||||
get_default_config 2>/dev/null
|
||||
check_old_flags
|
||||
old_flags
|
||||
get_args "$@"
|
||||
get_distro
|
||||
bold
|
||||
|
@ -3109,21 +3164,9 @@ main() {
|
|||
# Hide the cursor and disable line wrap
|
||||
printf "\033[?25l\033[?7l"
|
||||
|
||||
# Display the image
|
||||
if [[ "$image" != "off" ]]; then
|
||||
get_image_backend
|
||||
|
||||
# Find w3mimgdisplay
|
||||
[[ "$image_backend" == "w3m" ]] && \
|
||||
[[ "$image" != "ascii" ]] && \
|
||||
get_w3m_img_path
|
||||
|
||||
# Get the image src
|
||||
get_image
|
||||
|
||||
# Display the image if enabled
|
||||
display_image
|
||||
get_image_backend
|
||||
|
||||
if [[ "$image_backend" != "off" ]]; then
|
||||
# Set cursor position next to ascii art
|
||||
printf "%b" "\033[$((${lines:-0} - ${prompt_loc:-0}))A"
|
||||
|
||||
|
@ -3136,12 +3179,12 @@ main() {
|
|||
print_info 2>/dev/null
|
||||
|
||||
# Prompt calculation
|
||||
if [[ "$image" != "off" ]]; then
|
||||
if [[ "$image_backend" != "off" ]]; then
|
||||
dynamic_prompt
|
||||
|
||||
# w3m-img: Draw the image a second time to fix
|
||||
# rendering issues in specific terminal emulators.
|
||||
[[ "$image_backend" == "w3m" ]] && display_image
|
||||
[[ "$image_backend" == "image" && "$image_program" == "w3m" ]] && display_image
|
||||
fi
|
||||
|
||||
# Re-enable line wrap
|
||||
|
|
Reference in New Issue