Misc refactoring

This commit is contained in:
Timothy Warren 2023-11-16 13:00:02 -05:00
parent 1fc6e8f75f
commit 301196352a
8 changed files with 38 additions and 27 deletions

View File

@ -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<void> {
await appendFile(path, contents);
return await appendFile(path, contents);
},
appendFileSync: function (path: string, contents: string) {
return appendFileSync(path, contents);
},
};

View File

@ -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);

View File

@ -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();

View File

@ -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<string>;
openFileSync(path: string): string;
appendFile(path: string, contents: string): Promise<void>;
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<void> {
(await getTermios()).disableRawMode();
export function die(s: string | Error): void {
getTermios().then((t) => {
t.disableRawMode();
console.error(s);
(await getRuntime()).exit();
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<IRuntime> {
if (scrollRuntime === null) {
const runtime = getRuntimeType();

View File

@ -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;

View File

@ -1,3 +1,5 @@
export function noop() {}
// ----------------------------------------------------------------------------
// Strings
// ----------------------------------------------------------------------------

View File

@ -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;

View File

@ -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);