neofetch icon indicating copy to clipboard operation
neofetch copied to clipboard

Strip color codes from ascii art line length calculations

Open Syphist opened this issue 5 years ago • 13 comments

Description

This removes color and formatting codes from the line length calculation to allow for more control over custom ascii art. This would also fix issues I found when attempting to use the patch in #1220.

Features

Allows for support for finer control of ascii art using escapes such as "[38;5;39m" for defining coloring and formatting in the terminal.

Issues

None that I have found

TODO

Nothing

Fixed Issues

This so far only works if you directly paste the character for \033 or "" into the file. I was unable to get it to work for both.

Syphist avatar Aug 25 '20 05:08 Syphist

I have made several more attempts to work around SC2001, but have had no success. I am wondering if it is too complex to do it with the search and replace in bash and may need to be done with sed. I will probably make further attempts at this, if anyone else has any ideas feel free to let me know too.

Syphist avatar Aug 25 '20 09:08 Syphist

I have figured out the solution and have updated my code accordingly and have cleaned it up. I have tested both with the pasted character in the ascii file and the escape code and both work. This is also much faster than my initial sed implementation. I have updated the OP to reflect these changes as well as clarify a few things.

Syphist avatar Aug 25 '20 09:08 Syphist

+1 I agree, it's much needed this.

Z-8Bit avatar Jul 27 '21 16:07 Z-8Bit

+1 this modification works well for me, have tested on Big Sur

Screen Shot 2021-12-27 at 9 54 00 PM

thenick775 avatar Dec 28 '21 05:12 thenick775

Just tested this with colored ascii generated with lolcat and works great.

fvegaswp avatar Mar 29 '22 07:03 fvegaswp

Tested with colored ascii generated from jp2a and ascii-image-converter. Thanks Syphist. image

jonaramos avatar Jul 14 '22 09:07 jonaramos

Lol I actually ran into this issue while creating HyFetch. My solution was to pass in a separate ascii_len variable:

image

Thanks a lot!

hykilpikonna avatar Aug 12 '22 01:08 hykilpikonna

Works!

image

hykilpikonna avatar Aug 12 '22 01:08 hykilpikonna

Thank you for your contribution!

This PR is merged into hyfetch since this repo (dylanaraps/neofetch) seems no longer maintained.

HyFetch is a fork of neofetch with LGBTQ pride flags, but the repo also maintains an updated version of the original neofetch, addressing many pull requests that are not merged in the original repo.

Read the "Running Updated Original Neofetch" section for more info!

hykilpikonna avatar Aug 12 '22 01:08 hykilpikonna

I found a bug in the last line of strip_escape_codes:

-     eval "$2=\"${output}\""
+     eval "$2='${output}'"

If there exists $ signs in the ascii art, this eval statement will replace the $ with a variable content. You should use single quotes to prevent this.

Before Fix:

image

After Fix:

image

hykilpikonna avatar Aug 23 '22 15:08 hykilpikonna

I have committed your fix. Thanks for finding a solution and testing further.

Syphist avatar Aug 23 '22 15:08 Syphist

Okay I found another bug lol

- while IFS=$'\n' read -r line; do
+ while IFS=$'\n' read line; do

The width is incorrectly calculated for ascii distros with backslashes in them (e.g. Bedrock) because the backslashes were not treated as escape characters in read -r while they are actually escaped when defining the ASCII:

image

So, if read -r is used, the escaped backslash (\\) will not be resolved as one character but two characters, making the ascii length longer. (https://github.com/hykilpikonna/hyfetch/issues/19)

Before Fix:

image

After Fix:

image

hykilpikonna avatar Sep 05 '22 21:09 hykilpikonna

Thanks @hykilpikonna I fixed the issue on my fork as well just in case this ever gets merged upstream to neofetch or if someone bothers to use my fork.

Syphist avatar Sep 05 '22 21:09 Syphist