ansiToTextSpan function

TextSpan ansiToTextSpan(
  1. String source, {
  2. TextStyle? baseStyle,
})

Implementation

TextSpan ansiToTextSpan(String source, {TextStyle? baseStyle}) {
  source = _normalizeControlChars(source);
  source = _stripNonSgrCsi(source);

  final sgr = RegExp(r'(\x1B|\u001B)\[([0-9;]*)m');

  final List<InlineSpan> spans = [];
  TextStyle current = baseStyle ?? const TextStyle();
  int last = 0;

  for (final m in sgr.allMatches(source)) {
    if (m.start > last) {
      spans.add(TextSpan(text: source.substring(last, m.start), style: current));
    }

    final params = m[2]!.isEmpty ? <int>[0] : m[2]!.split(';').where((s) => s.isNotEmpty).map(int.parse).toList();
    _applySgr(params, (t) => current = t(current));

    last = m.end;
  }
  if (last < source.length) {
    spans.add(TextSpan(text: source.substring(last), style: current));
  }

  return TextSpan(style: baseStyle, children: spans);
}