copyToClipboard function
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;
}