scroll/src/common/types.ts

147 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-11-29 16:09:58 -05:00
import { RunTimeType } from './runtime.ts';
/**
* The size of terminal in rows and columns
*/
export interface ITerminalSize {
rows: number;
cols: number;
}
// ----------------------------------------------------------------------------
// Runtime adapter interfaces
// ----------------------------------------------------------------------------
2023-11-29 16:09:58 -05:00
/**
* 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'];
2023-11-10 08:36:18 -05:00
// ----------------------------------------------------------------------------
// General types
// ----------------------------------------------------------------------------
2023-11-21 15:14:08 -05:00
export class Position {
public x: number;
public y: number;
private constructor(x: number = 0, y: number = 0) {
2023-11-21 15:14:08 -05:00
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);
}
2023-11-21 15:14:08 -05:00
public static default(): Position {
return new Position();
2023-11-21 15:14:08 -05:00
}
}
2023-11-10 08:36:18 -05:00
// ----------------------------------------------------------------------------
// 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;
2023-11-10 08:36:18 -05:00
assertInstanceOf(actual: unknown, expectedType: any): void;
assertNotEquals(actual: unknown, expected: unknown): void;
2023-11-30 16:14:52 -05:00
assertNull(actual: unknown): void;
assertStrictEquals(actual: unknown, expected: unknown): void;
2023-11-10 08:36:18 -05:00
assertTrue(actual: boolean): void;
testSuite(testObj: any): void;
2023-11-10 08:36:18 -05:00
}