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

Update for deno v1.0.0 #3

Open
itsMapleLeaf opened this issue May 18, 2020 · 1 comment · May be fixed by #5
Open

Update for deno v1.0.0 #3

itsMapleLeaf opened this issue May 18, 2020 · 1 comment · May be fixed by #5

Comments

@itsMapleLeaf
Copy link

The module doesn't seem to work with the latest version. Mainly, the Deno.OperatingSystem enum used by the code doesn't exist. I don't have the time at the moment to make a PR, but I copied the module to a local file and made the appropriate changes to get it running:

type OperatingSystem = typeof Deno.build.os

type Dispatch = {
  [key in OperatingSystem]: Clipboard
}

const encoder = new TextEncoder()
const decoder = new TextDecoder()

export const encode = (x: string) => encoder.encode(x)
export const decode = (x: Uint8Array) => decoder.decode(x)

const opt: Deno.RunOptions = {
  cmd: [],
  stdin: "piped",
  stdout: "piped",
  stderr: "piped",
}

async function read(cmd: string[]): Promise<string> {
  const p = Deno.run({ ...opt, cmd })
  return decode(await p.output())
}

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",
  async readText() {
    // return read(['xclip', '-selection', 'clipboard', '-o']);
    return read(["xsel", "-b", "-o"])
  },
  async writeText(data) {
    // return write(['xclip', '-selection', 'clipboard'], data);
    return write(["xsel", "-b", "-i"], data)
  },
}

const mac: Clipboard = {
  os: "darwin",
  async readText() {
    return read(["pbpaste"])
  },
  async writeText(data) {
    return write(["pbcopy"], data)
  },
}

const win: Clipboard = {
  os: "windows",
  async readText() {
    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,
    )
  },
}

const dispatch: Dispatch = {
  linux,
  darwin: mac,
  windows: win,
}

class Clipboard {
  os: OperatingSystem
  constructor(os: OperatingSystem) {
    if (!dispatch[os]) {
      throw new Error(`Clipboard: unsupported OS: ${os}`)
    }
    this.os = os
  }
  async readText(): Promise<string> {
    return dispatch[this.os].readText()
  }
  async writeText(data: string): Promise<void> {
    return dispatch[this.os].writeText(data)
  }
}

export const clipboard = new Clipboard(Deno.build.os)

Only tested on Windows, but the changes are only type related, so I don't expect breakages

Nisgrak added a commit to Nisgrak/deno-clipboard that referenced this issue May 23, 2020
Fix proposed by @kingdaro in rsp#3
@jsejcksn jsejcksn linked a pull request Jun 11, 2020 that will close this issue
@Nisgrak
Copy link

Nisgrak commented Jun 26, 2020

It doesn't work in wsl

xsel: Can't open display: (null)
: Inappropriate ioctl for device
error: Uncaught Error: There was a problem writing to the clipboard
    if (!success) throw new Error(errMsg.genericWrite);
                        ^
    at Object.writeText (https://raw.githubusercontent.com/jsejcksn/deno-clipboard/master/mod.ts:69:25)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants