Some more refactoring

This commit is contained in:
Timothy Warren 2023-11-06 18:49:29 -05:00
parent 71ad718b6d
commit ce1d5e2cb2
4 changed files with 34 additions and 35 deletions

View File

@ -2,12 +2,14 @@
* The main entrypoint when using Bun as the runtime
*/
import { getTermios } from "../common/termios.ts";
import { getTermios } from "../common/mod.ts";
export * from './terminal_io.ts';
export async function init() {
const t = await getTermios();
t.enableRawMode();
process.on('exit', () => {
console.info('Disabling raw mode');
t.disableRawMode();

View File

@ -1,35 +1,5 @@
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 * from './termios.ts';
export type { ITestBase } from './test_base.ts';
export async function 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,15 +1,17 @@
/**
* The main entrypoint when using Deno as the runtime
*/
import {getTermios} from "../common/termios.ts";
import { getTermios } from "../common/mod.ts";
export * from './terminal_io.ts';
export async function init() {
const t = await getTermios();
t.enableRawMode();
globalThis.onbeforeunload = (): void => {
console.log('Disabling raw mode');
console.info('Disabling raw mode');
t.disableRawMode();
};
}

View File

@ -1,7 +1,32 @@
/**
* The starting point for running scroll
*/
import { main } from './common/mod.ts';
import { ctrl_key, importForRuntime, is_control } from './common/mod.ts';
async function main() {
const { inputLoop, init } = await importForRuntime('mod.ts');
// Set up handlers to enable/disable raw mode for each runtime
await init();
// The main event loop
for await (const chunk of inputLoop()) {
const decoder = new TextDecoder();
const char = String(decoder.decode(chunk));
if (char === ctrl_key('q')) {
return 0;
}
if (is_control(char)) {
console.log(char.codePointAt(0) + '\r');
} else {
console.log(`${char} ('${char.codePointAt(0)}')\r`);
}
}
return -1;
}
/**
* Start the event loop