Wrap runtime stdin loops in a generator
This commit is contained in:
parent
19e44261a4
commit
71ad718b6d
@ -2,34 +2,14 @@
|
|||||||
* The main entrypoint when using Bun as the runtime
|
* The main entrypoint when using Bun as the runtime
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { getTermios } from '../common/termios';
|
import { getTermios } from "../common/termios.ts";
|
||||||
import {ctrl_key, is_control} from "../common/strings";
|
export * from './terminal_io.ts';
|
||||||
|
|
||||||
export async function main(): Promise<number> {
|
export async function init() {
|
||||||
const t = await getTermios();
|
const t = await getTermios();
|
||||||
t.enableRawMode();
|
|
||||||
|
|
||||||
const decoder = new TextDecoder();
|
process.on('exit', () => {
|
||||||
for await (const chunk of Bun.stdin.stream()) {
|
console.info('Disabling raw mode');
|
||||||
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}`);
|
|
||||||
t.disableRawMode();
|
t.disableRawMode();
|
||||||
});
|
})
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
@ -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
2
src/common/ansi.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export enum Ansi {
|
||||||
|
}
|
@ -1,10 +1,35 @@
|
|||||||
import { importForRuntime } from './runtime.ts';
|
import { importForRuntime } from './runtime.ts';
|
||||||
|
import { getTermios } from './termios.ts';
|
||||||
|
import { ctrl_key, is_control } from './strings.ts';
|
||||||
|
|
||||||
export * from './runtime.ts';
|
export * from './runtime.ts';
|
||||||
export * from './strings.ts';
|
export * from './strings.ts';
|
||||||
export type { ITestBase } from './test_base.ts';
|
export type { ITestBase } from './test_base.ts';
|
||||||
|
|
||||||
export async function main() {
|
export async function main() {
|
||||||
const { main } = await importForRuntime('./mod.ts');
|
const t = await getTermios();
|
||||||
await main();
|
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;
|
||||||
}
|
}
|
@ -1,28 +1,15 @@
|
|||||||
/**
|
/**
|
||||||
* The main entrypoint when using Deno as the runtime
|
* The main entrypoint when using Deno as the runtime
|
||||||
*/
|
*/
|
||||||
import { getTermios } from '../common/termios.ts';
|
import {getTermios} from "../common/termios.ts";
|
||||||
import { ctrl_key, is_control } from '../common/strings.ts';
|
|
||||||
|
|
||||||
export async function main(): Promise<number> {
|
export * from './terminal_io.ts';
|
||||||
|
|
||||||
|
export async function init() {
|
||||||
const t = await getTermios();
|
const t = await getTermios();
|
||||||
t.enableRawMode();
|
|
||||||
|
|
||||||
const decoder = new TextDecoder();
|
globalThis.onbeforeunload = (): void => {
|
||||||
for await (const chunk of Deno.stdin.readable) {
|
console.log('Disabling raw mode');
|
||||||
const char = String(decoder.decode(chunk));
|
t.disableRawMode();
|
||||||
|
};
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user