···11export * from "./ops.ts";
22export * from "./term.ts";
33export * from "./input.ts";
44+export * from "./settings.ts";
55+export * from "./termcodes.ts";
···44export interface TermOptions {
55 height: number;
66 width: number;
77- top?: number;
87}
98109export interface RenderOptions {
1010+ mode?: "line";
1111+1212+ /**
1313+ * Row where to begin rendering. This should only be used when
1414+ * rendering into a region as part of the CLI main screen. For
1515+ * interfaces that use the entire screen, leave unset which will
1616+ * default to 0. This is 1-based which which is the DSR native
1717+ * format.
1818+ *
1919+ * https://www.ecma-international.org/publications-and-standards/standards/ecma-48/
2020+ */
2121+ row?: number;
2222+1123 pointer?: {
1224 x: number;
1325 y: number;
···3042}
31433244export async function createTerm(options: TermOptions): Promise<Term> {
3333- let { width, height, top = 0 } = options;
3434- let native = await createTermNative(width, height, top);
4545+ let { width, height } = options;
4646+ let native = await createTermNative(width, height);
3547 let { memory, statePtr, opsBuf } = native;
36483749 let prev = new Set<string>();
···4153 return {
4254 render(ops: Op[], options?: RenderOptions): RenderResult {
4355 let len = pack(ops, memory.buffer, opsBuf, memory.buffer.byteLength);
4444- native.reduce(statePtr, opsBuf, len);
5656+ let mode = options?.mode === "line" ? 1 : 0;
5757+ let row = options?.row ?? 1;
5858+ native.reduce(statePtr, opsBuf, len, mode, row);
45594660 if (options?.pointer) {
4761 let { x, y, down } = options.pointer;
+84
termcodes.ts
···11+/**
22+ * Encode a plain escape sequence.
33+ *
44+ * Prepends `ESC` (`\x1b`) to the given string and returns the result as bytes.
55+ *
66+ * @see {@link https://www.ecma-international.org/publications-and-standards/standards/ecma-48/ | ECMA-48}
77+ */
88+export function ESC(str: string): Uint8Array {
99+ return encode(`\x1b${str}`);
1010+}
1111+1212+/**
1313+ * Encode a Control Sequence Introducer (CSI) command.
1414+ *
1515+ * Prepends `ESC[` to the given string and returns the result as bytes.
1616+ *
1717+ * @see {@link https://www.ecma-international.org/publications-and-standards/standards/ecma-48/ | ECMA-48}
1818+ */
1919+export function CSI(str: string): Uint8Array {
2020+ return ESC(`[${str}`);
2121+}
2222+2323+/**
2424+ * Request the cursor position via Device Status Report (DSR).
2525+ *
2626+ * Sends `CSI 6n`. The terminal responds with a Cursor Position Report
2727+ * (`CSI row ; column R`) where row and column are 1-based.
2828+ *
2929+ * @see {@link https://www.ecma-international.org/publications-and-standards/standards/ecma-48/ | ECMA-48}
3030+ */
3131+export function DSR(): Uint8Array {
3232+ return CSI("6n");
3333+}
3434+3535+/**
3636+ * Show the cursor (DECTCEM set).
3737+ *
3838+ * DEC private mode 25. Not part of ECMA-48; originates from the VT220.
3939+ *
4040+ * @see {@link https://vt100.net/docs/vt510-rm/DECTCEM.html | VT510 DECTCEM}
4141+ */
4242+export function SHOWCURSOR(): Uint8Array {
4343+ return CSI("?25h");
4444+}
4545+4646+/**
4747+ * Hide the cursor (DECTCEM reset).
4848+ *
4949+ * DEC private mode 25. Not part of ECMA-48; originates from the VT220.
5050+ *
5151+ * @see {@link https://vt100.net/docs/vt510-rm/DECTCEM.html | VT510 DECTCEM}
5252+ */
5353+export function HIDECURSOR(): Uint8Array {
5454+ return CSI("?25l");
5555+}
5656+5757+/**
5858+ * Switch to the alternate screen buffer (xterm private mode 1049).
5959+ *
6060+ * Saves the cursor and switches to a clean alternate screen. Use
6161+ * {@link MAINSCREEN} to switch back.
6262+ *
6363+ * @see {@link https://invisible-island.net/xterm/ctlseqs/ctlseqs.html | xterm control sequences}
6464+ */
6565+export function ALTSCREEN(): Uint8Array {
6666+ return CSI("?1049h");
6767+}
6868+6969+/**
7070+ * Switch back to the main screen buffer (xterm private mode 1049).
7171+ *
7272+ * Restores the cursor and returns to the main screen with scrollback intact.
7373+ *
7474+ * @see {@link https://invisible-island.net/xterm/ctlseqs/ctlseqs.html | xterm control sequences}
7575+ */
7676+export function MAINSCREEN(): Uint8Array {
7777+ return CSI("?1049l");
7878+}
7979+8080+const encoder = new TextEncoder();
8181+8282+function encode(str: string): Uint8Array {
8383+ return encoder.encode(str);
8484+}