Timothy J. Warren
ab42873182
All checks were successful
timw4mail/scroll/pipeline/head This commit looks good
147 lines
3.2 KiB
JavaScript
147 lines
3.2 KiB
JavaScript
import { RunTimeType } from './runtime.ts';
|
|
|
|
/**
|
|
* The size of terminal in rows and columns
|
|
*/
|
|
export interface ITerminalSize {
|
|
rows: number;
|
|
cols: number;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// Runtime adapter interfaces
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/**
|
|
* 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 = 0, y: number = 0) {
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
|
|
public static at(x: number, y: number): Position {
|
|
return new Position(x, y);
|
|
}
|
|
|
|
public static from(p: Position): Position {
|
|
return new Position(p.x, p.y);
|
|
}
|
|
|
|
public static default(): Position {
|
|
return new Position();
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// 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;
|
|
}
|