[PR] dylanaraps/neofetch#1543 from Syphist - Strip color codes from ascii art line length calculations

Upstream PR: https://github.com/dylanaraps/neofetch/pull/1543
Thanks to @Syphist

Co-authored-by: Syphist <thesyphist@gmail.com>
This commit is contained in:
Azalea (on HyDEV-Daisy) 2022-08-11 21:23:35 -04:00
commit eeead8d7c3

View file

@ -39,7 +39,7 @@ sys_locale=${LANG:-C}
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-${HOME}/.config}
PATH=$PATH:/usr/xpg4/bin:/usr/sbin:/sbin:/usr/etc:/usr/libexec
reset='\e[0m'
shopt -s nocasematch
shopt -s nocasematch extglob
# Speed up script by not using unicode.
LC_ALL=C
@ -4580,6 +4580,26 @@ image_backend() {
[[ "$image_backend" != "off" ]] && printf '\e[%sA\e[9999999D' "${lines:-0}"
}
# From pull request #1220, this is a fast way to strip character codes
strip_escape_codes() {
local input="${1//\"/\\\"}" output="" i char within_code=0
for ((i=0; i < ${#input}; ++i)); do
char="${input:i:1}"
if (( within_code == 1 )); then
case "${char}" in
[a-zA-Z]) within_code=0 ;;
esac
continue
fi
if [[ "${char}" == $'\e' ]]; then
within_code=1
continue
fi
output+="${char}"
done
eval "$2=\"${output}\""
}
print_ascii() {
if [[ -f "$image_source" && ! "$image_source" =~ (png|jpg|jpeg|jpe|svg|gif) ]]; then
ascii_data="$(< "$image_source")"
@ -4598,8 +4618,12 @@ print_ascii() {
lines=$ascii_lines
else
while IFS=$'\n' read -r line; do
line=${line//\\\\/\\}
line=${line//█/ }
# Fast method to strip codes
strip_escape_codes "${line}" line
# Use patterns to replace color codes that the above line did not catch
line=${line//\\033\[*([0-9;])[JKmsu]/}
line=${line//\[*([0-9;])[JKmsu]/}
((++lines,${#line}>ascii_len)) && ascii_len="${#line}"
done <<< "${ascii_data//\$\{??\}}"
fi