2023-11-24 08:31:51 -05:00
|
|
|
import { ITerminal, ITerminalSize, readKey } from '../common/runtime.ts';
|
2023-11-08 15:53:14 -05:00
|
|
|
|
2023-11-13 15:33:56 -05:00
|
|
|
const DenoTerminalIO: ITerminal = {
|
|
|
|
argv: Deno.args,
|
2023-11-27 15:05:48 -05:00
|
|
|
inputLoop: async function* (): AsyncGenerator<Uint8Array, null> {
|
2023-11-29 14:55:57 -05:00
|
|
|
yield (await DenoTerminalIO.readStdinRaw()) ?? new Uint8Array(0);
|
2023-11-27 15:05:48 -05:00
|
|
|
|
|
|
|
return null;
|
2023-11-08 18:07:34 -05:00
|
|
|
},
|
2023-11-10 18:22:09 -05:00
|
|
|
getTerminalSize: function getSize(): Promise<ITerminalSize> {
|
2023-11-08 18:07:34 -05:00
|
|
|
const size: { rows: number; columns: number } = Deno.consoleSize();
|
2023-11-08 17:02:59 -05:00
|
|
|
|
2023-11-10 18:22:09 -05:00
|
|
|
return Promise.resolve({
|
2023-11-08 18:07:34 -05:00
|
|
|
rows: size.rows,
|
|
|
|
cols: size.columns,
|
2023-11-10 18:22:09 -05:00
|
|
|
});
|
2023-11-08 18:07:34 -05:00
|
|
|
},
|
2023-11-27 15:05:48 -05:00
|
|
|
readStdin: async function (): Promise<string | null> {
|
|
|
|
const raw = await DenoTerminalIO.readStdinRaw();
|
|
|
|
return readKey(raw ?? new Uint8Array(0));
|
|
|
|
},
|
|
|
|
readStdinRaw: async function (): Promise<Uint8Array | null> {
|
|
|
|
const reader = Deno.stdin.readable.getReader();
|
|
|
|
const chunk = await reader.read();
|
|
|
|
const res = chunk?.value;
|
|
|
|
reader.releaseLock();
|
|
|
|
return res ?? null;
|
2023-11-27 11:07:26 -05:00
|
|
|
},
|
2023-11-29 14:55:57 -05:00
|
|
|
writeStdout: async function (s: string): Promise<void> {
|
2023-11-10 18:22:09 -05:00
|
|
|
const buffer: Uint8Array = new TextEncoder().encode(s);
|
|
|
|
const stdout: WritableStream<Uint8Array> = Deno.stdout.writable;
|
2023-11-08 17:02:59 -05:00
|
|
|
|
2023-11-10 18:22:09 -05:00
|
|
|
const writer: WritableStreamDefaultWriter<Uint8Array> = stdout.getWriter();
|
|
|
|
await writer.ready;
|
|
|
|
await writer.write(buffer);
|
|
|
|
writer.releaseLock();
|
2023-11-08 18:07:34 -05:00
|
|
|
},
|
2023-11-08 15:53:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export default DenoTerminalIO;
|