From 0e7799ff7ef86bafb440b53d34ada6a60f3340cc Mon Sep 17 00:00:00 2001 From: Syphist Date: Tue, 25 Aug 2020 00:00:26 -0500 Subject: [PATCH 1/8] 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 4a3809624..985e6bafd 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/8] 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 985e6bafd..efce46813 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/8] 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 efce46813..f56651239 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/8] Made comment accurate --- neofetch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neofetch b/neofetch index f56651239..785836eac 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/8] 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 785836eac..32d482865 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/8] Fixed build error detected due to long comment. --- neofetch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neofetch b/neofetch index 32d482865..129779c82 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]/} From d0f4bddf8f4ffbea1f9087d75ba9bbeccec4b7d1 Mon Sep 17 00:00:00 2001 From: Syphist Date: Tue, 23 Aug 2022 10:39:29 -0500 Subject: [PATCH 7/8] Fix Bug With $ Character Proposed by hykilpikonna. Fixes a bug that evals stuff that follows $ characters. Co-authored-by: Hykilpikonna --- neofetch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neofetch b/neofetch index 71d94334b..6b8a3c7d4 100755 --- a/neofetch +++ b/neofetch @@ -4071,7 +4071,7 @@ strip_escape_codes() { fi output+="${char}" done - eval "$2=\"${output}\"" + eval "$2='${output}'" } print_ascii() { From 37ba351f4a04f7926d55a33808d89037954ec871 Mon Sep 17 00:00:00 2001 From: Syphist Date: Mon, 5 Sep 2022 16:55:06 -0500 Subject: [PATCH 8/8] Fix issue with backslashes --- neofetch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neofetch b/neofetch index 6b8a3c7d4..f058f5223 100755 --- a/neofetch +++ b/neofetch @@ -4087,7 +4087,7 @@ print_ascii() { LC_ALL="$sys_locale" # Calculate size of ascii file in line length / line count. - while IFS=$'\n' read -r line; do + while IFS=$'\n' read line; do line=${line//█/ } # Fast method to strip codes strip_escape_codes "${line}" line