chalk / slice-ansi

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

Support true color (ISO-8613-3) #17

Closed ghost closed 5 years ago

ghost commented 6 years ago

One way to do it:

const code = /\[(.*?)m/.exec(str.slice(i))[1]

I can't quite get the test cases, the output seems ok, but a lot of garbage tags e.g.:

test('supports true color (ISO-8613-3)', t => {
  t.is(m(
    '\u001b[38;5;181mpretty \u001b[39m\u001b[38;5;181municorn\u001b[39m', 7, 14),
    '\u001b[38;5;181municorn\u001b[39m'
  )
  // == '\u001b[38;5;181m\u001b[39m\u001b[38;5;181municorn\u001b[39m'
})
piranna commented 6 years ago

I have done a fix for that at https://github.com/chalk/slice-ansi/pull/18. Regarding the garbage tags, it's due how slice-ansi works, because it doesn't maintain state, just only the last tag for the current color. By maintaining state, it could be able to remove the useless and closed ones, maybe could be done in another pull-request.