diff --git a/src/common/mod.ts b/src/common/mod.ts index 0d239b8..90720ac 100644 --- a/src/common/mod.ts +++ b/src/common/mod.ts @@ -1,5 +1,39 @@ +import { importForRuntime } from "./runtime"; +import { ctrl_key, is_control } from "./strings"; + export * from './runtime.ts'; export * from './strings.ts'; export * from './termios.ts'; export type { ITestBase } from './test_base.ts'; +const decoder = new TextDecoder(); + +export function readKey(chunk): string { + const char = String(decoder.decode(chunk)) + + return char; +} + +export 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 char = readKey(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; +} \ No newline at end of file diff --git a/src/common/terminal_io.ts b/src/common/terminal_io.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/common/termios.ts b/src/common/termios.ts index f71d173..5d5a3d2 100644 --- a/src/common/termios.ts +++ b/src/common/termios.ts @@ -39,7 +39,7 @@ export const getTermios = async () => { await importForRuntime('ffi'); /** - * Implementation to toggle raw mode with Bun runtime + * Implementation to toggle raw mode */ class Termios implements ITermios { /** diff --git a/src/scroll.ts b/src/scroll.ts index 2fec6d5..e4c78a1 100644 --- a/src/scroll.ts +++ b/src/scroll.ts @@ -1,32 +1,7 @@ /** * The starting point for running scroll */ -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; -} +import { main } from './common/mod.ts'; /** * Start the event loop