scroll/src/deno/terminal_io.ts

32 lines
867 B
JavaScript
Raw Normal View History

import { ITerminalIO, ITerminalSize } from '../common/types.ts';
2023-11-08 18:07:34 -05:00
const DenoTerminalIO: ITerminalIO = {
/**
* 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) {
yield chunk;
}
},
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
},
write: 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;