forked from rsp/deno-clipboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix proposed by @kingdaro in rsp#3
- Loading branch information
Showing
1 changed file
with
52 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -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<string> { | ||
const p = Deno.run({ ...opt, args }); | ||
return decode(await p.output()); | ||
async function read(cmd: string[]): Promise<string> { | ||
const p = Deno.run({ ...opt, cmd }) | ||
return decode(await p.output()) | ||
} | ||
|
||
async function write(args: string[], data: string): Promise<void> { | ||
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<void> { | ||
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<string> { | ||
return dispatch[this.os].readText(); | ||
return dispatch[this.os].readText() | ||
} | ||
async writeText(data: string): Promise<void> { | ||
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) |