diff --git a/mod.ts b/mod.ts index f95b385..f79446d 100644 --- a/mod.ts +++ b/mod.ts @@ -1,88 +1,98 @@ // Copyright (c) 2019 RafaƂ Pocztarski. All rights reserved. // MIT License (Expat). See: https://github.com/rsp/deno-clipboard +type OperatingSystem = typeof Deno.build.os + type Dispatch = { - [key in Deno.OperatingSystem]: Clipboard; -}; + [key in OperatingSystem]: Clipboard +} -const encoder = new TextEncoder(); -const decoder = new TextDecoder(); +const encoder = new TextEncoder() +const decoder = new TextDecoder() -export const encode = (x: string) => encoder.encode(x); -export const decode = (x: Uint8Array) => decoder.decode(x); +export const encode = (x: string) => encoder.encode(x) +export const decode = (x: Uint8Array) => decoder.decode(x) const opt: Deno.RunOptions = { - args: [], - stdin: 'piped', - stdout: 'piped', - stderr: 'piped', -}; + cmd: [], + stdin: "piped", + stdout: "piped", + stderr: "piped", +} -async function read(args: string[]): Promise { - const p = Deno.run({ ...opt, args }); - return decode(await p.output()); +async function read(cmd: string[]): Promise { + const p = Deno.run({ ...opt, cmd }) + return decode(await p.output()) } -async function write(args: string[], data: string): Promise { - const p = Deno.run({ ...opt, args }); - await p.stdin.write(encode(data)); - p.stdin.close(); - await p.status(); +async function write(cmd: string[], data: string): Promise { + const p = Deno.run({ ...opt, cmd }) + await p.stdin?.write(encode(data)) + p.stdin?.close() + await p.status() } const linux: Clipboard = { - os: 'linux', + os: "linux", async readText() { // return read(['xclip', '-selection', 'clipboard', '-o']); - return read(['xsel', '-b', '-o']); + return read(["xsel", "-b", "-o"]) }, async writeText(data) { // return write(['xclip', '-selection', 'clipboard'], data); - return write(['xsel', '-b', '-i'], data); + return write(["xsel", "-b", "-i"], data) }, -}; +} const mac: Clipboard = { - os: 'mac', + os: "darwin", async readText() { - return read(['pbpaste']); + return read(["pbpaste"]) }, async writeText(data) { - return write(['pbcopy'], data); + return write(["pbcopy"], data) }, -}; +} const win: Clipboard = { - os: 'win', + os: "windows", async readText() { - const data = await read(['powershell', '-noprofile', '-command', 'Get-Clipboard']); - return data.replace(/\r/g, '').replace(/\n$/, ''); + const data = await read([ + "powershell", + "-noprofile", + "-command", + "Get-Clipboard", + ]) + return data.replace(/\r/g, "").replace(/\n$/, "") }, async writeText(data) { - return write(['powershell', '-noprofile', '-command', '$input|Set-Clipboard'], data); + return write( + ["powershell", "-noprofile", "-command", "$input|Set-Clipboard"], + data, + ) }, -}; +} const dispatch: Dispatch = { linux, - mac, - win, -}; + darwin: mac, + windows: win, +} class Clipboard { - os: Deno.OperatingSystem; - constructor(os: Deno.OperatingSystem) { + os: OperatingSystem + constructor(os: OperatingSystem) { if (!dispatch[os]) { - throw new Error(`Clipboard: unsupported OS: ${os}`); + throw new Error(`Clipboard: unsupported OS: ${os}`) } - this.os = os; + this.os = os } async readText(): Promise { - return dispatch[this.os].readText(); + return dispatch[this.os].readText() } async writeText(data: string): Promise { - return dispatch[this.os].writeText(data); + return dispatch[this.os].writeText(data) } } -export const clipboard = new Clipboard(Deno.build.os); +export const clipboard = new Clipboard(Deno.build.os)