Skip to content

Commit eeead8d

Browse files
hykilpikonnaSyphist
and
Syphist
committed
[PR] dylanaraps/neofetch#1543 from Syphist - Strip color codes from ascii art line length calculations
Upstream PR: dylanaraps/neofetch#1543 Thanks to @Syphist Co-authored-by: Syphist <thesyphist@gmail.com>
2 parents 9cf4b6c + bd9a01e commit eeead8d

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

neofetch

+26-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ sys_locale=${LANG:-C}
3939
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-${HOME}/.config}
4040
PATH=$PATH:/usr/xpg4/bin:/usr/sbin:/sbin:/usr/etc:/usr/libexec
4141
reset='\e[0m'
42-
shopt -s nocasematch
42+
shopt -s nocasematch extglob
4343

4444
# Speed up script by not using unicode.
4545
LC_ALL=C
@@ -4580,6 +4580,26 @@ image_backend() {
45804580
[[ "$image_backend" != "off" ]] && printf '\e[%sA\e[9999999D' "${lines:-0}"
45814581
}
45824582

4583+
# From pull request #1220, this is a fast way to strip character codes
4584+
strip_escape_codes() {
4585+
local input="${1//\"/\\\"}" output="" i char within_code=0
4586+
for ((i=0; i < ${#input}; ++i)); do
4587+
char="${input:i:1}"
4588+
if (( within_code == 1 )); then
4589+
case "${char}" in
4590+
[a-zA-Z]) within_code=0 ;;
4591+
esac
4592+
continue
4593+
fi
4594+
if [[ "${char}" == $'\e' ]]; then
4595+
within_code=1
4596+
continue
4597+
fi
4598+
output+="${char}"
4599+
done
4600+
eval "$2=\"${output}\""
4601+
}
4602+
45834603
print_ascii() {
45844604
if [[ -f "$image_source" && ! "$image_source" =~ (png|jpg|jpeg|jpe|svg|gif) ]]; then
45854605
ascii_data="$(< "$image_source")"
@@ -4598,8 +4618,12 @@ print_ascii() {
45984618
lines=$ascii_lines
45994619
else
46004620
while IFS=$'\n' read -r line; do
4601-
line=${line//\\\\/\\}
46024621
line=${line/// }
4622+
# Fast method to strip codes
4623+
strip_escape_codes "${line}" line
4624+
# Use patterns to replace color codes that the above line did not catch
4625+
line=${line//\\033\[*([0-9;])[JKmsu]/}
4626+
line=${line//\[*([0-9;])[JKmsu]/}
46034627
((++lines,${#line}>ascii_len)) && ascii_len="${#line}"
46044628
done <<< "${ascii_data//\$\{??\}}"
46054629
fi

0 commit comments

Comments
 (0)