toAnsiString method

String toAnsiString({
  1. bool trimTrailingSpaces = true,
  2. bool resetLineEndings = false,
})

Serializes this Buffer grid into an ANSI escape sequence string.

When trimTrailingSpaces is true (default), unstyled trailing blank and wide-padding cells (bg == 0 && mods == 0) are trimmed on each row. When resetLineEndings is true, \x1b[0m is emitted before \n on any row where an SGR style is active.

Implementation

String toAnsiString({
  bool trimTrailingSpaces = true,
  bool resetLineEndings = false,
}) {
  final sb = StringBuffer();
  var activeStyle = _defaultToAnsiStyle;

  for (var y = 0; y < height; y++) {
    final lastCol = trimTrailingSpaces ? _lastContentCol(y) : width - 1;
    activeStyle = _writeAnsiRowCells(sb, y, lastCol, activeStyle);
    if (resetLineEndings && activeStyle != _defaultToAnsiStyle) {
      activeStyle = _resetToDefaultAnsiStyle(sb, activeStyle);
    }
    sb.write('\n');
  }

  if (activeStyle != _defaultToAnsiStyle) {
    _resetToDefaultAnsiStyle(sb, activeStyle);
  }
  return sb.toString();
}