colors.js icon indicating copy to clipboard operation
colors.js copied to clipboard

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

Open flynx opened this issue 5 years ago • 5 comments

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!

flynx avatar Jun 03 '20 21:06 flynx

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

mikeerickson avatar Jun 23 '20 22:06 mikeerickson

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 =)

flynx avatar Jun 26 '20 19:06 flynx

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

mikeerickson avatar Jun 26 '20 20:06 mikeerickson

@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 avatar Jun 28 '20 15:06 flynx

@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 ;)

flynx avatar Jun 28 '20 15:06 flynx