fromNative static method

VtGridRefSnapshot fromNative(
  1. Pointer<GhosttyGridRef> ref
)

Implementation

static VtGridRefSnapshot fromNative(
  ffi.Pointer<bindings.GhosttyGridRef> ref,
) {
  final cellPtr = calloc<bindings.GhosttyCell>();
  final rowPtr = calloc<bindings.GhosttyRow>();
  final stylePtr = calloc<bindings.GhosttyStyle>();
  final graphemeLen = calloc<ffi.Size>();
  try {
    stylePtr.ref.size = ffi.sizeOf<bindings.GhosttyStyle>();
    _checkResult(
      bindings.ghostty_grid_ref_cell(ref, cellPtr),
      'grid_ref_cell',
    );
    _checkResult(bindings.ghostty_grid_ref_row(ref, rowPtr), 'grid_ref_row');
    _checkResult(
      bindings.ghostty_grid_ref_style(ref, stylePtr),
      'grid_ref_style',
    );
    final first = bindings.ghostty_grid_ref_graphemes(
      ref,
      ffi.nullptr,
      0,
      graphemeLen,
    );
    if (first != bindings.GhosttyResult.GHOSTTY_OUT_OF_SPACE &&
        first != bindings.GhosttyResult.GHOSTTY_SUCCESS) {
      _checkResult(first, 'grid_ref_graphemes(size_probe)');
    }
    final length = graphemeLen.value;
    final graphemes = length == 0
        ? ''
        : (() {
            final buffer = calloc<ffi.Uint32>(length);
            try {
              _checkResult(
                bindings.ghostty_grid_ref_graphemes(
                  ref,
                  buffer,
                  length,
                  graphemeLen,
                ),
                'grid_ref_graphemes',
              );
              return String.fromCharCodes(
                buffer.asTypedList(graphemeLen.value),
              );
            } finally {
              calloc.free(buffer);
            }
          })();

    return VtGridRefSnapshot(
      x: ref.ref.x,
      y: ref.ref.y,
      cell: VtCellSnapshot.fromRaw(cellPtr.value),
      row: VtRowSnapshot.fromRaw(rowPtr.value),
      style: VtStyle.fromNative(stylePtr.ref),
      graphemes: graphemes,
    );
  } finally {
    calloc.free(graphemeLen);
    calloc.free(stylePtr);
    calloc.free(rowPtr);
    calloc.free(cellPtr);
  }
}