messageLines function
The lines of message, each a list of spans, every span's style merged
onto style.
A String is read as a terminal would show it (parseAnsi): its SGR
sequences and OSC 8 links become the styles of its spans, every other
sequence is dropped, so a string styled elsewhere follows --no-color
too. A Span is itself; an Iterable is its elements one after
another, each a string or a span; null is an empty line; anything else
its toString. There is always at least one line.
Implementation
List<List<Span>> messageLines(Object? message, Style style) {
final lines = <List<Span>>[[]];
void add(Object? part) {
switch (part) {
case null:
return;
case Span():
lines.last.add(part);
case String():
final parsed = parseAnsi(part);
for (var i = 0; i < parsed.length; i++) {
if (i > 0) {
lines.add([]);
}
lines.last.addAll(parsed[i]);
}
if (part.endsWith('\n')) {
lines.add([]);
}
case Iterable<Object?>():
part.forEach(add);
default:
add('$part');
}
}
add(message);
if (lines.length > 1 && lines.last.isEmpty) {
// a message that ends with a line break is the lines before it
lines.removeLast();
}
return [
for (final line in lines)
[
for (final span in line)
if (span.text.isNotEmpty) _onto(span, style),
],
];
}