textForSelection method

  1. @override
String textForSelection(
  1. GhosttyTerminalSelection selection, {
  2. GhosttyTerminalCopyOptions options = const GhosttyTerminalCopyOptions(),
})
override

Extracts plain text for an inclusive cell selection.

Implementation

@override
String textForSelection(
  GhosttyTerminalSelection selection, {
  GhosttyTerminalCopyOptions options = const GhosttyTerminalCopyOptions(),
}) {
  final normalized = selection.normalized;
  if (lines.isEmpty) {
    return '';
  }

  final startRow = normalized.start.row.clamp(0, lines.length - 1);
  final endRow = normalized.end.row.clamp(0, lines.length - 1);
  final buffer = StringBuffer();
  for (var row = startRow; row <= endRow; row++) {
    final line = lines[row];
    final startCol = row == startRow ? normalized.start.col : 0;
    final endCol = row == endRow ? normalized.end.col : line.cellCount - 1;
    if (line.cellCount > 0) {
      final text = line.textForCellRange(startCol, endCol);
      buffer.write(options.trimTrailingSpaces ? text.trimRight() : text);
    }
    if (row != endRow) {
      buffer.write('\n');
    }
  }
  return buffer.toString();
}