-
Notifications
You must be signed in to change notification settings - Fork 354
feat: detect image events on paste with ctrl +v or command v #520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tyrellshawn
wants to merge
9
commits into
anomalyco:main
Choose a base branch
from
tyrellshawn:detect-image-events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5aec63b
detect image events with tests
tyrellshawn ae0a0f3
add example
tyrellshawn bb0f265
Merge branch 'main' into detect-image-events
tyrellshawn bfa893e
fixes
tyrellshawn 123c5f8
Merge branch 'main' into detect-image-events
kommander ba45a97
Merge branch 'main' into detect-image-events
tyrellshawn 36d0f31
;Merge branch 'detect-image-events' of https://github.com/tyrellshawn…
tyrellshawn 331dd8f
use just the buffer
tyrellshawn da530c4
Merge branch 'main' into detect-image-events
kommander File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import { | ||
| BoxRenderable, | ||
| InputRenderable, | ||
| TextareaRenderable, | ||
| TextRenderable, | ||
| bold, | ||
| createCliRenderer, | ||
| fg, | ||
| t, | ||
| type CliRenderer, | ||
| } from "../index" | ||
| import { KeyEvent, PasteEvent } from "../lib/KeyHandler" | ||
| import { setupCommonDemoKeys } from "./lib/standalone-keys" | ||
|
|
||
| let renderer: CliRenderer | null = null | ||
| let container: BoxRenderable | null = null | ||
| let singleLineInput: InputRenderable | null = null | ||
| let multilineInput: TextareaRenderable | null = null | ||
| let logDisplay: TextRenderable | null = null | ||
| let instructions: TextRenderable | null = null | ||
| let keypressHandler: ((event: KeyEvent) => void) | null = null | ||
| let pasteHandler: ((event: PasteEvent) => void) | null = null | ||
|
|
||
| const logEntries: string[] = [] | ||
|
|
||
| function formatHexHead(buffer: Buffer): string { | ||
| if (buffer.length === 0) return "<empty>" | ||
| const hex = buffer.subarray(0, 16).toString("hex") | ||
| return hex.match(/.{1,2}/g)?.join(" ") ?? hex | ||
| } | ||
|
|
||
| function formatTextPreview(buffer: Buffer): string { | ||
| const text = buffer.toString("utf8") | ||
| const normalized = text.replace(/\r/g, "").replace(/\n/g, " ⏎ ") | ||
| return normalized.length > 80 ? `${normalized.slice(0, 80)}…` : normalized | ||
| } | ||
|
|
||
| function updateLog(event: PasteEvent): void { | ||
| const entry = `len=${event.data.length} head=${formatHexHead(event.data)} preview=${formatTextPreview(event.data)}` | ||
|
|
||
| logEntries.unshift(entry) | ||
| logEntries.splice(12) | ||
|
|
||
| if (logDisplay) { | ||
| logDisplay.content = t`${logEntries.join("\n")}` | ||
| } | ||
|
|
||
| const text = event.text ?? event.data.toString("utf8") | ||
| if (singleLineInput) { | ||
| singleLineInput.value = text | ||
| } | ||
|
|
||
| if (multilineInput) { | ||
| multilineInput.value = text | ||
| } | ||
| } | ||
|
|
||
| function createLayout(rendererInstance: CliRenderer): void { | ||
| container = new BoxRenderable(rendererInstance, { | ||
| id: "paste-demo-root", | ||
| flexDirection: "column", | ||
| width: "100%", | ||
| height: "100%", | ||
| padding: 2, | ||
| gap: 1, | ||
| }) | ||
|
|
||
| instructions = new TextRenderable(rendererInstance, { | ||
| id: "paste-demo-instructions", | ||
| width: 100, | ||
| height: 4, | ||
| content: t`${bold("Paste Demo")} | ||
| - Paste into the single-line input or the textarea | ||
| - Logs show file type, byte length, first 16 bytes, and text preview | ||
| - Press q to quit, Ctrl+C to exit`, | ||
| }) | ||
|
|
||
| singleLineInput = new InputRenderable(rendererInstance, { | ||
| id: "paste-demo-input", | ||
| width: 70, | ||
| height: 3, | ||
| placeholder: "Paste here (single line)", | ||
| }) | ||
|
|
||
| multilineInput = new TextareaRenderable(rendererInstance, { | ||
| id: "paste-demo-textarea", | ||
| width: 70, | ||
| height: 6, | ||
| placeholder: "Or paste here (textarea)", | ||
| }) | ||
|
|
||
| logDisplay = new TextRenderable(rendererInstance, { | ||
| id: "paste-demo-log", | ||
| width: 100, | ||
| height: 12, | ||
| fg: fg("#A0FFA0"), | ||
| content: t`Waiting for paste events…`, | ||
| }) | ||
|
|
||
| container.add(instructions) | ||
| container.add(singleLineInput) | ||
| container.add(multilineInput) | ||
| container.add(logDisplay) | ||
|
|
||
| rendererInstance.root.add(container) | ||
| } | ||
|
|
||
| export function run(rendererInstance: CliRenderer): void { | ||
| renderer = rendererInstance | ||
| renderer.setBackgroundColor("#0e1116") | ||
|
|
||
| createLayout(rendererInstance) | ||
|
|
||
| keypressHandler = (event: KeyEvent) => { | ||
| if (event.name === "q") { | ||
| renderer?.destroy() | ||
| return | ||
| } | ||
| } | ||
|
|
||
| pasteHandler = (event: PasteEvent) => { | ||
| updateLog(event) | ||
| } | ||
|
|
||
| renderer.keyInput.on("keypress", keypressHandler) | ||
| renderer.keyInput.on("paste", pasteHandler) | ||
| renderer.requestRender() | ||
| } | ||
|
|
||
| export function destroy(rendererInstance: CliRenderer): void { | ||
| rendererInstance.clearFrameCallbacks() | ||
|
|
||
| if (keypressHandler) { | ||
| rendererInstance.keyInput.off("keypress", keypressHandler) | ||
| keypressHandler = null | ||
| } | ||
|
|
||
| if (pasteHandler) { | ||
| rendererInstance.keyInput.off("paste", pasteHandler) | ||
| pasteHandler = null | ||
| } | ||
|
|
||
| if (container) { | ||
| rendererInstance.root.remove("paste-demo-root") | ||
| container = null | ||
| } | ||
|
|
||
| singleLineInput = null | ||
| multilineInput = null | ||
| logDisplay = null | ||
| instructions = null | ||
| logEntries.length = 0 | ||
| } | ||
|
|
||
| if (import.meta.main) { | ||
| const rendererInstance = await createCliRenderer({ | ||
| exitOnCtrlC: true, | ||
| }) | ||
|
|
||
| run(rendererInstance) | ||
| setupCommonDemoKeys(rendererInstance) | ||
| rendererInstance.start() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { InternalKeyHandler } from "./KeyHandler" | ||
|
|
||
| test("processPaste emits buffer for image data", () => { | ||
| const handler = new InternalKeyHandler() | ||
| const pngBytes = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00]) | ||
|
|
||
| let received: any | ||
| handler.on("paste", (event) => { | ||
| received = event | ||
| }) | ||
|
|
||
| handler.processPaste(pngBytes) | ||
|
|
||
| expect(Buffer.isBuffer(received?.data)).toBe(true) | ||
| expect(received?.data?.equals?.(pngBytes)).toBe(true) | ||
| }) | ||
|
|
||
| test("processPaste emits buffer for text data", () => { | ||
| const handler = new InternalKeyHandler() | ||
|
|
||
| let receivedData: Buffer | undefined | ||
| handler.on("paste", (event) => { | ||
| receivedData = event.data | ||
| }) | ||
|
|
||
| handler.processPaste("plain text") | ||
|
|
||
| expect(Buffer.isBuffer(receivedData)).toBe(true) | ||
| expect(receivedData?.toString()).toBe("plain text") | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { expect, test } from "bun:test" | ||
| import { detectPasteFileType } from "./paste-detect" | ||
|
|
||
| test("detectPasteFileType identifies common image signatures", () => { | ||
| expect(detectPasteFileType(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))).toBe("image/png") | ||
| expect(detectPasteFileType(Buffer.from([0xff, 0xd8, 0xff, 0x00]))).toBe("image/jpeg") | ||
| expect(detectPasteFileType(Buffer.from("GIF89a"))).toBe("image/gif") | ||
| expect(detectPasteFileType(Buffer.from("RIFF1234WEBP"))).toBe("image/webp") | ||
| expect(detectPasteFileType(Buffer.from([0x42, 0x4d, 0x00, 0x00]))).toBe("image/bmp") | ||
| expect(detectPasteFileType(Buffer.from([0x00, 0x00, 0x01, 0x00, 0x00]))).toBe("image/x-icon") | ||
| expect(detectPasteFileType(Buffer.from('<?xml version="1.0"?><svg></svg>'))).toBe("image/svg+xml") | ||
| }) | ||
|
|
||
| test("detectPasteFileType returns undefined for non-image data", () => { | ||
| expect(detectPasteFileType(Buffer.from("plain text"))).toBeUndefined() | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]) | ||
| const JPEG_SIGNATURE = Buffer.from([0xff, 0xd8, 0xff]) | ||
| const GIF_SIGNATURE = Buffer.from([0x47, 0x49, 0x46, 0x38]) | ||
| const WEBP_RIFF = Buffer.from("RIFF") | ||
| const WEBP_WEBP = Buffer.from("WEBP") | ||
| const BMP_SIGNATURE = Buffer.from([0x42, 0x4d]) | ||
| const ICO_SIGNATURE = Buffer.from([0x00, 0x00, 0x01, 0x00]) | ||
|
|
||
| function matchesSignature(buffer: Buffer, signature: Buffer, offset: number = 0): boolean { | ||
| if (buffer.length < signature.length + offset) return false | ||
| for (let i = 0; i < signature.length; i++) { | ||
| if (buffer[i + offset] !== signature[i]) { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| function detectSvg(buffer: Buffer): boolean { | ||
| if (buffer.length === 0) return false | ||
| const sample = buffer.slice(0, 512).toString("utf8").trimStart().toLowerCase() | ||
| return sample.startsWith("<svg") || sample.startsWith("<?xml") | ||
| } | ||
|
|
||
| export function detectPasteFileType(buffer: Buffer): string | undefined { | ||
| if (matchesSignature(buffer, PNG_SIGNATURE)) return "image/png" | ||
| if (matchesSignature(buffer, JPEG_SIGNATURE)) return "image/jpeg" | ||
| if (matchesSignature(buffer, GIF_SIGNATURE)) return "image/gif" | ||
| if (matchesSignature(buffer, BMP_SIGNATURE)) return "image/bmp" | ||
| if (matchesSignature(buffer, ICO_SIGNATURE)) return "image/x-icon" | ||
|
|
||
| if (matchesSignature(buffer, WEBP_RIFF) && matchesSignature(buffer, WEBP_WEBP, 8)) { | ||
| return "image/webp" | ||
| } | ||
|
|
||
| if (detectSvg(buffer)) return "image/svg+xml" | ||
|
|
||
| return undefined | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if the PasteEvent should maybe only have
dataand we droptextas for large pastes like images it would always toString duplicate the data, while consumers of the paste event can do the toString when it is actually text content and not a file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree but how would we define the data interface. As long as we know it can do toString we are good? I feel like having a getFileType method in the interface would be good too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just as
data: Buffer? The PasteEvent has the optional fileType.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated