getTrimmedLength method

int getTrimmedLength([
  1. int? cols
])

Returns the offset of the last cell that has content from the start of the line.

Implementation

int getTrimmedLength([int? cols]) {
  final maxCols = _data.length ~/ _cellSize;

  if (cols == null || cols > maxCols) {
    cols = maxCols;
  }

  if (cols <= 0) {
    return 0;
  }

  for (var i = cols - 1; i >= 0; i--) {
    var codePoint = getCodePoint(i);

    if (codePoint != 0) {
      // we are at the last cell in this line that has content.
      // the length of this line is the index of this cell + 1
      // the only exception is that if that last cell is wider
      // than 1 then we have to add the diff
      final lastCellWidth = getWidth(i);
      return i + lastCellWidth;
    }
  }
  return 0;
}