chalk / slice-ansi

Slice a string with ANSI escape codes
MIT License
47 stars 21 forks source link

Slice fails when ANSI string and ordinary string are mixed #14

Closed kjirou closed 4 years ago

kjirou commented 6 years ago

Issuehunt badges

Hello, the following behaviors looked like a bug.

node
> chalk = require('chalk')
> sliceAnsi = require('slice-ansi')
> str = 'a' + chalk.red('bc') + 'd'
'a\u001b[31mbc\u001b[39md'
> sliceAnsi(str, 0, 1) === 'a'
false  # I expected true
> sliceAnsi(str, 1, 2) === chalk.red('b')
true
> sliceAnsi(str, 2, 3) === chalk.red('c')
false  # I expected true
> sliceAnsi(str, 3, 4) === 'd'
false  # I expected true

As a supplement, the example described in the README appeared to be working properly.

node
> str2 = 'The quick brown ' + chalk.red('fox jumped over ') + 'the lazy ' + chalk.green('dog and then ran away with the unicorn.');
'The quick brown \u001b[31mfox jumped over \u001b[39mthe lazy \u001b[32mdog and then ran away with the unicorn.\u001b[39m'
> sliceAnsi(str2, 20, 30) === chalk.red('jumped ove')
true

Thanks.

Environments


IssueHunt Summary #### [tiagodanin tiagodanin](https://issuehunt.io/u/tiagodanin) has been rewarded. ### Backers (Total: $60.00) - [issuehunt issuehunt](https://issuehunt.io/u/issuehunt) ($60.00) ### Submitted pull Requests - [#25 [WIP] Get all ansi escape sequences](https://issuehunt.io/r/chalk/slice-ansi/pull/25) --- ### Tips - Checkout the [Issuehunt explorer](https://issuehunt.io/r/chalk/slice-ansi/) to discover more funded issues. - Need some help from other developers? [Add your repositories](https://issuehunt.io/r/new) on IssueHunt to raise funds. --- IssueHunt has been backed by the following sponsors. [Become a sponsor](https://issuehunt.io/membership/members)
sindresorhus commented 6 years ago

Would you be able to submit a pull request with a failing test? :)

kjirou commented 6 years ago

@sindresorhus Pull-requested!

kjirou commented 6 years ago

Pull-requireted again with #16

IssueHuntBot commented 5 years ago

@issuehunt has funded $60.00 to this issue. See it on IssueHunt

sindresorhus commented 5 years ago

// @stroncium This one has a bounty too, in case you're interested ;)

stroncium commented 5 years ago

Haha, don't think of me as so materialistic. I actually don't think I will be able to even use bounties except funding some more issues(don't think paypal will allow me to accept any money due to my country of residence).

Anyhow, I actually wrote all the code for this issue already. The problem is the code is much bigger than whole this library as it required implementing first proper escape sequence tokenizer, then a virtual machine to process SGR commands, compare states and a compiler of state changes to commands which introduce them, plus I'd like to also implement a few other escape sequences which would make sense in terms of string manipulations. I also reckon this code would allow to implement a much simpler and correct version of wrap-ansi too. So, I'm thinking what to do with this code now.

issuehunt-oss[bot] commented 4 years ago

@sindresorhus has rewarded $54.00 to @tiagodanin. See it on IssueHunt