chalk / ansi-regex

Regular expression for matching ANSI escape codes
MIT License
184 stars 79 forks source link

Some vim (or iTerm2?) ANSI sequences not fully consumed #50

Closed shyndman closed 2 years ago

shyndman commented 2 years ago

Hi there,

I've been playing around with your library, feeding it strings from iTerm2 using "Edit > Copy With Control Sequences", and I came across several that are not fully consumed by the regex.

They appear to be non-standard (I think — I'm not an expert), and while I copied them out of a vim session, I don't know whether iTerm2 is reporting the sequences as they were written, or giving me back its internal state (which presumably could differ?).

Anyway, here's what I'm opening in vim:

repro.tsx

interface TextPaneProps {
  label: string;
}

and here's what I'm getting out of the "Copy With Control Sequences" action:

interface TextPaneProps {
  label: [0;38:5:121mstring;
}

The sequence coloring the word string (with an 8-bit color) uses both semicolons AND colon separators.

This sequence (and I've noticed a few others like it) are only consumed up until right before the first colon: https://regex101.com/r/5B3Hte/1

Thanks! Hope this helps.

shyndman commented 2 years ago

I believe I've verified it is iTerm2's doing, not vim's.

Running this in my shell:

echo '\x1b[0;38;5;121mwidth\x1b[0m:\x1b[0;103m100%\x1b[0m;'

Gives me this back when copied:

[0;38:5:121mwidth:100%;

I'm curious to hear what you think.

Qix- commented 2 years ago

Correct me if I'm wrong but this has nothing to do with the ansi-regex node.js package.

shyndman commented 2 years ago

Well, it might? Like I said, I'm not an expert. Is that valid ANSI or not? The regex doesn't match on it.

Is it or isn't it the purpose of this package to match on ANSI escape codes?

Qix- commented 2 years ago

It's just that you didn't provide any code to indicate you're even using this library.

> s ='\x1b[0;38;5;121mwidth\x1b[0m:\x1b[0;103m100%\x1b[0m;'
'\x1B[0;38;5;121mwidth\x1B[0m:\x1B[0;103m100%\x1B[0m;'
> r = new RegExp(pattern, 'g')
/[\u001B\u009B][[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d\/#&.:=?%@~_]+)*|[a-zA-Z\d]+(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?\u0007)|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))/g
> r.exec(s)
[
  '\x1B[0;38;5;121m',
  index: 0,
  input: '\x1B[0;38;5;121mwidth\x1B[0m:\x1B[0;103m100%\x1B[0m;',
  groups: undefined
]
> r.exec(s)
[
  '\x1B[0m',
  index: 18,
  input: '\x1B[0;38;5;121mwidth\x1B[0m:\x1B[0;103m100%\x1B[0m;',
  groups: undefined
]
> r.exec(s)
[
  '\x1B[0;103m',
  index: 23,
  input: '\x1B[0;38;5;121mwidth\x1B[0m:\x1B[0;103m100%\x1B[0m;',
  groups: undefined
]
> r.exec(s)
[
  '\x1B[0m',
  index: 35,
  input: '\x1B[0;38;5;121mwidth\x1B[0m:\x1B[0;103m100%\x1B[0m;',
  groups: undefined
]
> r.exec(s)
null

Seems like it works fine.

Can you show some code?