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"; import {tempFile} from "../util/fs.js"; export const getSubjectDisplay = (sub: StrRVal): string => { if ( sub.term === 'string' ) { return sub.value } if ( sub.term === 'int' ) { return String(sub.term) } return JSON.stringify(sub.value, null, '\t') // fixme } export type Display = { showSubject(sub: StrRVal): Awaitable showRaw(str: string): Awaitable } export class ConsoleDisplay implements Display { showSubject(sub: StrRVal) { console.log(`\n---------------\n${getSubjectDisplay(sub)}\n---------------\n`) } showRaw(str: string) { console.log(str) } } export class NullDisplay implements Display { showSubject() {} showRaw() {} } export type Clipboard = { read(): Awaitable overwrite(sub: string): Awaitable } export class FakeClipboard { private val = '' read() { return this.val } overwrite(sub: string) { this.val = sub } } 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, }