suppressOverflowingTerminalGraphics function
Removes graphics displays that would paint outside viewportHeight.
The terminal does not clip graphics protocol payloads to ordinary text slicing. Replacing an overflowing display sequence with equivalent cell padding keeps layout stable while preventing out-of-viewport image paints.
Implementation
String suppressOverflowingTerminalGraphics(String text, int viewportHeight) {
if (viewportHeight <= 0 ||
(!text.contains('\x1b_G') && !text.contains('\x9fG'))) {
return text;
}
final lines = text.split('\n');
var changed = false;
for (var lineIndex = 0; lineIndex < lines.length; lineIndex++) {
final remainingRows = viewportHeight - lineIndex;
if (remainingRows <= 0) {
lines[lineIndex] = '';
changed = true;
continue;
}
final clipped = _suppressOverflowingLineGraphics(
lines[lineIndex],
remainingRows: remainingRows,
);
if (clipped != lines[lineIndex]) {
lines[lineIndex] = clipped;
changed = true;
}
}
return changed ? lines.join('\n') : text;
}