scroll/src/deno/terminal_io.ts

38 lines
1.0 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,
2023-11-08 18:07:34 -05:00
/**
* Wrap the runtime-specific hook into stdin
*/
inputLoop: async function* inputLoop() {
2023-11-08 18:07:34 -05:00
for await (const chunk of Deno.stdin.readable) {
2023-11-24 08:31:51 -05:00
yield readKey(chunk);
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
},
2023-11-27 11:07:26 -05:00
readStdin: async function (): Promise<string> {
const gen = DenoTerminalIO.inputLoop();
const chunk = await gen.next();
return chunk.value!;
},
2023-11-13 15:33:56 -05:00
writeStdout: async function write(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;