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 { IFIO } from '../common/runtime.ts';
import { readFileSync } from 'node:fs'; import { appendFileSync, readFileSync } from 'node:fs';
import { appendFile } from 'node:fs/promises'; import { appendFile } from 'node:fs/promises';
const BunFileIO: IFIO = { const BunFileIO: IFIO = {
@ -12,7 +12,10 @@ const BunFileIO: IFIO = {
return readFileSync(path).toString(); return readFileSync(path).toString();
}, },
appendFile: async function (path: string, contents: string): Promise<void> { 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, file: BunFileIO,
ffi: BunFFI, ffi: BunFFI,
term: BunTerminalIO, term: BunTerminalIO,
onEvent: (eventName: string, handler: (e: Event) => void) => onEvent: (eventName: string, handler) => process.on(eventName, handler),
process.on(eventName, handler),
onExit: (cb: () => void): void => { onExit: (cb: () => void): void => {
process.on('beforeExit', cb); process.on('beforeExit', cb);
process.on('exit', cb); process.on('exit', cb);

View File

@ -15,13 +15,11 @@ export async function main() {
}); });
// Setup error handler to log to file // Setup error handler to log to file
onEvent('error', (error: Event) => { onEvent('error', (error) => {
t.disableRawMode(); t.disableRawMode();
error.preventDefault(); if (error instanceof ErrorEvent) {
error.stopPropagation(); file.appendFileSync('./scroll.err', JSON.stringify(error, null, 2));
file.appendFile('scroll.err', JSON.stringify(error, null, 2)).then( }
() => {},
);
}); });
const terminalSize = await term.getTerminalSize(); const terminalSize = await term.getTerminalSize();

View File

@ -35,6 +35,9 @@ export interface IFFI {
getPointer(ta: any): unknown; getPointer(ta: any): unknown;
} }
/**
* The size of terminal in rows and columns
*/
export interface ITerminalSize { export interface ITerminalSize {
rows: number; rows: number;
cols: number; cols: number;
@ -71,6 +74,7 @@ export interface IFIO {
openFile(path: string): Promise<string>; openFile(path: string): Promise<string>;
openFileSync(path: string): string; openFileSync(path: string): string;
appendFile(path: string, contents: string): Promise<void>; 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 eventName - The event to listen for
* @param handler - The event handler * @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 * Set a beforeExit/beforeUnload event handler for the runtime
@ -129,10 +136,13 @@ let scrollRuntime: IRuntime | null = null;
* Kill program, displaying an error message * Kill program, displaying an error message
* @param s * @param s
*/ */
export async function die(s: string | Error): Promise<void> { export function die(s: string | Error): void {
(await getTermios()).disableRawMode(); getTermios().then((t) => {
console.error(s); t.disableRawMode();
(await getRuntime()).exit(); console.error(s);
getRuntime().then((r) => r.exit());
});
} }
/** /**
@ -151,6 +161,9 @@ export function getRuntimeType(): RunTimeType {
return runtime; return runtime;
} }
/**
* Get the adapter object for the current Runtime
*/
export async function getRuntime(): Promise<IRuntime> { export async function getRuntime(): Promise<IRuntime> {
if (scrollRuntime === null) { if (scrollRuntime === null) {
const runtime = getRuntimeType(); const runtime = getRuntimeType();

View File

@ -51,10 +51,6 @@ class Termios {
this.#ptr = ffi.getPointer(this.#termios); this.#ptr = ffi.getPointer(this.#termios);
} }
get inRawMode() {
return this.#inRawMode;
}
enableRawMode() { enableRawMode() {
if (this.#inRawMode) { if (this.#inRawMode) {
throw new Error('Can not enable raw mode when in raw mode'); throw new Error('Can not enable raw mode when in raw mode');
@ -63,7 +59,7 @@ class Termios {
// Get the current termios settings // Get the current termios settings
let res = this.#ffi.tcgetattr(STDIN_FILENO, this.#ptr); let res = this.#ffi.tcgetattr(STDIN_FILENO, this.#ptr);
if (res === -1) { 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 // 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 // Update termios struct with (most of the) raw settings
this.#ffi.cfmakeraw(this.#ptr); this.#ffi.cfmakeraw(this.#ptr);
// @TODO: Tweak a few more terminal settings
// Actually set the new termios settings // Actually set the new termios settings
res = this.#ffi.tcsetattr(STDIN_FILENO, TCSANOW, this.#ptr); res = this.#ffi.tcsetattr(STDIN_FILENO, TCSANOW, this.#ptr);
if (res === -1) { 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; this.#inRawMode = true;
@ -98,7 +90,7 @@ class Termios {
const oldTermiosPtr = this.#ffi.getPointer(this.#cookedTermios); const oldTermiosPtr = this.#ffi.getPointer(this.#cookedTermios);
const res = this.#ffi.tcsetattr(STDIN_FILENO, TCSANOW, oldTermiosPtr); const res = this.#ffi.tcsetattr(STDIN_FILENO, TCSANOW, oldTermiosPtr);
if (res === -1) { if (res === -1) {
die('Failed to restore canonical mode.').then(() => {}); die('Failed to restore canonical mode.');
} }
this.#inRawMode = false; this.#inRawMode = false;

View File

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

View File

@ -23,6 +23,10 @@ const DenoFileIO: IFIO = {
await writer.write(encoder.encode(contents)); await writer.write(encoder.encode(contents));
file.close(); file.close();
}, },
appendFileSync: function (path: string, contents: string) {
const encoder = new TextEncoder();
Deno.writeFileSync(path, encoder.encode(contents));
},
}; };
export default DenoFileIO; export default DenoFileIO;

View File

@ -11,7 +11,7 @@ const DenoRuntime: IRuntime = {
file: DenoFileIO, file: DenoFileIO,
ffi: DenoFFI, ffi: DenoFFI,
term: DenoTerminalIO, term: DenoTerminalIO,
onEvent: (eventName: string, handler: (e: Event) => void) => onEvent: (eventName: string, handler) =>
globalThis.addEventListener(eventName, handler), globalThis.addEventListener(eventName, handler),
onExit: (cb: () => void): void => { onExit: (cb: () => void): void => {
globalThis.addEventListener('onbeforeunload', cb); globalThis.addEventListener('onbeforeunload', cb);