Wrap runtime stdin loops in a generator

This commit is contained in:
Timothy Warren 2023-11-06 15:36:41 -05:00
parent 19e44261a4
commit 71ad718b6d
6 changed files with 60 additions and 50 deletions

View File

@ -2,34 +2,14 @@
* The main entrypoint when using Bun as the runtime
*/
import { getTermios } from '../common/termios';
import {ctrl_key, is_control} from "../common/strings";
import { getTermios } from "../common/termios.ts";
export * from './terminal_io.ts';
export async function main(): Promise<number> {
export async function init() {
const t = await getTermios();
t.enableRawMode();
const decoder = new TextDecoder();
for await (const chunk of Bun.stdin.stream()) {
const char = String(decoder.decode(chunk));
if (char === ctrl_key('q')) {
t.disableRawMode();
return 0;
}
if (is_control(char)) {
console.log(char.codePointAt(0) + '\r');
} else {
console.log(`${char} ('${char.codePointAt(0)}')\r`);
}
}
process.on('exit', (code) => {
console.log(`Process exited with code ${code}`);
process.on('exit', () => {
console.info('Disabling raw mode');
t.disableRawMode();
});
return -1;
})
}

View File

@ -0,0 +1,8 @@
/**
* Wrap the runtime-specific hook into stdin
*/
export async function* inputLoop() {
for await (const chunk of Bun.stdin.stream()) {
yield chunk;
}
}

2
src/common/ansi.ts Normal file
View File

@ -0,0 +1,2 @@
export enum Ansi {
}

View File

@ -1,10 +1,35 @@
import { importForRuntime } from './runtime.ts';
import { getTermios } from './termios.ts';
import { ctrl_key, is_control } from './strings.ts';
export * from './runtime.ts';
export * from './strings.ts';
export type { ITestBase } from './test_base.ts';
export async function main() {
const { main } = await importForRuntime('./mod.ts');
await main();
}
const t = await getTermios();
t.enableRawMode();
const { inputLoop, init } = await importForRuntime('mod.ts');
// Set up handlers to disable raw mode for each runtime
await init();
for await (const chunk of inputLoop()) {
const decoder = new TextDecoder();
const char = String(decoder.decode(chunk));
if (char === ctrl_key('q')) {
t.disableRawMode();
return 0;
}
if (is_control(char)) {
console.log(char.codePointAt(0) + '\r');
} else {
console.log(`${char} ('${char.codePointAt(0)}')\r`);
}
}
t.disableRawMode();
return -1;
}

View File

@ -1,28 +1,15 @@
/**
* The main entrypoint when using Deno as the runtime
*/
import { getTermios } from '../common/termios.ts';
import { ctrl_key, is_control } from '../common/strings.ts';
import {getTermios} from "../common/termios.ts";
export async function main(): Promise<number> {
export * from './terminal_io.ts';
export async function init() {
const t = await getTermios();
t.enableRawMode();
const decoder = new TextDecoder();
for await (const chunk of Deno.stdin.readable) {
const char = String(decoder.decode(chunk));
if (char === ctrl_key('q')) {
t.disableRawMode();
return 0;
}
if (is_control(char)) {
console.log(char.codePointAt(0) + '\r');
} else {
console.log(`${char} ('${char.codePointAt(0)}')\r`);
}
}
return -1;
globalThis.onbeforeunload = (): void => {
console.log('Disabling raw mode');
t.disableRawMode();
};
}

View File

@ -0,0 +1,8 @@
/**
* Wrap the runtime-specific hook into stdin
*/
export async function* inputLoop() {
for await (const chunk of Deno.stdin.readable) {
yield chunk;
}
}