scroll/src/common/types.ts

174 lines
3.7 KiB
JavaScript

import { RunTimeType } from './runtime.ts';
// ----------------------------------------------------------------------------
// Runtime adapter interfaces
// ----------------------------------------------------------------------------
/**
* The size of terminal in rows and columns
*/
export interface ITerminalSize {
rows: number;
cols: number;
}
/**
* The native functions for getting/setting terminal settings
*/
export interface IFFI {
/**
* Get the existing termios settings (for canonical mode)
*/
tcgetattr(fd: number, termiosPtr: unknown): number;
/**
* Update the termios settings
*/
tcsetattr(fd: number, act: number, termiosPtr: unknown): number;
/**
* Update the termios pointer with raw mode settings
*/
cfmakeraw(termiosPtr: unknown): void;
/**
* Convert a TypedArray to an opaque pointer for ffi calls
*/
// deno-lint-ignore no-explicit-any
getPointer(ta: any): unknown;
/**
* Closes the FFI handle
*/
close(): void;
}
/**
* The common interface for runtime adapters
*/
export interface IRuntime {
/**
* The name of the runtime
*/
name: RunTimeType;
/**
* Runtime-specific terminal functionality
*/
term: {
/**
* The arguments passed to the program on launch
*/
argv: string[];
/**
* The generator function returning chunks of input from the stdin stream
*/
inputLoop(): AsyncGenerator<Uint8Array, null>;
/**
* Get the size of the terminal
*/
getTerminalSize(): Promise<ITerminalSize>;
/**
* Get the current chunk of input, if it exists
*/
readStdin(): Promise<string | null>;
/**
* Get the raw chunk of input
*/
readStdinRaw(): Promise<Uint8Array | null>;
/**
* Pipe a string to stdout
*/
writeStdout(s: string): Promise<void>;
};
/**
* Runtime-specific file system io
*/
file: {
openFile(path: string): Promise<string>;
appendFile(path: string, contents: string): Promise<void>;
saveFile(path: string, contents: string): Promise<void>;
};
/**
* Set up an event handler
*
* @param eventName - The event to listen for
* @param handler - The event handler
*/
onEvent: (
eventName: string,
handler: (e: Event | ErrorEvent) => void,
) => void;
/**
* Set a beforeExit/beforeUnload event handler for the runtime
* @param cb - The event handler
*/
onExit(cb: () => void): void;
/**
* Stop execution
*
* @param code
*/
exit(code?: number): void;
}
/**
* Runtime-specific terminal functionality
*/
export type ITerminal = IRuntime['term'];
/**
* Runtime-specific file handling
*/
export type IFileIO = IRuntime['file'];
// ----------------------------------------------------------------------------
// General types
// ----------------------------------------------------------------------------
export class Position {
public x: number;
public y: number;
private constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
public static at(x: number, y: number): Position {
return new Position(x, y);
}
public static default(): Position {
return new Position(0, 0);
}
}
// ----------------------------------------------------------------------------
// Testing
// ----------------------------------------------------------------------------
/**
* The shared test interface, so tests can be run by both runtimes
*/
export interface ITestBase {
assertEquals(actual: unknown, expected: unknown): void;
assertExists(actual: unknown): void;
assertFalse(actual: boolean): void;
assertInstanceOf(actual: unknown, expectedType: any): void;
assertNotEquals(actual: unknown, expected: unknown): void;
assertNull(actual: unknown): void;
assertStrictEquals(actual: unknown, expected: unknown): void;
assertTrue(actual: boolean): void;
testSuite(testObj: any): void;
}