scroll/src/deno/terminal_io.ts

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-11-24 08:31:51 -05:00
import { ITerminal, ITerminalSize, readKey } from '../common/runtime.ts';
2023-11-13 15:33:56 -05:00
const DenoTerminalIO: ITerminal = {
argv: Deno.args,
inputLoop: async function* (): AsyncGenerator<Uint8Array, null> {
2023-11-29 14:55:57 -05:00
yield (await DenoTerminalIO.readStdinRaw()) ?? new Uint8Array(0);
return null;
2023-11-08 18:07:34 -05:00
},
getTerminalSize: function getSize(): Promise<ITerminalSize> {
2023-11-08 18:07:34 -05:00
const size: { rows: number; columns: number } = Deno.consoleSize();
return Promise.resolve({
2023-11-08 18:07:34 -05:00
rows: size.rows,
cols: size.columns,
});
2023-11-08 18:07:34 -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> {
const buffer: Uint8Array = new TextEncoder().encode(s);
const stdout: WritableStream<Uint8Array> = Deno.stdout.writable;
const writer: WritableStreamDefaultWriter<Uint8Array> = stdout.getWriter();
await writer.ready;
await writer.write(buffer);
writer.releaseLock();
2023-11-08 18:07:34 -05:00
},
};
export default DenoTerminalIO;