From 0e7799ff7ef86bafb440b53d34ada6a60f3340cc Mon Sep 17 00:00:00 2001 From: Syphist Date: Tue, 25 Aug 2020 00:00:26 -0500 Subject: [PATCH 1/6] Using regex I was able to strip color codes from ascii art line length calculations. --- neofetch | 2 ++ 1 file changed, 2 insertions(+) diff --git a/neofetch b/neofetch index 4a380962..985e6baf 100755 --- a/neofetch +++ b/neofetch @@ -3878,6 +3878,8 @@ print_ascii() { while IFS=$'\n' read -r line; do line=${line//\\\\/\\} line=${line//█/ } + # Use Regex through sed to remove color and formatting codes + line=$(echo "$line" | sed 's/\x1B\[[0-9;]*[JKmsu]//g') ((++lines,${#line}>ascii_len)) && ascii_len="${#line}" done <<< "${ascii_data//\$\{??\}}" From 4141531b229829e23f843d6a90469061660c1e68 Mon Sep 17 00:00:00 2001 From: Syphist Date: Tue, 25 Aug 2020 04:40:42 -0500 Subject: [PATCH 2/6] Fixed SC2001 error, you also need to use \033 instead of pasting the character in. --- neofetch | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/neofetch b/neofetch index 985e6baf..efce4681 100755 --- a/neofetch +++ b/neofetch @@ -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 @@ -3879,7 +3879,8 @@ print_ascii() { line=${line//\\\\/\\} line=${line//█/ } # Use Regex through sed to remove color and formatting codes - line=$(echo "$line" | sed 's/\x1B\[[0-9;]*[JKmsu]//g') + search='\\033\[*([0-9;])[JKmsu]' + line=${line//$search/} ((++lines,${#line}>ascii_len)) && ascii_len="${#line}" done <<< "${ascii_data//\$\{??\}}" From 10cf43e53a42a922d6bf53205482493b90d8b376 Mon Sep 17 00:00:00 2001 From: Syphist Date: Tue, 25 Aug 2020 04:45:13 -0500 Subject: [PATCH 3/6] Added support for pasted character and removed unecessary variables. --- neofetch | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neofetch b/neofetch index efce4681..f5665123 100755 --- a/neofetch +++ b/neofetch @@ -3879,8 +3879,8 @@ print_ascii() { line=${line//\\\\/\\} line=${line//█/ } # Use Regex through sed to remove color and formatting codes - search='\\033\[*([0-9;])[JKmsu]' - line=${line//$search/} + line=${line//\\033\[*([0-9;])[JKmsu]/} + line=${line//\[*([0-9;])[JKmsu]/} ((++lines,${#line}>ascii_len)) && ascii_len="${#line}" done <<< "${ascii_data//\$\{??\}}" From 86e266e5b7e2d7ff8411f4081cd2bc12b3b79280 Mon Sep 17 00:00:00 2001 From: Syphist Date: Tue, 25 Aug 2020 04:49:06 -0500 Subject: [PATCH 4/6] Made comment accurate --- neofetch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neofetch b/neofetch index f5665123..785836ea 100755 --- a/neofetch +++ b/neofetch @@ -3878,7 +3878,7 @@ print_ascii() { while IFS=$'\n' read -r line; do line=${line//\\\\/\\} line=${line//█/ } - # Use Regex through sed to remove color and formatting codes + # Use patterns to replace color codes line=${line//\\033\[*([0-9;])[JKmsu]/} line=${line//\[*([0-9;])[JKmsu]/} ((++lines,${#line}>ascii_len)) && ascii_len="${#line}" From cb32928102be4fed8871b877ad5f195e69b163f4 Mon Sep 17 00:00:00 2001 From: Syphist Date: Tue, 25 Aug 2020 05:34:33 -0500 Subject: [PATCH 5/6] Added patch from dylanaraps/neofetch/1220 as it runs faster in some instances and does not hinder performance where it doesn't work --- neofetch | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/neofetch b/neofetch index 785836ea..32d48286 100755 --- a/neofetch +++ b/neofetch @@ -3862,6 +3862,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")" @@ -3876,9 +3896,10 @@ print_ascii() { # Calculate size of ascii file in line length / line count. while IFS=$'\n' read -r line; do - line=${line//\\\\/\\} line=${line//█/ } - # Use patterns to replace color codes + # This method to strip escape codes doesn't always work but is faster in some cases if run first + 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}" From 1b5dcdf7bc23964fc0be72c75c9d7db81442f047 Mon Sep 17 00:00:00 2001 From: Syphist Date: Tue, 25 Aug 2020 05:44:17 -0500 Subject: [PATCH 6/6] Fixed build error detected due to long comment. --- neofetch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neofetch b/neofetch index 32d48286..129779c8 100755 --- a/neofetch +++ b/neofetch @@ -3897,7 +3897,7 @@ print_ascii() { # Calculate size of ascii file in line length / line count. while IFS=$'\n' read -r line; do line=${line//█/ } - # This method to strip escape codes doesn't always work but is faster in some cases if run first + # 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]/}