drudru/ansi_up

State is preserved between calls

Kagami opened this issue · 2 comments

Kagami commented

Hi,

Not sure if that is intended to be like so, but seems like ansi_up preserves state between calls and if previous call contained incomplete ANSI sequence, the next output will be corrupted:

import { AnsiUp } from "ansi_up";

const out1 = "\x1b[0m\x1b[0;32mTEXT\x1b[0m\x1b[1";
const out2 = "\x1b[0m\x1b[1;34mTEST";

const ansi_up = new AnsiUp();
ansi_up.use_classes = true;

ansi_up.ansi_to_html(out1);
console.log(ansi_up.ansi_to_html(out2));

It outputs [1 at the start of the second call, but doesn't do it if I comment the first call.

Related: #88

Thanks.

drudru commented

Hello @Kagami

This is by design. '\x1b[1' is not a complete ANSI sequence.
Therefore it is invalid.
The library recognizes that the sequence is invalid as soon as the \x1b or ESC is received from out2.
In that situation, the library will ignore the \x1b and just treat the [1 as text.

Kagami commented

Ok, thanks.