getSelectedText method
Returns the currently selected text.
Implementation
String getSelectedText() {
if (selectionStart == null || selectionEnd == null) return '';
final (x1, y1) = selectionStart!;
final (x2, y2) = selectionEnd!;
final startY = math.min(y1, y2);
final endY = math.max(y1, y2);
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(x1, x2);
endX = math.max(x1, x2);
} else if (y == startY) {
startX = y1 < y2 ? x1 : x2;
endX = Style.visibleLength(plain);
} else if (y == endY) {
startX = 0;
endX = y1 < y2 ? x2 : x1;
} 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(ranges.cutAnsiByCells(plain, startX, endX));
}
if (y < endY) {
sb.write('\n');
}
}
return sb.toString();
}