formatBytes method

Uint8List formatBytes()

Formats the terminal into a byte buffer.

Implementation

Uint8List formatBytes() {
  _ensureOpen();
  _terminal._ensureOpen();

  var required = _requiredSize();
  if (required == 0) {
    return Uint8List(0);
  }

  for (var attempt = 0; attempt < 2; attempt++) {
    final buffer = calloc<ffi.Uint8>(required);
    final outWritten = calloc<ffi.Size>();
    try {
      final result = bindings.ghostty_formatter_format_buf(
        _handle,
        buffer,
        required,
        outWritten,
      );
      if (result == bindings.GhosttyResult.GHOSTTY_SUCCESS) {
        return Uint8List.fromList(buffer.asTypedList(outWritten.value));
      }
      if (result != bindings.GhosttyResult.GHOSTTY_OUT_OF_SPACE) {
        _checkResult(result, 'ghostty_formatter_format_buf');
      }
      required = outWritten.value;
      if (required == 0) {
        return Uint8List(0);
      }
    } finally {
      calloc.free(outWritten);
      calloc.free(buffer);
    }
  }

  throw StateError(
    'VtTerminalFormatter output changed while formatting. Retry the call.',
  );
}