copyToClipboard function

Future<bool> copyToClipboard(
  1. String text, {
  2. required void onStatus(
    1. String message
    ),
  3. Clipboard clipboard = const Clipboard(),
})

Writes text to the clipboard and reports what happened via onStatus.

PLAN.md §5.2 offers two options for clipboard hygiene: restore the previous contents ~150ms later, or document the overwrite loudly. The first would undo the tool's entire purpose here — the whole point is that the developer's next ⌘V, whenever they get to it, pastes what they just selected — so this always takes the second option instead.

Implementation

Future<bool> copyToClipboard(
  String text, {
  required void Function(String message) onStatus,
  Clipboard clipboard = const Clipboard(),
}) async {
  final String? previous = await clipboard.read();
  final bool ok = await clipboard.write(text);
  if (!ok) {
    onStatus('Could not copy to clipboard — is pbcopy/xclip/clip available on this system?');
    return false;
  }
  onStatus(
    previous == null || previous.trim().isEmpty
        ? 'Copied to clipboard.'
        : 'Copied to clipboard (replaced whatever was there before).',
  );
  return true;
}