getSelectedText method
Extracts the selected text from rendered content lines.
lines should be the full content lines (not just visible), in order.
Selection coordinates are in content space.
Implementation
String getSelectedText(List<String> lines) {
if (!hasSelection) return '';
final s = _selectionStart!;
final e = _selectionEnd!;
final startY = math.min(s.y, e.y);
final endY = math.max(s.y, e.y);
if (startY < 0 || endY >= lines.length) return '';
final sb = StringBuffer();
for (var y = startY; y <= endY; y++) {
final line = lines[y];
final plain = Style.stripAnsi(line);
int startX, endX;
if (startY == endY) {
startX = math.min(s.x, e.x);
endX = math.max(s.x, e.x);
} else if (y == startY) {
startX = s.y < e.y ? s.x : e.x;
endX = Style.visibleLength(plain);
} else if (y == endY) {
startX = 0;
endX = s.y < e.y ? e.x : s.x;
} else {
startX = 0;
endX = Style.visibleLength(plain);
}
final maxX = Style.visibleLength(plain);
startX = startX.clamp(0, maxX);
endX = endX.clamp(0, maxX);
if (startX < endX) {
sb.write(cutAnsiByCells(plain, startX, endX));
}
if (y < endY) {
sb.write('\n');
}
}
return sb.toString();
}