From ce1d5e2cb25c596565a6928ee41c440e4d03e6c4 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Mon, 6 Nov 2023 18:49:29 -0500 Subject: [PATCH] Some more refactoring --- src/bun/mod.ts | 4 +++- src/common/mod.ts | 32 +------------------------------- src/deno/mod.ts | 6 ++++-- src/scroll.ts | 27 ++++++++++++++++++++++++++- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/bun/mod.ts b/src/bun/mod.ts index cc7a4c2..c736946 100644 --- a/src/bun/mod.ts +++ b/src/bun/mod.ts @@ -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(); diff --git a/src/common/mod.ts b/src/common/mod.ts index c97aad4..0d239b8 100644 --- a/src/common/mod.ts +++ b/src/common/mod.ts @@ -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; -} \ No newline at end of file diff --git a/src/deno/mod.ts b/src/deno/mod.ts index b9962cb..1f58990 100644 --- a/src/deno/mod.ts +++ b/src/deno/mod.ts @@ -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(); }; } diff --git a/src/scroll.ts b/src/scroll.ts index e4c78a1..2fec6d5 100644 --- a/src/scroll.ts +++ b/src/scroll.ts @@ -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