diff --git a/src/index.ts b/src/index.ts index fccbe9c..a6d2749 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import {Lexer} from "./vm/lexer.js"; import {Parser} from "./vm/parser.js"; import {commands} from "./vm/commands/index.js"; import {Executor} from "./vm/vm.js"; -import {ConsoleDisplay, FakeClipboard, OutputManager} from "./vm/output.js"; +import {ConsoleDisplay, OutputManager, WlClipboard} from "./vm/output.js"; const lifecycle = new Lifecycle() const input = new Input() @@ -20,7 +20,7 @@ parser.subscribe(exec => log.verbose('exec', exec)) const output: OutputManager = { display: new ConsoleDisplay, - clipboard: new FakeClipboard, + clipboard: new WlClipboard, } const exec = new Executor(output, parser) diff --git a/src/vm/output.ts b/src/vm/output.ts index 824fe68..164bccf 100644 --- a/src/vm/output.ts +++ b/src/vm/output.ts @@ -1,5 +1,8 @@ import {StrRVal} from "./commands/command.js"; import {Awaitable} from "../util/types.js"; +import childProcess from "node:child_process"; +import fs from "node:fs"; +import crypto from "node:crypto"; export const getSubjectDisplay = (sub: StrRVal): string => { if ( sub.term === 'string' ) { @@ -44,6 +47,28 @@ export class FakeClipboard { } } +const tempFile = () => `/tmp/str-${crypto.randomBytes(4).readUInt32LE(0)}.txt` +export class WlClipboard { + async read(): Promise { + const tmp = tempFile() + fs.writeFileSync(tmp, '') + const proc = childProcess.spawn('sh', ['-c', `wl-paste > "${tmp}"`]) + await new Promise(res => { + proc.on('close', () => res()) + }) + return fs.readFileSync(tmp).toString('utf-8') + } + + async overwrite(sub: string): Promise { + const tmp = tempFile() + fs.writeFileSync(tmp, sub) + const proc = childProcess.spawn('sh', ['-c', `wl-copy < "${tmp}"`], { stdio: 'inherit' }) + await new Promise(res => { + proc.on('close', () => res()) + }) + } +} + export type OutputManager = { display: Display, clipboard: Clipboard,