-
-
Save scarf005/31a24dd5dc8587448598f44088f11bbc to your computer and use it in GitHub Desktop.
tiny, fast clipboard util
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
#!/usr/bin/env -S deno run --allow-run --allow-read --no-config --no-check --no-npm | |
import clipboard from "https://deno.land/x/[email protected]/mod.ts" | |
import { readAll } from "https://deno.land/[email protected]/streams/mod.ts" | |
const filename = Deno.args[0] | |
if (filename) { | |
const text = await Deno.readTextFile(filename) | |
await clipboard.writeText(text) | |
} else if (!Deno.isatty(Deno.stdin.rid)) { | |
const text = new TextDecoder().decode(await readAll(Deno.stdin)) | |
await clipboard.writeText(text) | |
} else { | |
const text = await clipboard.readText() | |
console.log(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
#!/usr/bin/env -S deno run --allow-run --allow-read --no-config --no-check --no-npm | |
type SupportedOS = Extract<typeof Deno.build.os, "linux" | "darwin" | "windows"> | |
type Clipboard = { name: string; args?: string[] } | |
const writeCmds: Record<string, Clipboard> = { | |
linux: { name: "xclip", args: ["-selection", "clipboard"] }, | |
windows: { name: "powershell", args: ["-Command", "Set-Clipboard"] }, | |
darwin: { name: "pbcopy" }, | |
} satisfies Record<SupportedOS, Clipboard> | |
const readCmds: Record<string, Clipboard> = { | |
linux: { name: "xclip", args: ["-selection", "clipboard", "-o"] }, | |
windows: { name: "powershell", args: ["-Command", "Get-Clipboard"] }, | |
darwin: { name: "pbpaste" }, | |
} satisfies Record<SupportedOS, Clipboard> | |
const writeCmd = () => { | |
const { name, args } = writeCmds[Deno.build.os] | |
return new Deno.Command(name, { args, stdin: "piped" }) | |
} | |
const readCmd = () => { | |
const { name, args } = readCmds[Deno.build.os] | |
return new Deno.Command(name, { args, stdout: "piped" }) | |
} | |
const [filename] = Deno.args | |
if (filename) { | |
await using clip = writeCmd().spawn() | |
await Deno | |
.readFile(filename) | |
.then((arr) => new Blob([arr]).stream().pipeTo(clip.stdin)) | |
} else if (Deno.isatty(Deno.stdin.rid)) { | |
await using clip = readCmd().spawn() | |
await clip.stdout.pipeTo(Deno.stdout.writable) | |
} else { | |
await using clip = writeCmd().spawn() | |
await Deno.stdin.readable.pipeTo(clip.stdin) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment