Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I was making a small block drawing package with Node.js, and i was tinkering with it, but at the exact moment i put a ANSI Color code, it started to break.

Before:
Image[^]

After
Image[^]

The reason that makes it break is that all the characters i use to make the ANSI codes work are being treated as just normal characters when i use the string .length property.

So what i need is to find a way to get the .length property not take in count the non-printable characters

What I have tried:

I even started to write a regex, before noting how weird it will be with some other things like other escapes and so on...
Posted
Updated 12-Nov-22 6:15am
v2

That's because ANSI escape sequences are just that: sequences.
They have a common format: They start with an ESC character, and the following character either completes the sequence are indicates a Control Sequence if is is an open square bracket.
Control Sequences are variable length, and can include multiple numeric values separated by semicolons, terminated by the command code character.
For example, the ANSI escape sequence to set the colour to white on a blue background would be ESC [ 3 7 ; 4 4 m (with no spaces)
So when you include an ANSI sequence in a string, the length reflects the number of characters in it - not the number of escape sequences plus the number of printable characters!
 
Share this answer
 
Comments
Member 15627495 12-Nov-22 6:23am    
by string.js, you request the 'JS and Dom Engine'. then all ANSI sequences are built when you call .length.

if you read your file as 'text/chars' , you'll gain on it. but through Js you will only get all 'Js types' with cast of the ANSI to some strings. when reading about Js I learn that the DOM is fill and built before you have it for display. It's a pre-execution of all ( functions too ), that useful for debug and error tracking, an error is thrown before you use the resource of your code. to say code and dom are built before you use the web page or scripts.
Quote:
The reason that makes it break is that all the characters i use to make the ANSI codes work are being treated as just normal characters when i use the string .length property.

That's because, in the terms of a string, they ARE normal characters.

The control sequences only become meaningful when the console rendering engine sees the ESC character and start interpreting the characters after it. The only way you're going to get an accurate character count for the rest of your string is to write your own interpreter to identify the control sequences and remove them from the string. The problem you have is there are a LOT of control commands and not all control sequences are the same number of characters. They vary in length by the command character and length of parameters for that command.

Once you scan the string for all the control sequences and remove them, then you can get the length of the remaining string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900