Skip to content
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

Fix OperatingSystem in v1.0.0 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 52 additions & 42 deletions mod.ts
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)