Images: Rewrite functions

This commit is contained in:
Dylan Araps 2016-11-13 00:30:09 +11:00
parent 7d1d49a1a7
commit 5ccbb39fbf
2 changed files with 132 additions and 132 deletions

View file

@ -287,8 +287,8 @@ disk_display="off"
# Image Source # Image Source
# --image wall, ascii, /path/to/img, /path/to/dir/, off # --image wallpaper, /path/to/img, /path/to/dir/, off
image="wall" image_source="wallpaper"
# Thumbnail directory # Thumbnail directory
thumbnail_dir="$HOME/.cache/thumbnails/neofetch" thumbnail_dir="$HOME/.cache/thumbnails/neofetch"

226
neofetch
View file

@ -1797,56 +1797,46 @@ get_cols() {
# IMAGES # IMAGES
get_wallpaper() { get_image_backend() {
case "$os" in get_w3m_img_path
"Linux" | "BSD")
if type -p feh >/dev/null && [[ -f "$HOME/.fehbg" ]]; then
img="$(awk -F\' '/feh/ {printf $2}' "$HOME/.fehbg")"
elif type -p nitrogen >/dev/null; then # Fallback to ascii mode if imagemagick isn't installed.
img="$(awk -F'=' '/file/ {printf $2;exit;}' "$XDG_CONFIG_HOME/nitrogen/bg-saved.cfg")" type -p convert >/dev/null 2>&1 || image_backend="ascii"
elif type -p gsettings >/dev/null; then case "${image_backend:=image}" in
# Get DE if user has disabled the function. "image")
[[ -z "$de" ]] && get_de case "$image_source" in
"wall"*) get_wallpaper 2>/dev/null ;;
case "$de" in "off") image_backend="off"; return ;;
"MATE"*) img="$(gsettings get org.mate.background picture-filename)" ;; *)
*) img="$(gsettings get org.gnome.desktop.background picture-uri)" ;; if [[ -d "$image_source" ]]; then
esac files=("${image_source%/}"/*.{png,jpg,jpeg})
img="$(printf "%s" "${files[RANDOM % (${#files[@]} - 1)]}")"
# Strip quotes etc from the path. else
img="${img/'file://'}" img="$image_source"
img="${img//\'}"
img="${img//\%20/ }"
fi fi
;;
"Mac OS X") [ ! -f "$img" ] && { image_backend="ascii"; get_ascii 2>/dev/null; return; }
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"
;;
esac
;; ;;
esac esac
# If img is an xml file don't use it. if [[ -n "$ITERM_PROFILE" ]]; then
[[ "${img/*\./}" == "xml" ]] && img="" image_program="iterm2"
# Error msg elif [[ "$(tycat 2>/dev/null)" ]]; then
[[ -z "$img" ]] && err "Image: Wallpaper detection failed, falling back to ascii mode." image_program="tycat"
else
image_program="w3m"
fi
make_thumbnail
display_image
;;
"ascii") get_ascii 2>/dev/null ;;
"off") ;;
esac
} }
get_ascii() { get_ascii() {
@ -1921,28 +1911,86 @@ get_ascii() {
export LC_ALL=C export LC_ALL=C
} }
get_image() { get_w3m_img_path() {
# Fallback to ascii mode if imagemagick isn't installed. if [[ -x "$w3m_img_path" ]]; then
type -p convert >/dev/null 2>&1 || image="ascii" 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"
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 else
img="$image" image_backend="ascii"
err "Image: w3m-img wasn't found on your system, falling back to ascii mode."
fi fi
}
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")"
elif type -p nitrogen >/dev/null; then
img="$(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"*) 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"
;;
esac
;; ;;
esac esac
# If img is an xml file don't use it.
[[ "${img/*\./}" == "xml" ]] && img=""
# Error msg
[[ -z "$img" ]] && err "Image: Wallpaper detection failed, falling back to ascii mode."
}
make_thumbnail() {
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"
elif [[ "$image_backend" == "tycat" ]]; then elif [[ "$image_program" == "tycat" ]]; then
printf "%b" "\033}qs\000" printf "%b" "\033}qs\000"
else else
@ -1954,7 +2002,7 @@ get_image() {
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
if [[ "$image_backend" == "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]))"
term_height="$((term_size[3] * term_size[1]))" term_height="$((term_size[3] * term_size[1]))"
@ -1969,7 +2017,7 @@ get_image() {
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_backend" != "tycat" ]]; then if (("${#term_size}" <= 5)) && [[ "$image_program" != "tycat" ]]; then
if type -p xdotool >/dev/null 2>&1 && \ if type -p xdotool >/dev/null 2>&1 && \
[[ "$image_backend" != "iterm2" ]]; then [[ "$image_backend" != "iterm2" ]]; then
@ -1981,7 +2029,7 @@ get_image() {
elif type -p xwininfo >/dev/null 2>&1 && \ elif type -p xwininfo >/dev/null 2>&1 && \
type -p xdpyinfo >/dev/null 2>&1 || \ type -p xdpyinfo >/dev/null 2>&1 || \
type -p xprop >/dev/null 2>&1 && \ type -p xprop >/dev/null 2>&1 && \
[[ "$image_backend" != "iterm2" ]]; then [[ "$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]+")"
@ -2121,43 +2169,8 @@ get_image() {
img="$thumbnail_dir/$imgname" img="$thumbnail_dir/$imgname"
} }
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="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"
elif [[ "$(tycat 2>/dev/null)" ]]; then
image_backend="tycat"
else
image_backend="w3m"
fi
}
display_image() { display_image() {
if [[ "$image" != "ascii" ]]; then case "$image_program" in
case "$image_backend" in
"w3m") "w3m")
# Add a tiny delay to fix issues with images not # Add a tiny delay to fix issues with images not
# appearing in specific terminal emulators. # appearing in specific terminal emulators.
@ -2174,7 +2187,6 @@ display_image() {
tycat "$img" tycat "$img"
;; ;;
esac esac
fi
} }
# SCREENSHOT # SCREENSHOT
@ -2742,7 +2754,7 @@ get_term_padding() {
dynamic_prompt() { dynamic_prompt() {
# Calculate image height in terminal cells. # Calculate image height in terminal cells.
if [[ "$image" != "ascii" ]]; then if [[ "$image_backend" != "ascii" ]]; then
get_term_padding 2>/dev/null get_term_padding 2>/dev/null
lines="$(((height + (${border:-0} * 2) + ${yoffset:-0}) / font_height))" lines="$(((height + (${border:-0} * 2) + ${yoffset:-0}) / font_height))"
fi fi
@ -3007,8 +3019,8 @@ get_args() {
# Image # Image
--image) --image)
image="$2" image_source="$2"
case "$2" in "-"* | "") image="ascii" ;; esac case "$2" in "-"* | "") image_backend="ascii" ;; esac
;; ;;
--image_size | --size) image_size="$2" ;; --image_size | --size) image_size="$2" ;;
@ -3027,7 +3039,7 @@ get_args() {
# Ascii # Ascii
--ascii) --ascii)
image="ascii" image_backend="ascii"
ascii="$2" ascii="$2"
case "$2" in "-"* | "") ascii="distro" ;; esac case "$2" in "-"* | "") ascii="distro" ;; esac
;; ;;
@ -3105,21 +3117,9 @@ main() {
# Hide the cursor and disable line wrap # Hide the cursor and disable line wrap
printf "\033[?25l\033[?7l" printf "\033[?25l\033[?7l"
# Display the image
if [[ "$image" != "off" ]]; then
get_image_backend get_image_backend
# Find w3mimgdisplay if [[ "$image_backend" != "off" ]]; then
[[ "$image_backend" == "w3m" ]] && \
[[ "$image" != "ascii" ]] && \
get_w3m_img_path
# Get the image src
get_image
# Display the image if enabled
display_image
# Set cursor position next to ascii art # Set cursor position next to ascii art
printf "%b" "\033[$((${lines:-0} - ${prompt_loc:-0}))A" printf "%b" "\033[$((${lines:-0} - ${prompt_loc:-0}))A"
@ -3132,12 +3132,12 @@ main() {
print_info 2>/dev/null print_info 2>/dev/null
# Prompt calculation # Prompt calculation
if [[ "$image" != "off" ]]; then if [[ "$image_backend" != "off" ]]; then
dynamic_prompt dynamic_prompt
# 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_backend" == "w3m" ]] && display_image [[ "$image_program" == "w3m" ]] && display_image
fi fi
# Re-enable line wrap # Re-enable line wrap