screenshotClipboard.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { mkdir, unlink, writeFile } from 'fs/promises'
  2. import { tmpdir } from 'os'
  3. import { join } from 'path'
  4. import { type AnsiToPngOptions, ansiToPng } from './ansiToPng.js'
  5. import { execFileNoThrowWithCwd } from './execFileNoThrow.js'
  6. import { logError } from './log.js'
  7. import { getPlatform } from './platform.js'
  8. /**
  9. * Copies an image (from ANSI text) to the system clipboard.
  10. * Supports macOS, Linux (with xclip/xsel), and Windows.
  11. *
  12. * Pure-TS pipeline: ANSI text → bitmap-font render → PNG encode. No WASM,
  13. * no system fonts, so this works in every build (native and JS).
  14. */
  15. export async function copyAnsiToClipboard(
  16. ansiText: string,
  17. options?: AnsiToPngOptions,
  18. ): Promise<{ success: boolean; message: string }> {
  19. try {
  20. const tempDir = join(tmpdir(), 'claude-code-screenshots')
  21. await mkdir(tempDir, { recursive: true })
  22. const pngPath = join(tempDir, `screenshot-${Date.now()}.png`)
  23. const pngBuffer = ansiToPng(ansiText, options)
  24. await writeFile(pngPath, pngBuffer)
  25. const result = await copyPngToClipboard(pngPath)
  26. try {
  27. await unlink(pngPath)
  28. } catch {
  29. // Ignore cleanup errors
  30. }
  31. return result
  32. } catch (error) {
  33. logError(error)
  34. return {
  35. success: false,
  36. message: `Failed to copy screenshot: ${error instanceof Error ? error.message : 'Unknown error'}`,
  37. }
  38. }
  39. }
  40. async function copyPngToClipboard(
  41. pngPath: string,
  42. ): Promise<{ success: boolean; message: string }> {
  43. const platform = getPlatform()
  44. if (platform === 'macos') {
  45. // macOS: Use osascript to copy PNG to clipboard
  46. // Escape backslashes and double quotes for AppleScript string
  47. const escapedPath = pngPath.replace(/\\/g, '\\\\').replace(/"/g, '\\"')
  48. const script = `set the clipboard to (read (POSIX file "${escapedPath}") as «class PNGf»)`
  49. const result = await execFileNoThrowWithCwd('osascript', ['-e', script], {
  50. timeout: 5000,
  51. })
  52. if (result.code === 0) {
  53. return { success: true, message: 'Screenshot copied to clipboard' }
  54. }
  55. return {
  56. success: false,
  57. message: `Failed to copy to clipboard: ${result.stderr}`,
  58. }
  59. }
  60. if (platform === 'linux') {
  61. // Linux: Try xclip first, then xsel
  62. const xclipResult = await execFileNoThrowWithCwd(
  63. 'xclip',
  64. ['-selection', 'clipboard', '-t', 'image/png', '-i', pngPath],
  65. { timeout: 5000 },
  66. )
  67. if (xclipResult.code === 0) {
  68. return { success: true, message: 'Screenshot copied to clipboard' }
  69. }
  70. // Try xsel as fallback
  71. const xselResult = await execFileNoThrowWithCwd(
  72. 'xsel',
  73. ['--clipboard', '--input', '--type', 'image/png'],
  74. { timeout: 5000 },
  75. )
  76. if (xselResult.code === 0) {
  77. return { success: true, message: 'Screenshot copied to clipboard' }
  78. }
  79. return {
  80. success: false,
  81. message:
  82. 'Failed to copy to clipboard. Please install xclip or xsel: sudo apt install xclip',
  83. }
  84. }
  85. if (platform === 'windows') {
  86. // Windows: Use PowerShell to copy image to clipboard
  87. const psScript = `Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Clipboard]::SetImage([System.Drawing.Image]::FromFile('${pngPath.replace(/'/g, "''")}'))`
  88. const result = await execFileNoThrowWithCwd(
  89. 'powershell',
  90. ['-NoProfile', '-Command', psScript],
  91. { timeout: 5000 },
  92. )
  93. if (result.code === 0) {
  94. return { success: true, message: 'Screenshot copied to clipboard' }
  95. }
  96. return {
  97. success: false,
  98. message: `Failed to copy to clipboard: ${result.stderr}`,
  99. }
  100. }
  101. return {
  102. success: false,
  103. message: `Screenshot to clipboard is not supported on ${platform}`,
  104. }
  105. }