end method

VtOscCommand end({
  1. int terminator = 0x07,
})

Finalizes parsing and returns a stable command snapshot.

Returns a VtOscCommand with type GhosttyOscCommandType.GHOSTTY_OSC_COMMAND_INVALID if the fed bytes did not form a valid OSC sequence.

Implementation

VtOscCommand end({int terminator = 0x07}) {
  _ensureOpen();
  if (terminator < 0 || terminator > 255) {
    throw RangeError.range(terminator, 0, 255, 'terminator');
  }

  final command = bindings.ghostty_osc_end(_handle, terminator);

  // Guard: if the native call returned a null pointer, treat as invalid.
  if (command == ffi.nullptr) {
    return const VtOscCommand(
      type: bindings.GhosttyOscCommandType.GHOSTTY_OSC_COMMAND_INVALID,
    );
  }

  final type = bindings.ghostty_osc_command_type(command);

  // Guard: don't attempt to extract data from invalid/unrecognised commands
  // — the native library may segfault if asked for data on a command that
  // doesn't carry it.
  if (type == bindings.GhosttyOscCommandType.GHOSTTY_OSC_COMMAND_INVALID) {
    return VtOscCommand(type: type);
  }

  String? windowTitle;

  // Only query the window-title data field for command types that carry it.
  if (type ==
          bindings
              .GhosttyOscCommandType
              .GHOSTTY_OSC_COMMAND_CHANGE_WINDOW_TITLE ||
      type ==
          bindings
              .GhosttyOscCommandType
              .GHOSTTY_OSC_COMMAND_CHANGE_WINDOW_ICON) {
    final out = calloc<ffi.Pointer<ffi.Char>>();
    try {
      final hasTitle = bindings.ghostty_osc_command_data(
        command,
        bindings
            .GhosttyOscCommandData
            .GHOSTTY_OSC_DATA_CHANGE_WINDOW_TITLE_STR,
        out.cast(),
      );
      final ptr = out.value;
      if (hasTitle && ptr != ffi.nullptr) {
        windowTitle = ptr.cast<Utf8>().toDartString();
      }
    } finally {
      calloc.free(out);
    }
  }

  return VtOscCommand(type: type, windowTitle: windowTitle);
}