From 301196352a4706b97b6c23ad72e572fb1e11676d Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 16 Nov 2023 13:00:02 -0500 Subject: [PATCH] Misc refactoring --- src/bun/file_io.ts | 7 +++++-- src/bun/mod.ts | 3 +-- src/common/main.ts | 10 ++++------ src/common/runtime.ts | 23 ++++++++++++++++++----- src/common/termios.ts | 14 +++----------- src/common/utils.ts | 2 ++ src/deno/file_io.ts | 4 ++++ src/deno/mod.ts | 2 +- 8 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/bun/file_io.ts b/src/bun/file_io.ts index 6415ddd..068ec52 100644 --- a/src/bun/file_io.ts +++ b/src/bun/file_io.ts @@ -1,6 +1,6 @@ import { IFIO } from '../common/runtime.ts'; -import { readFileSync } from 'node:fs'; +import { appendFileSync, readFileSync } from 'node:fs'; import { appendFile } from 'node:fs/promises'; const BunFileIO: IFIO = { @@ -12,7 +12,10 @@ const BunFileIO: IFIO = { return readFileSync(path).toString(); }, appendFile: async function (path: string, contents: string): Promise { - await appendFile(path, contents); + return await appendFile(path, contents); + }, + appendFileSync: function (path: string, contents: string) { + return appendFileSync(path, contents); }, }; diff --git a/src/bun/mod.ts b/src/bun/mod.ts index 57ecd0e..ab0d017 100644 --- a/src/bun/mod.ts +++ b/src/bun/mod.ts @@ -12,8 +12,7 @@ const BunRuntime: IRuntime = { file: BunFileIO, ffi: BunFFI, term: BunTerminalIO, - onEvent: (eventName: string, handler: (e: Event) => void) => - process.on(eventName, handler), + onEvent: (eventName: string, handler) => process.on(eventName, handler), onExit: (cb: () => void): void => { process.on('beforeExit', cb); process.on('exit', cb); diff --git a/src/common/main.ts b/src/common/main.ts index f7af2d7..152ecee 100644 --- a/src/common/main.ts +++ b/src/common/main.ts @@ -15,13 +15,11 @@ export async function main() { }); // Setup error handler to log to file - onEvent('error', (error: Event) => { + onEvent('error', (error) => { t.disableRawMode(); - error.preventDefault(); - error.stopPropagation(); - file.appendFile('scroll.err', JSON.stringify(error, null, 2)).then( - () => {}, - ); + if (error instanceof ErrorEvent) { + file.appendFileSync('./scroll.err', JSON.stringify(error, null, 2)); + } }); const terminalSize = await term.getTerminalSize(); diff --git a/src/common/runtime.ts b/src/common/runtime.ts index 1330b13..5f6c004 100644 --- a/src/common/runtime.ts +++ b/src/common/runtime.ts @@ -35,6 +35,9 @@ export interface IFFI { getPointer(ta: any): unknown; } +/** + * The size of terminal in rows and columns + */ export interface ITerminalSize { rows: number; cols: number; @@ -71,6 +74,7 @@ export interface IFIO { openFile(path: string): Promise; openFileSync(path: string): string; appendFile(path: string, contents: string): Promise; + appendFileSync(path: string, contents: string): void; } /** @@ -103,7 +107,10 @@ export interface IRuntime { * @param eventName - The event to listen for * @param handler - The event handler */ - onEvent: (eventName: string, handler: (e: Event) => void) => void; + onEvent: ( + eventName: string, + handler: (e: Event | ErrorEvent) => void, + ) => void; /** * Set a beforeExit/beforeUnload event handler for the runtime @@ -129,10 +136,13 @@ let scrollRuntime: IRuntime | null = null; * Kill program, displaying an error message * @param s */ -export async function die(s: string | Error): Promise { - (await getTermios()).disableRawMode(); - console.error(s); - (await getRuntime()).exit(); +export function die(s: string | Error): void { + getTermios().then((t) => { + t.disableRawMode(); + console.error(s); + + getRuntime().then((r) => r.exit()); + }); } /** @@ -151,6 +161,9 @@ export function getRuntimeType(): RunTimeType { return runtime; } +/** + * Get the adapter object for the current Runtime + */ export async function getRuntime(): Promise { if (scrollRuntime === null) { const runtime = getRuntimeType(); diff --git a/src/common/termios.ts b/src/common/termios.ts index bb91219..e38428f 100644 --- a/src/common/termios.ts +++ b/src/common/termios.ts @@ -51,10 +51,6 @@ class Termios { this.#ptr = ffi.getPointer(this.#termios); } - get inRawMode() { - return this.#inRawMode; - } - enableRawMode() { if (this.#inRawMode) { throw new Error('Can not enable raw mode when in raw mode'); @@ -63,7 +59,7 @@ class Termios { // Get the current termios settings let res = this.#ffi.tcgetattr(STDIN_FILENO, this.#ptr); if (res === -1) { - die('Failed to get terminal settings').then(() => {}); + die('Failed to get terminal settings'); } // The #ptr property is pointing to the #termios TypedArray. As the pointer @@ -75,14 +71,10 @@ class Termios { // Update termios struct with (most of the) raw settings this.#ffi.cfmakeraw(this.#ptr); - // @TODO: Tweak a few more terminal settings - // Actually set the new termios settings res = this.#ffi.tcsetattr(STDIN_FILENO, TCSANOW, this.#ptr); if (res === -1) { - die('Failed to update terminal settings. Can\'t enter raw mode').then( - () => {}, - ); + die('Failed to update terminal settings. Can\'t enter raw mode'); } this.#inRawMode = true; @@ -98,7 +90,7 @@ class Termios { const oldTermiosPtr = this.#ffi.getPointer(this.#cookedTermios); const res = this.#ffi.tcsetattr(STDIN_FILENO, TCSANOW, oldTermiosPtr); if (res === -1) { - die('Failed to restore canonical mode.').then(() => {}); + die('Failed to restore canonical mode.'); } this.#inRawMode = false; diff --git a/src/common/utils.ts b/src/common/utils.ts index 643c1a9..db2fc22 100644 --- a/src/common/utils.ts +++ b/src/common/utils.ts @@ -1,3 +1,5 @@ +export function noop() {} + // ---------------------------------------------------------------------------- // Strings // ---------------------------------------------------------------------------- diff --git a/src/deno/file_io.ts b/src/deno/file_io.ts index a1cb2d8..69d38ef 100644 --- a/src/deno/file_io.ts +++ b/src/deno/file_io.ts @@ -23,6 +23,10 @@ const DenoFileIO: IFIO = { await writer.write(encoder.encode(contents)); file.close(); }, + appendFileSync: function (path: string, contents: string) { + const encoder = new TextEncoder(); + Deno.writeFileSync(path, encoder.encode(contents)); + }, }; export default DenoFileIO; diff --git a/src/deno/mod.ts b/src/deno/mod.ts index fc66d24..f7835b0 100644 --- a/src/deno/mod.ts +++ b/src/deno/mod.ts @@ -11,7 +11,7 @@ const DenoRuntime: IRuntime = { file: DenoFileIO, ffi: DenoFFI, term: DenoTerminalIO, - onEvent: (eventName: string, handler: (e: Event) => void) => + onEvent: (eventName: string, handler) => globalThis.addEventListener(eventName, handler), onExit: (cb: () => void): void => { globalThis.addEventListener('onbeforeunload', cb);