Skip to content

Commit

Permalink
Fix OperatingSystem in v1.0.0
Browse files Browse the repository at this point in the history
Fix proposed by @kingdaro in rsp#3
  • Loading branch information
Nisgrak authored May 23, 2020
1 parent b89938f commit 2abc74c
Showing 1 changed file with 52 additions and 42 deletions.
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)

0 comments on commit 2abc74c

Please sign in to comment.