decodeAnsiColorEscapeCodes function
Main entrypoint to call to parse ansi color escaped text.
An instance of ansiUp is passed in to maintain text styling state across multiple invocations of this method.
Implementation
Iterable<StyledText> decodeAnsiColorEscapeCodes(
String text,
AnsiUp ansiUp,
) sync* {
ansiUp._text = text;
while (true) {
final packet = ansiUp._getNextPacket();
if ((packet.kind == PacketKind.EOS) ||
(packet.kind == PacketKind.Incomplete)) {
break;
}
// Drop single ESC or Unknown CSI.
if ((packet.kind == PacketKind.ESC) ||
(packet.kind == PacketKind.Unknown)) {
continue;
}
if (packet.kind == PacketKind.Text) {
yield StyledText.from(ansiUp._withState(packet));
} else if (packet.kind == PacketKind.SGR) {
ansiUp._processAnsi(packet);
} else if (packet.kind == PacketKind.OSCURL) {
final url = packet.url;
if (url.startsWith('http:') || url.startsWith('https:')) {
yield StyledText(packet.text, url: url);
} else {
yield StyledText(packet.text); // Not a safe url to include.
}
}
}
}