Marak / colors.js

get colors in your node.js console
https://github.com/Marak/colors.js
Other
5.16k stars 446 forks source link

FEATURE: a way to clear the escape sequences from a string... #274

Open flynx opened 4 years ago

flynx commented 4 years ago

Hi,

Something like this would come in really handy for cases when string lengths would need to be calculated for text formatting (i.e. screen char length)...

Object.defineProperty(String.prototype, 'raw', {
    get: function(){
        return this.replace(/\x1b\[..?m/g, '') }, })

Used like this

var s = 'some string'.zebra

console.log('number of characters:', s.length, 
    '\nscreen width in chars:', s.raw.length)

Thanks!

mikeerickson commented 4 years ago

Hey @flynx Thanks for this, just what I needed.

flynx commented 4 years ago

Hey @flynx Thanks for this, just what I needed.

You are quite welcome, but I really hope @Marak would get into this discussion as externally patching a good lib is always worse than using relevant provided functionality =)

mikeerickson commented 4 years ago

@flynx how about submitting a PR to provide the needed changes. I would think if it had merit @Marak would accept the PR.

flynx commented 4 years ago

@flynx how about submitting a PR to provide the needed changes. I would think if it had merit @Marak would accept the PR.

So far I had just enough time to do a simple hack/patch (posted above), a good PR would involve some more work diving into the module architecture/style/infrastructure/... ...I'm coming up on a point in the project I'm working on now where I'll have some time for this and subsequently will have to make a decision on the way forward on this aspect, I'll think about it then =)

flynx commented 4 years ago

@flynx how about submitting a PR to provide the needed changes. I would think if it had merit @Marak would accept the PR.

A quick look at the code revealed that this is already implemented but I/we either missed it in the docs or it is not documented...

Here's a converted example:

var s = 'some string'.zebra

console.log('number of characters:', s.length, 
    '\nscreen width in chars:', s.strip.length)

Guess I now have to re-qualify this as a documentation missing issue, but first re-check the docs, just in case ;)