@@ -39,7 +39,7 @@ sys_locale=${LANG:-C}
39
39
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:- ${HOME} / .config}
40
40
PATH=$PATH :/usr/xpg4/bin:/usr/sbin:/sbin:/usr/etc:/usr/libexec
41
41
reset=' \e[0m'
42
- shopt -s nocasematch
42
+ shopt -s nocasematch extglob
43
43
44
44
# Speed up script by not using unicode.
45
45
LC_ALL=C
@@ -4580,6 +4580,26 @@ image_backend() {
4580
4580
[[ " $image_backend " != " off" ]] && printf ' \e[%sA\e[9999999D' " ${lines:- 0} "
4581
4581
}
4582
4582
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
+
4583
4603
print_ascii () {
4584
4604
if [[ -f " $image_source " && ! " $image_source " =~ (png| jpg| jpeg| jpe| svg| gif) ]]; then
4585
4605
ascii_data=" $( < " $image_source " ) "
@@ -4598,8 +4618,12 @@ print_ascii() {
4598
4618
lines=$ascii_lines
4599
4619
else
4600
4620
while IFS=$' \n ' read -r line; do
4601
- line=${line// \\\\ / \\ }
4602
4621
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]/ }
4603
4627
(( ++ lines, ${# line} > ascii_len)) && ascii_len=" ${# line} "
4604
4628
done <<< " ${ascii_data//\$\{??\}}"
4605
4629
fi
0 commit comments