generated from ali/HarmonyTemplate
T7: VT terminal emulator (parser, buffer, emulator, input, self-test)
- VtParser: Paul Williams state machine subset - UTF-8 decode (strict, FFFD on overlong/surrogate/interrupt), C0 execute, C1 shortcuts, ESC/ESC_INT/SS3/CSI/OSC(BEL|ST)/DCS(prefix+hook+data)/STR strings, CAN/SUB abort, reprocess semantics for aborted strings - TerminalBuffer: main + lazy alt grids, scrollback 5000 (main only, region top==0), DECSTBM margins, tab stops, IL/DL/ICH/DCH/ECH/SU/SD/ RI with wide-cell orphan repair, per-row dirty flags, resize - TerminalEmulator: SGR (16/256/truecolor, 38;2/48;2), deferred wrap (DECAWM), DECSET/RST 1/6/7/25/47/1000/1002/1003/1004/1006/1047/1048/ 1049/2004, origin mode, IRM, DECSC/RC + CSI s/u, DECSTBM, RIS, DECALN, REP; replies DA1 ESC[?1;2c, DA2 ESC[>0;100;0c, DSR 5/6, XTVERSION DCS >|HavenOH(0.1) ST, CSI 18t size report; OSC 0/2/7/8/9/52/777 with hyperlink scheme whitelist - VtInput: xterm key encodings (arrows/DECCKM/Home/End/PgUp..F12 with modifier params, F1-F4 SS3, ctrl/alt maps), bracketed paste, SGR mouse - VtSelfTest: 12 cases incl. CJK double-width, alt-screen round-trip, scrollback growth, scroll region + RI, query replies, and the 'echo ni hao && vim' byte-stream replay (T7 acceptance) - 12/12 pass - DiagPage: button 11 runs the emulator self-test Verified: Node 24 transform-types run 12/12 PASS; hvigor BUILD SUCCESSFUL
This commit is contained in:
@@ -22,6 +22,7 @@ import { verifyHostKeyTofu, InMemoryKnownHostStore, HostKeyDecider, HostKeyPromp
|
|||||||
import { SshSession, SshSessionOptions, ExecResult } from '../ssh/SshSession';
|
import { SshSession, SshSessionOptions, ExecResult } from '../ssh/SshSession';
|
||||||
import { AuthResult } from '../ssh/Auth';
|
import { AuthResult } from '../ssh/Auth';
|
||||||
import { bytesToUtf8, utf8ToBytes, hexToBytes, bytesToHex, bytesEqual, concatBytes, SshWriter } from '../ssh/SshWire';
|
import { bytesToUtf8, utf8ToBytes, hexToBytes, bytesToHex, bytesEqual, concatBytes, SshWriter } from '../ssh/SshWire';
|
||||||
|
import { runVtSelfTest, VtTestResult } from '../vterm/VtSelfTest';
|
||||||
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
|
import { cryptoFramework } from '@kit.CryptoArchitectureKit';
|
||||||
import { BusinessError } from '@kit.BasicServicesKit';
|
import { BusinessError } from '@kit.BasicServicesKit';
|
||||||
|
|
||||||
@@ -137,6 +138,11 @@ struct DiagPage {
|
|||||||
.onClick(() => {
|
.onClick(() => {
|
||||||
this.runTest((): Promise<void> => this.testLiveAuth());
|
this.runTest((): Promise<void> => this.testLiveAuth());
|
||||||
})
|
})
|
||||||
|
Button('11. VT 终端模拟器自测')
|
||||||
|
.width('100%')
|
||||||
|
.onClick(() => {
|
||||||
|
this.runTest((): Promise<void> => this.testVtEmulator());
|
||||||
|
})
|
||||||
}
|
}
|
||||||
.width('100%')
|
.width('100%')
|
||||||
|
|
||||||
@@ -821,4 +827,26 @@ struct DiagPage {
|
|||||||
session.close();
|
session.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 11) VT terminal emulator self test: 12 cases over small grids. */
|
||||||
|
private async testVtEmulator(): Promise<void> {
|
||||||
|
const results: VtTestResult[] = runVtSelfTest();
|
||||||
|
let pass: number = 0;
|
||||||
|
let fail: number = 0;
|
||||||
|
for (let i = 0; i < results.length; i++) {
|
||||||
|
const r: VtTestResult = results[i];
|
||||||
|
if (r.ok) {
|
||||||
|
pass += 1;
|
||||||
|
this.log('PASS', `${i + 1}. ${r.name}`);
|
||||||
|
} else {
|
||||||
|
fail += 1;
|
||||||
|
this.log('FAIL', `${i + 1}. ${r.name} — ${r.detail}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fail === 0) {
|
||||||
|
this.log('PASS', `VT 终端模拟器自测 ${pass}/${results.length} 全部通过 ✓`);
|
||||||
|
} else {
|
||||||
|
this.log('FAIL', `VT 终端模拟器自测 ${pass}/${results.length},${fail} 项失败`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Haven for HarmonyOS contributors.
|
||||||
|
* Port of Haven by GlassHaven (AGPL-3.0). See LICENSE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Terminal color schemes. Four schemes ported with exact values from the
|
||||||
|
* Android app (UserPreferencesRepository.kt TerminalColorScheme); each has
|
||||||
|
* a background/foreground/cursor color plus the 16-entry ANSI palette in
|
||||||
|
* libvterm order: 0..7 normal, 8..15 bright
|
||||||
|
* (black, red, green, yellow, blue, magenta, cyan, white).
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface TerminalColorScheme {
|
||||||
|
name: string;
|
||||||
|
/** 0x00RRGGBB */
|
||||||
|
background: number;
|
||||||
|
/** 0x00RRGGBB */
|
||||||
|
foreground: number;
|
||||||
|
/** 0x00RRGGBB */
|
||||||
|
cursor: number;
|
||||||
|
/** 16 entries: normal 0..7, bright 8..15. */
|
||||||
|
ansi: number[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export const DEFAULT_SCHEME_NAME: string = 'Haven';
|
||||||
|
|
||||||
|
export const COLOR_SCHEMES: TerminalColorScheme[] = [
|
||||||
|
{
|
||||||
|
name: 'Haven',
|
||||||
|
background: 0x1A1A2E,
|
||||||
|
foreground: 0x00E676,
|
||||||
|
cursor: 0x00E676,
|
||||||
|
ansi: [
|
||||||
|
0x1A1A2E, 0xFF5C8A, 0x00E676, 0xFFD54F,
|
||||||
|
0x4FC3F7, 0xCE93D8, 0x80DEEA, 0xB0BEC5,
|
||||||
|
0x424242, 0xFF8A80, 0x69F0AE, 0xFFE082,
|
||||||
|
0x82B1FF, 0xE1BEE7, 0xA7FFEB, 0xECEFF1
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Light',
|
||||||
|
background: 0xFFFFFF,
|
||||||
|
foreground: 0x1A1A1A,
|
||||||
|
cursor: 0x1A1A1A,
|
||||||
|
ansi: [
|
||||||
|
0x073642, 0xDC322F, 0x859900, 0xB58900,
|
||||||
|
0x268BD2, 0xD33682, 0x2AA198, 0xEEE8D5,
|
||||||
|
0x002B36, 0xCB4B16, 0x586E75, 0x657B83,
|
||||||
|
0x839496, 0x6C71C4, 0x93A1A1, 0xFDF6E3
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Solarized Dark',
|
||||||
|
background: 0x002B36,
|
||||||
|
foreground: 0x839496,
|
||||||
|
cursor: 0x839496,
|
||||||
|
ansi: [
|
||||||
|
0x073642, 0xDC322F, 0x859900, 0xB58900,
|
||||||
|
0x268BD2, 0xD33682, 0x2AA198, 0xEEE8D5,
|
||||||
|
0x002B36, 0xCB4B16, 0x586E75, 0x657B83,
|
||||||
|
0x839496, 0x6C71C4, 0x93A1A1, 0xFDF6E3
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Dracula',
|
||||||
|
background: 0x282A36,
|
||||||
|
foreground: 0xF8F8F2,
|
||||||
|
cursor: 0xF8F8F2,
|
||||||
|
ansi: [
|
||||||
|
0x21222C, 0xFF5555, 0x50FA7B, 0xF1FA8C,
|
||||||
|
0xBD93F9, 0xFF79C6, 0x8BE9FD, 0xF8F8F2,
|
||||||
|
0x6272A4, 0xFF6E6E, 0x69FF94, 0xFFFFA5,
|
||||||
|
0xD6ACFF, 0xFF92DF, 0xA4FFFF, 0xFFFFFF
|
||||||
|
]
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
/** Scheme lookup by name (first scheme is the fallback). */
|
||||||
|
export function schemeByName(name: string): TerminalColorScheme {
|
||||||
|
for (let i: number = 0; i < COLOR_SCHEMES.length; i++) {
|
||||||
|
if (COLOR_SCHEMES[i].name === name) {
|
||||||
|
return COLOR_SCHEMES[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return COLOR_SCHEMES[0];
|
||||||
|
}
|
||||||
@@ -0,0 +1,658 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Haven for HarmonyOS contributors.
|
||||||
|
* Port of Haven by GlassHaven (AGPL-3.0). See LICENSE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Terminal screen buffer: an active rows×cols grid of cells, a lazily
|
||||||
|
* allocated alternate screen, and a scrollback ring (main screen only).
|
||||||
|
* Owns the cursor, scroll margins, tab stops and per-row dirty flags.
|
||||||
|
* The emulator drives it; rendering reads it.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface VtCell {
|
||||||
|
/** Codepoint; blank cells hold 0x20. */
|
||||||
|
cp: number;
|
||||||
|
/** -1 default, 0-255 ANSI index, 0x1000000|RGB truecolor. */
|
||||||
|
fg: number;
|
||||||
|
/** -1 default, 0-255 ANSI index, 0x1000000|RGB truecolor. */
|
||||||
|
bg: number;
|
||||||
|
/** ATTR_* bitfield. */
|
||||||
|
attr: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ATTR_BOLD: number = 1;
|
||||||
|
export const ATTR_DIM: number = 2;
|
||||||
|
export const ATTR_ITALIC: number = 4;
|
||||||
|
export const ATTR_UNDERLINE: number = 8;
|
||||||
|
export const ATTR_BLINK: number = 16;
|
||||||
|
export const ATTR_REVERSE: number = 32;
|
||||||
|
export const ATTR_STRIKE: number = 64;
|
||||||
|
export const ATTR_WIDE: number = 128;
|
||||||
|
export const ATTR_WIDE_CONT: number = 256;
|
||||||
|
|
||||||
|
export const COLOR_DEFAULT: number = -1;
|
||||||
|
export const COLOR_TRUECOLOR_FLAG: number = 0x1000000;
|
||||||
|
|
||||||
|
export function colorTruecolor(rgb: number): number {
|
||||||
|
return COLOR_TRUECOLOR_FLAG | (rgb & 0xffffff);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isTruecolor(c: number): boolean {
|
||||||
|
return c >= COLOR_TRUECOLOR_FLAG;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function truecolorRgb(c: number): number {
|
||||||
|
return c & 0xffffff;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const DEFAULT_SCROLLBACK: number = 5000;
|
||||||
|
|
||||||
|
/** Snapshot returned by TerminalBuffer.restoreCursor (DECSC payload). */
|
||||||
|
export interface SavedCursorState {
|
||||||
|
r: number;
|
||||||
|
c: number;
|
||||||
|
fg: number;
|
||||||
|
bg: number;
|
||||||
|
attr: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ED_TO_CURSOR: number = 0;
|
||||||
|
export const ED_FROM_START: number = 1;
|
||||||
|
export const ED_ALL: number = 2;
|
||||||
|
export const ED_ALL_AND_SCROLLBACK: number = 3;
|
||||||
|
|
||||||
|
export const EL_TO_EOL: number = 0;
|
||||||
|
export const EL_FROM_START: number = 1;
|
||||||
|
export const EL_ALL: number = 2;
|
||||||
|
|
||||||
|
function blankCell(): VtCell {
|
||||||
|
return { cp: 0x20, fg: COLOR_DEFAULT, bg: COLOR_DEFAULT, attr: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeRow(cols: number): VtCell[] {
|
||||||
|
const row: VtCell[] = [];
|
||||||
|
for (let c: number = 0; c < cols; c++) {
|
||||||
|
row.push(blankCell());
|
||||||
|
}
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeGrid(cols: number, rows: number): VtCell[][] {
|
||||||
|
const grid: VtCell[][] = [];
|
||||||
|
for (let r: number = 0; r < rows; r++) {
|
||||||
|
grid.push(makeRow(cols));
|
||||||
|
}
|
||||||
|
return grid;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class TerminalBuffer {
|
||||||
|
public cols: number;
|
||||||
|
public rows: number;
|
||||||
|
public cursorR: number = 0;
|
||||||
|
public cursorC: number = 0;
|
||||||
|
/** Scroll margins, 0-based inclusive. */
|
||||||
|
public scrollTop: number = 0;
|
||||||
|
public scrollBottom: number;
|
||||||
|
|
||||||
|
private mainRows: VtCell[][];
|
||||||
|
private altRows: VtCell[][] | null = null;
|
||||||
|
private activeRows: VtCell[][];
|
||||||
|
private altActive: boolean = false;
|
||||||
|
|
||||||
|
public scrollback: VtCell[][] = [];
|
||||||
|
private maxScrollback: number;
|
||||||
|
private scrollbackVersion: number = 0;
|
||||||
|
|
||||||
|
private tabStops: boolean[];
|
||||||
|
|
||||||
|
private savedR: number = 0;
|
||||||
|
private savedC: number = 0;
|
||||||
|
private savedFg: number = COLOR_DEFAULT;
|
||||||
|
private savedBg: number = COLOR_DEFAULT;
|
||||||
|
private savedAttr: number = 0;
|
||||||
|
|
||||||
|
private dirtyMain: boolean[];
|
||||||
|
private dirtyAlt: boolean[] | null = null;
|
||||||
|
|
||||||
|
constructor(cols: number, rows: number, maxScrollback: number) {
|
||||||
|
this.cols = cols;
|
||||||
|
this.rows = rows;
|
||||||
|
this.scrollBottom = rows - 1;
|
||||||
|
this.maxScrollback = maxScrollback;
|
||||||
|
this.mainRows = makeGrid(cols, rows);
|
||||||
|
this.activeRows = this.mainRows;
|
||||||
|
this.tabStops = this.buildTabStops(cols);
|
||||||
|
this.dirtyMain = this.buildAllDirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- geometry ----------
|
||||||
|
|
||||||
|
isAlt(): boolean {
|
||||||
|
return this.altActive;
|
||||||
|
}
|
||||||
|
|
||||||
|
getRow(r: number): VtCell[] {
|
||||||
|
return this.activeRows[r];
|
||||||
|
}
|
||||||
|
|
||||||
|
cellAt(r: number, c: number): VtCell {
|
||||||
|
return this.activeRows[r][c];
|
||||||
|
}
|
||||||
|
|
||||||
|
resize(newCols: number, newRows: number): void {
|
||||||
|
if (newCols === this.cols && newRows === this.rows) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.mainRows = this.resizeGrid(this.mainRows, newCols, newRows);
|
||||||
|
if (this.altRows !== null) {
|
||||||
|
this.altRows = this.resizeGrid(this.altRows, newCols, newRows);
|
||||||
|
}
|
||||||
|
for (let i: number = 0; i < this.scrollback.length; i++) {
|
||||||
|
this.scrollback[i] = this.resizeRow(this.scrollback[i], newCols);
|
||||||
|
}
|
||||||
|
this.cols = newCols;
|
||||||
|
this.rows = newRows;
|
||||||
|
this.activeRows = this.altActive && this.altRows !== null ? this.altRows : this.mainRows;
|
||||||
|
this.scrollTop = 0;
|
||||||
|
this.scrollBottom = newRows - 1;
|
||||||
|
this.tabStops = this.buildTabStops(newCols);
|
||||||
|
this.clampCursor();
|
||||||
|
this.markAllDirty();
|
||||||
|
this.scrollbackVersion++;
|
||||||
|
}
|
||||||
|
|
||||||
|
private resizeGrid(grid: VtCell[][], newCols: number, newRows: number): VtCell[][] {
|
||||||
|
const out: VtCell[][] = [];
|
||||||
|
for (let r: number = 0; r < newRows; r++) {
|
||||||
|
if (r < grid.length) {
|
||||||
|
out.push(this.resizeRow(grid[r], newCols));
|
||||||
|
} else {
|
||||||
|
out.push(makeRow(newCols));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
private resizeRow(row: VtCell[], newCols: number): VtCell[] {
|
||||||
|
const out: VtCell[] = [];
|
||||||
|
for (let c: number = 0; c < newCols; c++) {
|
||||||
|
if (c < row.length) {
|
||||||
|
out.push(row[c]);
|
||||||
|
} else {
|
||||||
|
out.push(blankCell());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// A wide head cut off at the right edge leaves a dangling cell pair.
|
||||||
|
if (newCols > 0 && (out[newCols - 1].attr & ATTR_WIDE) !== 0) {
|
||||||
|
out[newCols - 1] = blankCell();
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
clampCursor(): void {
|
||||||
|
if (this.cursorR > this.rows - 1) {
|
||||||
|
this.cursorR = this.rows - 1;
|
||||||
|
}
|
||||||
|
if (this.cursorR < 0) {
|
||||||
|
this.cursorR = 0;
|
||||||
|
}
|
||||||
|
if (this.cursorC > this.cols - 1) {
|
||||||
|
this.cursorC = this.cols - 1;
|
||||||
|
}
|
||||||
|
if (this.cursorC < 0) {
|
||||||
|
this.cursorC = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- dirty tracking ----------
|
||||||
|
|
||||||
|
private buildAllDirty(): boolean[] {
|
||||||
|
const flags: boolean[] = [];
|
||||||
|
for (let r: number = 0; r < this.rows; r++) {
|
||||||
|
flags.push(true);
|
||||||
|
}
|
||||||
|
return flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
markAllDirty(): void {
|
||||||
|
this.dirtyMain = this.buildAllDirty();
|
||||||
|
if (this.altRows !== null) {
|
||||||
|
this.dirtyAlt = this.buildAllDirty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
isRowDirty(r: number): boolean {
|
||||||
|
const flags: boolean[] | null = this.altActive ? this.dirtyAlt : this.dirtyMain;
|
||||||
|
if (flags === null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return r >= 0 && r < flags.length && flags[r];
|
||||||
|
}
|
||||||
|
|
||||||
|
clearRowDirty(r: number): void {
|
||||||
|
const flags: boolean[] | null = this.altActive ? this.dirtyAlt : this.dirtyMain;
|
||||||
|
if (flags !== null && r >= 0 && r < flags.length) {
|
||||||
|
flags[r] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getScrollbackVersion(): number {
|
||||||
|
return this.scrollbackVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
private markDirty(r: number): void {
|
||||||
|
const flags: boolean[] | null = this.altActive ? this.dirtyAlt : this.dirtyMain;
|
||||||
|
if (flags !== null && r >= 0 && r < flags.length) {
|
||||||
|
flags[r] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private markRangeDirty(top: number, bottom: number): void {
|
||||||
|
for (let r: number = top; r <= bottom; r++) {
|
||||||
|
this.markDirty(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- scrollback ----------
|
||||||
|
|
||||||
|
getScrollbackCount(): number {
|
||||||
|
return this.scrollback.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
getScrollbackLine(i: number): VtCell[] {
|
||||||
|
return this.scrollback[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
clearScrollback(): void {
|
||||||
|
if (this.scrollback.length > 0) {
|
||||||
|
this.scrollback = [];
|
||||||
|
this.scrollbackVersion++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private pushScrollback(row: VtCell[]): void {
|
||||||
|
this.scrollback.push(row);
|
||||||
|
while (this.scrollback.length > this.maxScrollback) {
|
||||||
|
this.scrollback.shift();
|
||||||
|
}
|
||||||
|
this.scrollbackVersion++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- screens ----------
|
||||||
|
|
||||||
|
private ensureAltRows(): void {
|
||||||
|
if (this.altRows === null) {
|
||||||
|
this.altRows = makeGrid(this.cols, this.rows);
|
||||||
|
this.dirtyAlt = this.buildAllDirty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switchToAlt(): void {
|
||||||
|
if (this.altActive) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.ensureAltRows();
|
||||||
|
this.altActive = true;
|
||||||
|
if (this.altRows !== null) {
|
||||||
|
this.activeRows = this.altRows;
|
||||||
|
}
|
||||||
|
this.markAllDirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
switchToMain(): void {
|
||||||
|
if (!this.altActive) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.altActive = false;
|
||||||
|
this.activeRows = this.mainRows;
|
||||||
|
this.markAllDirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
clearActive(): void {
|
||||||
|
for (let r: number = 0; r < this.rows; r++) {
|
||||||
|
this.activeRows[r] = makeRow(this.cols);
|
||||||
|
}
|
||||||
|
this.markAllDirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
clearAlt(): void {
|
||||||
|
if (this.altRows === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (let r: number = 0; r < this.rows; r++) {
|
||||||
|
this.altRows[r] = makeRow(this.cols);
|
||||||
|
}
|
||||||
|
if (this.altActive) {
|
||||||
|
this.markAllDirty();
|
||||||
|
} else if (this.dirtyAlt !== null) {
|
||||||
|
for (let r: number = 0; r < this.dirtyAlt.length; r++) {
|
||||||
|
this.dirtyAlt[r] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fillActive(cp: number): void {
|
||||||
|
for (let r: number = 0; r < this.rows; r++) {
|
||||||
|
const row: VtCell[] = this.activeRows[r];
|
||||||
|
for (let c: number = 0; c < this.cols; c++) {
|
||||||
|
row[c] = { cp: cp, fg: COLOR_DEFAULT, bg: COLOR_DEFAULT, attr: 0 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.markAllDirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- margins ----------
|
||||||
|
|
||||||
|
setMargins(top: number, bottom: number): void {
|
||||||
|
this.scrollTop = top;
|
||||||
|
this.scrollBottom = bottom;
|
||||||
|
}
|
||||||
|
|
||||||
|
resetMargins(): void {
|
||||||
|
this.scrollTop = 0;
|
||||||
|
this.scrollBottom = this.rows - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- cursor save/restore ----------
|
||||||
|
|
||||||
|
saveCursor(fg: number, bg: number, attr: number): void {
|
||||||
|
this.savedR = this.cursorR;
|
||||||
|
this.savedC = this.cursorC;
|
||||||
|
this.savedFg = fg;
|
||||||
|
this.savedBg = bg;
|
||||||
|
this.savedAttr = attr;
|
||||||
|
}
|
||||||
|
|
||||||
|
restoreCursor(): SavedCursorState {
|
||||||
|
return {
|
||||||
|
r: this.savedR,
|
||||||
|
c: this.savedC,
|
||||||
|
fg: this.savedFg,
|
||||||
|
bg: this.savedBg,
|
||||||
|
attr: this.savedAttr
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- tab stops ----------
|
||||||
|
|
||||||
|
private buildTabStops(cols: number): boolean[] {
|
||||||
|
const stops: boolean[] = [];
|
||||||
|
for (let c: number = 0; c < cols; c++) {
|
||||||
|
stops.push(c > 0 && c % 8 === 0);
|
||||||
|
}
|
||||||
|
return stops;
|
||||||
|
}
|
||||||
|
|
||||||
|
setTabStopAtCursor(): void {
|
||||||
|
if (this.cursorC < this.tabStops.length) {
|
||||||
|
this.tabStops[this.cursorC] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clearTabStopAtCursor(): void {
|
||||||
|
if (this.cursorC < this.tabStops.length) {
|
||||||
|
this.tabStops[this.cursorC] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clearAllTabStops(): void {
|
||||||
|
for (let c: number = 0; c < this.tabStops.length; c++) {
|
||||||
|
this.tabStops[c] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- cursor motion ----------
|
||||||
|
|
||||||
|
backspace(): void {
|
||||||
|
if (this.cursorC > 0) {
|
||||||
|
this.cursorC--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tabForward(n: number): void {
|
||||||
|
for (let i: number = 0; i < n; i++) {
|
||||||
|
let next: number = this.cols - 1;
|
||||||
|
for (let c: number = this.cursorC + 1; c < this.cols; c++) {
|
||||||
|
if (this.tabStops[c]) {
|
||||||
|
next = c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.cursorC = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tabBackward(n: number): void {
|
||||||
|
for (let i: number = 0; i < n; i++) {
|
||||||
|
let prev: number = 0;
|
||||||
|
for (let c: number = this.cursorC - 1; c >= 0; c--) {
|
||||||
|
if (this.tabStops[c]) {
|
||||||
|
prev = c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.cursorC = prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- scrolling ----------
|
||||||
|
|
||||||
|
lineFeed(): void {
|
||||||
|
if (this.cursorR === this.scrollBottom) {
|
||||||
|
this.scrollRegionUp(1);
|
||||||
|
} else if (this.cursorR < this.rows - 1) {
|
||||||
|
this.cursorR++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reverseIndex(): void {
|
||||||
|
if (this.cursorR === this.scrollTop) {
|
||||||
|
this.scrollRegionDown(1);
|
||||||
|
} else if (this.cursorR > 0) {
|
||||||
|
this.cursorR--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
scrollRegionUp(n: number): void {
|
||||||
|
const top: number = this.scrollTop;
|
||||||
|
const bottom: number = this.scrollBottom;
|
||||||
|
if (top > bottom) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const count: number = Math.min(n, bottom - top + 1);
|
||||||
|
if (count <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!this.altActive && top === 0) {
|
||||||
|
for (let i: number = 0; i < count; i++) {
|
||||||
|
this.pushScrollback(this.activeRows[top + i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (let r: number = top; r <= bottom - count; r++) {
|
||||||
|
this.activeRows[r] = this.activeRows[r + count];
|
||||||
|
}
|
||||||
|
for (let r: number = bottom - count + 1; r <= bottom; r++) {
|
||||||
|
this.activeRows[r] = makeRow(this.cols);
|
||||||
|
}
|
||||||
|
this.markRangeDirty(top, bottom);
|
||||||
|
}
|
||||||
|
|
||||||
|
scrollRegionDown(n: number): void {
|
||||||
|
const top: number = this.scrollTop;
|
||||||
|
const bottom: number = this.scrollBottom;
|
||||||
|
if (top > bottom) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const count: number = Math.min(n, bottom - top + 1);
|
||||||
|
if (count <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (let r: number = bottom; r >= top + count; r--) {
|
||||||
|
this.activeRows[r] = this.activeRows[r - count];
|
||||||
|
}
|
||||||
|
for (let r: number = top; r < top + count; r++) {
|
||||||
|
this.activeRows[r] = makeRow(this.cols);
|
||||||
|
}
|
||||||
|
this.markRangeDirty(top, bottom);
|
||||||
|
}
|
||||||
|
|
||||||
|
insertLines(n: number): void {
|
||||||
|
if (this.cursorR < this.scrollTop || this.cursorR > this.scrollBottom) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const top: number = this.cursorR;
|
||||||
|
const bottom: number = this.scrollBottom;
|
||||||
|
const count: number = Math.min(n, bottom - top + 1);
|
||||||
|
for (let r: number = bottom; r >= top + count; r--) {
|
||||||
|
this.activeRows[r] = this.activeRows[r - count];
|
||||||
|
}
|
||||||
|
for (let r: number = top; r < top + count; r++) {
|
||||||
|
this.activeRows[r] = makeRow(this.cols);
|
||||||
|
}
|
||||||
|
this.markRangeDirty(top, bottom);
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteLines(n: number): void {
|
||||||
|
if (this.cursorR < this.scrollTop || this.cursorR > this.scrollBottom) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const top: number = this.cursorR;
|
||||||
|
const bottom: number = this.scrollBottom;
|
||||||
|
const count: number = Math.min(n, bottom - top + 1);
|
||||||
|
for (let r: number = top; r <= bottom - count; r++) {
|
||||||
|
this.activeRows[r] = this.activeRows[r + count];
|
||||||
|
}
|
||||||
|
for (let r: number = bottom - count + 1; r <= bottom; r++) {
|
||||||
|
this.activeRows[r] = makeRow(this.cols);
|
||||||
|
}
|
||||||
|
this.markRangeDirty(top, bottom);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- erasing ----------
|
||||||
|
|
||||||
|
eraseInDisplay(mode: number): void {
|
||||||
|
if (mode === ED_ALL_AND_SCROLLBACK) {
|
||||||
|
this.eraseInDisplay(ED_ALL);
|
||||||
|
this.clearScrollback();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (mode === ED_TO_CURSOR) {
|
||||||
|
this.clearRect(this.cursorR, this.cursorC, this.rows - 1, this.cols - 1);
|
||||||
|
} else if (mode === ED_FROM_START) {
|
||||||
|
this.clearRect(0, 0, this.cursorR, this.cursorC);
|
||||||
|
} else {
|
||||||
|
this.clearRect(0, 0, this.rows - 1, this.cols - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
eraseInLine(mode: number): void {
|
||||||
|
if (mode === EL_TO_EOL) {
|
||||||
|
this.clearRect(this.cursorR, this.cursorC, this.cursorR, this.cols - 1);
|
||||||
|
} else if (mode === EL_FROM_START) {
|
||||||
|
this.clearRect(this.cursorR, 0, this.cursorR, this.cursorC);
|
||||||
|
} else {
|
||||||
|
this.clearRect(this.cursorR, 0, this.cursorR, this.cols - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private clearRect(r0: number, c0: number, r1: number, c1: number): void {
|
||||||
|
for (let r: number = r0; r <= r1; r++) {
|
||||||
|
const row: VtCell[] = this.activeRows[r];
|
||||||
|
for (let c: number = c0; c <= c1; c++) {
|
||||||
|
row[c] = blankCell();
|
||||||
|
}
|
||||||
|
this.markDirty(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
eraseChars(n: number): void {
|
||||||
|
const row: VtCell[] = this.activeRows[this.cursorR];
|
||||||
|
const end: number = Math.min(this.cursorC + n, this.cols);
|
||||||
|
for (let c: number = this.cursorC; c < end; c++) {
|
||||||
|
row[c] = blankCell();
|
||||||
|
}
|
||||||
|
this.repairWideOrphans(this.cursorR);
|
||||||
|
this.markDirty(this.cursorR);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- character shifting ----------
|
||||||
|
|
||||||
|
insertChars(n: number): void {
|
||||||
|
const row: VtCell[] = this.activeRows[this.cursorR];
|
||||||
|
const count: number = Math.min(n, this.cols - this.cursorC);
|
||||||
|
if (count <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (let c: number = this.cols - 1; c >= this.cursorC + count; c--) {
|
||||||
|
row[c] = row[c - count];
|
||||||
|
}
|
||||||
|
for (let c: number = this.cursorC; c < this.cursorC + count; c++) {
|
||||||
|
row[c] = blankCell();
|
||||||
|
}
|
||||||
|
this.repairWideOrphans(this.cursorR);
|
||||||
|
this.markDirty(this.cursorR);
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteChars(n: number): void {
|
||||||
|
const row: VtCell[] = this.activeRows[this.cursorR];
|
||||||
|
const count: number = Math.min(n, this.cols - this.cursorC);
|
||||||
|
if (count <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (let c: number = this.cursorC; c <= this.cols - 1 - count; c++) {
|
||||||
|
row[c] = row[c + count];
|
||||||
|
}
|
||||||
|
for (let c: number = this.cols - count; c < this.cols; c++) {
|
||||||
|
row[c] = blankCell();
|
||||||
|
}
|
||||||
|
this.repairWideOrphans(this.cursorR);
|
||||||
|
this.markDirty(this.cursorR);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Blank out wide half-cells left dangling by shifts/erases on a row. */
|
||||||
|
private repairWideOrphans(r: number): void {
|
||||||
|
const row: VtCell[] = this.activeRows[r];
|
||||||
|
for (let c: number = 0; c < this.cols; c++) {
|
||||||
|
const attr: number = row[c].attr;
|
||||||
|
if ((attr & ATTR_WIDE_CONT) !== 0) {
|
||||||
|
if (c === 0 || (row[c - 1].attr & ATTR_WIDE) === 0) {
|
||||||
|
row[c] = blankCell();
|
||||||
|
}
|
||||||
|
} else if ((attr & ATTR_WIDE) !== 0 && c === this.cols - 1) {
|
||||||
|
row[c] = blankCell();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- cell writes ----------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write one glyph at (r, c). Wide glyphs also occupy c+1. Neighboring
|
||||||
|
* wide half-cells that would be left dangling are blanked first.
|
||||||
|
*/
|
||||||
|
putChar(r: number, c: number, cp: number, fg: number, bg: number, attr: number, wide: boolean): void {
|
||||||
|
const row: VtCell[] = this.activeRows[r];
|
||||||
|
if (c > 0 && (row[c - 1].attr & ATTR_WIDE) !== 0) {
|
||||||
|
row[c - 1] = blankCell();
|
||||||
|
}
|
||||||
|
if (c + 1 < this.cols && (row[c + 1].attr & ATTR_WIDE_CONT) !== 0) {
|
||||||
|
row[c + 1] = blankCell();
|
||||||
|
}
|
||||||
|
if (wide) {
|
||||||
|
if (c + 1 < this.cols) {
|
||||||
|
if (c + 2 < this.cols && (row[c + 2].attr & ATTR_WIDE_CONT) !== 0) {
|
||||||
|
row[c + 2] = blankCell();
|
||||||
|
}
|
||||||
|
row[c] = { cp: cp, fg: fg, bg: bg, attr: attr | ATTR_WIDE };
|
||||||
|
row[c + 1] = { cp: cp, fg: fg, bg: bg, attr: attr | ATTR_WIDE_CONT };
|
||||||
|
} else {
|
||||||
|
// No room for the continuation half: degrade to a single cell.
|
||||||
|
row[c] = { cp: cp, fg: fg, bg: bg, attr: attr };
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
row[c] = { cp: cp, fg: fg, bg: bg, attr: attr };
|
||||||
|
}
|
||||||
|
this.markDirty(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,936 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Haven for HarmonyOS contributors.
|
||||||
|
* Port of Haven by GlassHaven (AGPL-3.0). See LICENSE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* VT terminal emulator: interprets the parser's dispatches into a
|
||||||
|
* TerminalBuffer, tracks modes (DECSET/DECRST), colors (SGR) and answers
|
||||||
|
* terminal queries (DA1/DA2/DSR/XTVERSION) through the writeBack callback.
|
||||||
|
* Rendering never happens here; the UI reads the buffer and dirty flags.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { VtParser, VtSink } from './VtParser';
|
||||||
|
import {
|
||||||
|
TerminalBuffer,
|
||||||
|
SavedCursorState,
|
||||||
|
COLOR_DEFAULT,
|
||||||
|
colorTruecolor,
|
||||||
|
ED_TO_CURSOR,
|
||||||
|
ED_FROM_START,
|
||||||
|
ED_ALL,
|
||||||
|
ED_ALL_AND_SCROLLBACK,
|
||||||
|
EL_TO_EOL,
|
||||||
|
EL_FROM_START,
|
||||||
|
EL_ALL,
|
||||||
|
ATTR_BOLD,
|
||||||
|
ATTR_DIM,
|
||||||
|
ATTR_ITALIC,
|
||||||
|
ATTR_UNDERLINE,
|
||||||
|
ATTR_BLINK,
|
||||||
|
ATTR_REVERSE,
|
||||||
|
ATTR_STRIKE
|
||||||
|
} from './TerminalBuffer';
|
||||||
|
import { wcwidth } from './wcwidth';
|
||||||
|
import { base64ToBytes, bytesToUtf8, utf8ToBytes } from '../ssh/SshWire';
|
||||||
|
|
||||||
|
const MOUSE_NONE: number = 0;
|
||||||
|
const MOUSE_NORMAL: number = 1000;
|
||||||
|
const MOUSE_BUTTON: number = 1002;
|
||||||
|
const MOUSE_ANY: number = 1003;
|
||||||
|
|
||||||
|
export class TerminalEmulator implements VtSink {
|
||||||
|
private buf: TerminalBuffer;
|
||||||
|
private parser: VtParser;
|
||||||
|
private writeBack: (data: Uint8Array) => void;
|
||||||
|
|
||||||
|
// current SGR state
|
||||||
|
private fg: number = COLOR_DEFAULT;
|
||||||
|
private bg: number = COLOR_DEFAULT;
|
||||||
|
private attr: number = 0;
|
||||||
|
private lastPrintedCp: number = 0;
|
||||||
|
private wrapPending: boolean = false;
|
||||||
|
/** Parameters of the CSI sequence currently being dispatched. */
|
||||||
|
private curParams: number[] = [];
|
||||||
|
|
||||||
|
// modes
|
||||||
|
private originMode: boolean = false;
|
||||||
|
private autowrap: boolean = true;
|
||||||
|
private insertMode: boolean = false;
|
||||||
|
private showCursor: boolean = true;
|
||||||
|
private cursorAppMode: boolean = false;
|
||||||
|
private keypadApp: boolean = false;
|
||||||
|
private mouseMode: number = MOUSE_NONE;
|
||||||
|
private sgrMouse: boolean = false;
|
||||||
|
private bracketedPasteEnabled: boolean = false;
|
||||||
|
private focusReportEnabled: boolean = false;
|
||||||
|
private cursorStyle: number = 0;
|
||||||
|
|
||||||
|
// DECSC extra state
|
||||||
|
private savedOriginMode: boolean = false;
|
||||||
|
private savedAutowrap: boolean = true;
|
||||||
|
|
||||||
|
// UI callbacks (all optional)
|
||||||
|
public onTitle: ((title: string) => void) | null = null;
|
||||||
|
public onCwd: ((cwd: string) => void) | null = null;
|
||||||
|
public onHyperlink: ((uri: string) => void) | null = null;
|
||||||
|
public onClipboard: ((text: string) => void) | null = null;
|
||||||
|
public onNotification: ((title: string, body: string) => void) | null = null;
|
||||||
|
public onBell: (() => void) | null = null;
|
||||||
|
|
||||||
|
constructor(cols: number, rows: number, writeBack: (data: Uint8Array) => void) {
|
||||||
|
this.buf = new TerminalBuffer(cols, rows, 5000);
|
||||||
|
this.writeBack = writeBack;
|
||||||
|
this.parser = new VtParser(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- public API ----------
|
||||||
|
|
||||||
|
feed(data: Uint8Array): void {
|
||||||
|
this.parser.feed(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
getBuffer(): TerminalBuffer {
|
||||||
|
return this.buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
getCols(): number {
|
||||||
|
return this.buf.cols;
|
||||||
|
}
|
||||||
|
|
||||||
|
getRows(): number {
|
||||||
|
return this.buf.rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
getCursorRow(): number {
|
||||||
|
return this.buf.cursorR;
|
||||||
|
}
|
||||||
|
|
||||||
|
getCursorCol(): number {
|
||||||
|
return Math.min(this.buf.cursorC, this.buf.cols - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
isCursorVisible(): boolean {
|
||||||
|
return this.showCursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
getCursorStyle(): number {
|
||||||
|
return this.cursorStyle;
|
||||||
|
}
|
||||||
|
|
||||||
|
isAltScreen(): boolean {
|
||||||
|
return this.buf.isAlt();
|
||||||
|
}
|
||||||
|
|
||||||
|
isCursorAppMode(): boolean {
|
||||||
|
return this.cursorAppMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
getMouseMode(): number {
|
||||||
|
return this.mouseMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
isSgrMouse(): boolean {
|
||||||
|
return this.sgrMouse;
|
||||||
|
}
|
||||||
|
|
||||||
|
isBracketedPaste(): boolean {
|
||||||
|
return this.bracketedPasteEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
isFocusReport(): boolean {
|
||||||
|
return this.focusReportEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
resize(cols: number, rows: number): void {
|
||||||
|
this.buf.resize(cols, rows);
|
||||||
|
this.wrapPending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- VtSink: printing ----------
|
||||||
|
|
||||||
|
print(cp: number): void {
|
||||||
|
const w: number = wcwidth(cp);
|
||||||
|
if (w <= 0) {
|
||||||
|
return; // combining marks / controls: not rendered
|
||||||
|
}
|
||||||
|
const cols: number = this.buf.cols;
|
||||||
|
if (this.wrapPending) {
|
||||||
|
this.wrapPending = false;
|
||||||
|
if (this.autowrap) {
|
||||||
|
this.buf.lineFeed();
|
||||||
|
this.buf.cursorC = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let c: number = this.buf.cursorC;
|
||||||
|
if (c + w > cols) {
|
||||||
|
if (this.autowrap) {
|
||||||
|
this.buf.lineFeed();
|
||||||
|
this.buf.cursorC = 0;
|
||||||
|
c = 0;
|
||||||
|
} else {
|
||||||
|
c = cols - w;
|
||||||
|
this.buf.cursorC = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this.insertMode) {
|
||||||
|
this.buf.insertChars(w);
|
||||||
|
}
|
||||||
|
this.buf.putChar(this.buf.cursorR, c, cp, this.fg, this.bg, this.attr, w === 2);
|
||||||
|
this.buf.cursorC = c + w;
|
||||||
|
this.lastPrintedCp = cp;
|
||||||
|
if (this.buf.cursorC >= cols) {
|
||||||
|
if (this.autowrap) {
|
||||||
|
this.wrapPending = true;
|
||||||
|
} else {
|
||||||
|
this.buf.cursorC = cols - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
executeC0(b: number): void {
|
||||||
|
switch (b) {
|
||||||
|
case 0x07: // BEL
|
||||||
|
if (this.onBell !== null) {
|
||||||
|
this.onBell();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x08: // BS
|
||||||
|
this.clearWrapPending();
|
||||||
|
this.buf.backspace();
|
||||||
|
break;
|
||||||
|
case 0x09: // TAB
|
||||||
|
this.clearWrapPending();
|
||||||
|
this.buf.tabForward(1);
|
||||||
|
break;
|
||||||
|
case 0x0a: // LF
|
||||||
|
case 0x0b: // VT
|
||||||
|
case 0x0c: // FF
|
||||||
|
this.clearWrapPending();
|
||||||
|
this.buf.lineFeed();
|
||||||
|
break;
|
||||||
|
case 0x0d: // CR
|
||||||
|
this.clearWrapPending();
|
||||||
|
this.buf.cursorC = 0;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break; // SO/SI/ENQ/... ignored
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- VtSink: ESC ----------
|
||||||
|
|
||||||
|
escDispatch(intermediates: Uint8Array, final: number): void {
|
||||||
|
this.clearWrapPending();
|
||||||
|
const inter: number = intermediates.length > 0 ? intermediates[0] : 0;
|
||||||
|
if (inter === 0x23) { // '#'
|
||||||
|
if (final === 0x38) {
|
||||||
|
this.decaln();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (inter === 0x4f) { // SS3 (from C1 shortcut): ignored
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (inter !== 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (final) {
|
||||||
|
case 0x37:
|
||||||
|
this.decsc();
|
||||||
|
break;
|
||||||
|
case 0x38:
|
||||||
|
this.decrc();
|
||||||
|
break;
|
||||||
|
case 0x44: // IND
|
||||||
|
this.buf.lineFeed();
|
||||||
|
break;
|
||||||
|
case 0x45: // NEL
|
||||||
|
this.buf.cursorC = 0;
|
||||||
|
this.buf.lineFeed();
|
||||||
|
break;
|
||||||
|
case 0x48: // HTS
|
||||||
|
this.buf.setTabStopAtCursor();
|
||||||
|
break;
|
||||||
|
case 0x4d: // RI
|
||||||
|
this.buf.reverseIndex();
|
||||||
|
break;
|
||||||
|
case 0x63: // RIS
|
||||||
|
this.fullReset();
|
||||||
|
break;
|
||||||
|
case 0x3d:
|
||||||
|
this.keypadApp = true;
|
||||||
|
break;
|
||||||
|
case 0x3e:
|
||||||
|
this.keypadApp = false;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- VtSink: CSI ----------
|
||||||
|
|
||||||
|
csiDispatch(isPrivate: boolean, params: number[], intermediates: Uint8Array, final: number): void {
|
||||||
|
this.clearWrapPending();
|
||||||
|
this.curParams = params;
|
||||||
|
const buf: TerminalBuffer = this.buf;
|
||||||
|
switch (final) {
|
||||||
|
case 0x40: // ICH
|
||||||
|
buf.insertChars(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x41: // CUU
|
||||||
|
this.moveUp(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x42: // CUD
|
||||||
|
this.moveDown(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x43: // CUF
|
||||||
|
this.moveRight(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x44: // CUB
|
||||||
|
this.moveLeft(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x45: // CNL
|
||||||
|
this.moveDown(this.n(0));
|
||||||
|
buf.cursorC = 0;
|
||||||
|
break;
|
||||||
|
case 0x46: // CPL
|
||||||
|
this.moveUp(this.n(0));
|
||||||
|
buf.cursorC = 0;
|
||||||
|
break;
|
||||||
|
case 0x47: // CHA
|
||||||
|
this.setCol(this.n(0) - 1);
|
||||||
|
break;
|
||||||
|
case 0x48: // CUP
|
||||||
|
case 0x66: // HVP
|
||||||
|
this.cup();
|
||||||
|
break;
|
||||||
|
case 0x49: // CHT
|
||||||
|
buf.tabForward(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x4a: // ED
|
||||||
|
buf.eraseInDisplay(this.edMode(this.p(0, 0)));
|
||||||
|
break;
|
||||||
|
case 0x4b: // EL
|
||||||
|
buf.eraseInLine(this.p(0, 0));
|
||||||
|
break;
|
||||||
|
case 0x4c: // IL
|
||||||
|
buf.insertLines(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x4d: // DL
|
||||||
|
buf.deleteLines(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x50: // DCH
|
||||||
|
buf.deleteChars(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x53: // SU
|
||||||
|
buf.scrollRegionUp(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x54: // SD
|
||||||
|
buf.scrollRegionDown(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x58: // ECH
|
||||||
|
buf.eraseChars(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x5a: // CBT
|
||||||
|
buf.tabBackward(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x60: // HPA
|
||||||
|
this.setCol(this.n(0) - 1);
|
||||||
|
break;
|
||||||
|
case 0x61: // HPR
|
||||||
|
this.setCol(buf.cursorC + this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x62: // REP
|
||||||
|
this.rep(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x63: // DA
|
||||||
|
if (isPrivate) {
|
||||||
|
this.sendDa2();
|
||||||
|
} else {
|
||||||
|
this.sendDa1();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x64: // VPA
|
||||||
|
this.vpa();
|
||||||
|
break;
|
||||||
|
case 0x65: // VPR
|
||||||
|
this.moveDown(this.n(0));
|
||||||
|
break;
|
||||||
|
case 0x67: // TBC
|
||||||
|
this.tbc(this.p(0, 0));
|
||||||
|
break;
|
||||||
|
case 0x68: // SM / DECSET
|
||||||
|
case 0x6c: // RM / DECRST
|
||||||
|
this.handleModes(isPrivate, final === 0x68, params);
|
||||||
|
break;
|
||||||
|
case 0x6d: // SGR
|
||||||
|
if (!isPrivate) {
|
||||||
|
this.sgr(params);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x6e: // DSR
|
||||||
|
this.dsr();
|
||||||
|
break;
|
||||||
|
case 0x71: // DECSCUSR: CSI Ps SP q
|
||||||
|
if (intermediates.length === 1 && intermediates[0] === 0x20) {
|
||||||
|
this.cursorStyle = this.clamp(this.p(0, 0), 0, 6);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x72: // DECSTBM
|
||||||
|
if (!isPrivate) {
|
||||||
|
this.decstbm();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x73: // SCP
|
||||||
|
if (!isPrivate) {
|
||||||
|
this.decsc();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 0x74: // window ops
|
||||||
|
this.windowOp();
|
||||||
|
break;
|
||||||
|
case 0x75: // RCP
|
||||||
|
if (!isPrivate) {
|
||||||
|
this.decrc();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- VtSink: OSC ----------
|
||||||
|
|
||||||
|
oscDispatch(payload: Uint8Array): void {
|
||||||
|
let sep: number = -1;
|
||||||
|
for (let i: number = 0; i < payload.length; i++) {
|
||||||
|
if (payload[i] === 0x3b) {
|
||||||
|
sep = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let cmd: number = -1;
|
||||||
|
let rest: Uint8Array;
|
||||||
|
if (sep < 0) {
|
||||||
|
cmd = this.parseOscNumber(payload, 0, payload.length);
|
||||||
|
rest = new Uint8Array(0);
|
||||||
|
} else {
|
||||||
|
cmd = this.parseOscNumber(payload, 0, sep);
|
||||||
|
rest = payload.slice(sep + 1);
|
||||||
|
}
|
||||||
|
if (cmd === 0 || cmd === 2) {
|
||||||
|
if (this.onTitle !== null) {
|
||||||
|
this.onTitle(bytesToUtf8(rest));
|
||||||
|
}
|
||||||
|
} else if (cmd === 7) {
|
||||||
|
if (this.onCwd !== null) {
|
||||||
|
this.onCwd(bytesToUtf8(rest));
|
||||||
|
}
|
||||||
|
} else if (cmd === 8) {
|
||||||
|
this.handleHyperlink(rest);
|
||||||
|
} else if (cmd === 52) {
|
||||||
|
this.handleClipboard(rest);
|
||||||
|
} else if (cmd === 9 || cmd === 777) {
|
||||||
|
this.handleNotification(rest);
|
||||||
|
}
|
||||||
|
// 110-119 dynamic color resets and anything else: ignored
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- VtSink: DCS ----------
|
||||||
|
|
||||||
|
dcsDispatch(prefix: Uint8Array, data: Uint8Array): void {
|
||||||
|
if (prefix.length === 2 && prefix[0] === 0x3e && prefix[1] === 0x7c) {
|
||||||
|
if (data.length >= 1 && data[0] === 0x71) {
|
||||||
|
// XTVERSION query
|
||||||
|
this.writeBack(utf8ToBytes('\u001bP>|HavenOH(0.1)\u001b\\'));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// DECRQSS / tmux probes: unanswered
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- OSC handlers ----------
|
||||||
|
|
||||||
|
private parseOscNumber(payload: Uint8Array, from: number, to: number): number {
|
||||||
|
let v: number = 0;
|
||||||
|
let any: boolean = false;
|
||||||
|
for (let i: number = from; i < to; i++) {
|
||||||
|
const b: number = payload[i];
|
||||||
|
if (b >= 0x30 && b <= 0x39) {
|
||||||
|
v = v * 10 + (b - 0x30);
|
||||||
|
any = true;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return any ? v : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleHyperlink(rest: Uint8Array): void {
|
||||||
|
let idx: number = -1;
|
||||||
|
for (let i: number = 0; i < rest.length; i++) {
|
||||||
|
if (rest[i] === 0x3b) {
|
||||||
|
idx = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (idx < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const uri: string = bytesToUtf8(rest.slice(idx + 1));
|
||||||
|
if (this.onHyperlink === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (uri.length === 0) {
|
||||||
|
this.onHyperlink('');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.uriAllowed(uri)) {
|
||||||
|
this.onHyperlink(uri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private uriAllowed(uri: string): boolean {
|
||||||
|
return uri.indexOf('http://') === 0 ||
|
||||||
|
uri.indexOf('https://') === 0 ||
|
||||||
|
uri.indexOf('mailto:') === 0 ||
|
||||||
|
uri.indexOf('tel:') === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleClipboard(rest: Uint8Array): void {
|
||||||
|
let idx: number = -1;
|
||||||
|
for (let i: number = 0; i < rest.length; i++) {
|
||||||
|
if (rest[i] === 0x3b) {
|
||||||
|
idx = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let selection: string = 'c';
|
||||||
|
let dataPart: Uint8Array = rest;
|
||||||
|
if (idx >= 0) {
|
||||||
|
selection = bytesToUtf8(rest.slice(0, idx));
|
||||||
|
dataPart = rest.slice(idx + 1);
|
||||||
|
}
|
||||||
|
if (dataPart.length === 0) {
|
||||||
|
return; // clipboard query: unsupported
|
||||||
|
}
|
||||||
|
if (selection.length > 0 && selection !== 'c' && selection !== 's') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const decoded: Uint8Array = base64ToBytes(bytesToUtf8(dataPart));
|
||||||
|
const text: string = bytesToUtf8(decoded);
|
||||||
|
if (this.onClipboard !== null) {
|
||||||
|
this.onClipboard(text);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// malformed base64: ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleNotification(rest: Uint8Array): void {
|
||||||
|
// OSC 9 ; title ; body / OSC 777 ; notify ; title ; body
|
||||||
|
const text: string = bytesToUtf8(rest);
|
||||||
|
const parts: string[] = text.split(';');
|
||||||
|
if (parts.length >= 2 && this.onNotification !== null) {
|
||||||
|
const title: string = parts[parts.length - 2];
|
||||||
|
const body: string = parts[parts.length - 1];
|
||||||
|
this.onNotification(title, body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- cursor helpers ----------
|
||||||
|
|
||||||
|
private clearWrapPending(): void {
|
||||||
|
if (this.wrapPending) {
|
||||||
|
this.wrapPending = false;
|
||||||
|
if (this.buf.cursorC >= this.buf.cols) {
|
||||||
|
this.buf.cursorC = this.buf.cols - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private clamp(v: number, lo: number, hi: number): number {
|
||||||
|
if (v < lo) {
|
||||||
|
return lo;
|
||||||
|
}
|
||||||
|
if (v > hi) {
|
||||||
|
return hi;
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
private moveUp(n: number): void {
|
||||||
|
const limit: number = this.buf.cursorR >= this.buf.scrollTop ? this.buf.scrollTop : 0;
|
||||||
|
this.buf.cursorR = Math.max(this.buf.cursorR - n, limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
private moveDown(n: number): void {
|
||||||
|
const limit: number = this.buf.cursorR <= this.buf.scrollBottom ? this.buf.scrollBottom : this.buf.rows - 1;
|
||||||
|
this.buf.cursorR = Math.min(this.buf.cursorR + n, limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
private moveLeft(n: number): void {
|
||||||
|
this.buf.cursorC = Math.max(this.buf.cursorC - n, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private moveRight(n: number): void {
|
||||||
|
this.buf.cursorC = Math.min(this.buf.cursorC + n, this.buf.cols - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private setCol(c: number): void {
|
||||||
|
this.buf.cursorC = this.clamp(c, 0, this.buf.cols - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private cup(): void {
|
||||||
|
let r: number = this.p(0, 1) - 1;
|
||||||
|
const c: number = this.clamp(this.p(1, 1) - 1, 0, this.buf.cols - 1);
|
||||||
|
const rowLimit: number = this.originMode ? this.buf.scrollBottom - this.buf.scrollTop : this.buf.rows - 1;
|
||||||
|
r = this.clamp(r, 0, rowLimit);
|
||||||
|
this.buf.cursorR = this.originMode ? r + this.buf.scrollTop : r;
|
||||||
|
this.buf.cursorC = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
private vpa(): void {
|
||||||
|
let r: number = this.n(0) - 1;
|
||||||
|
const rowLimit: number = this.originMode ? this.buf.scrollBottom - this.buf.scrollTop : this.buf.rows - 1;
|
||||||
|
r = this.clamp(r, 0, rowLimit);
|
||||||
|
this.buf.cursorR = this.originMode ? r + this.buf.scrollTop : r;
|
||||||
|
}
|
||||||
|
|
||||||
|
private home(): void {
|
||||||
|
this.buf.cursorR = this.originMode ? this.buf.scrollTop : 0;
|
||||||
|
this.buf.cursorC = 0;
|
||||||
|
this.wrapPending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private rep(n: number): void {
|
||||||
|
if (this.lastPrintedCp <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const count: number = this.clamp(n, 1, 1000);
|
||||||
|
for (let i: number = 0; i < count; i++) {
|
||||||
|
this.print(this.lastPrintedCp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private tbc(mode: number): void {
|
||||||
|
if (mode === 0) {
|
||||||
|
this.buf.clearTabStopAtCursor();
|
||||||
|
} else if (mode === 3) {
|
||||||
|
this.buf.clearAllTabStops();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private edMode(v: number): number {
|
||||||
|
if (v === ED_FROM_START || v === ED_ALL || v === ED_ALL_AND_SCROLLBACK) {
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
return ED_TO_CURSOR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- SGR ----------
|
||||||
|
|
||||||
|
private sgr(params: number[]): void {
|
||||||
|
if (params.length === 0) {
|
||||||
|
this.resetStyle();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let i: number = 0;
|
||||||
|
while (i < params.length) {
|
||||||
|
const v: number = params[i] < 0 ? 0 : params[i];
|
||||||
|
if (v === 0) {
|
||||||
|
this.resetStyle();
|
||||||
|
} else if (v === 1) {
|
||||||
|
this.attr |= ATTR_BOLD;
|
||||||
|
} else if (v === 2) {
|
||||||
|
this.attr |= ATTR_DIM;
|
||||||
|
} else if (v === 3) {
|
||||||
|
this.attr |= ATTR_ITALIC;
|
||||||
|
} else if (v === 4) {
|
||||||
|
this.attr |= ATTR_UNDERLINE;
|
||||||
|
} else if (v === 5 || v === 6) {
|
||||||
|
this.attr |= ATTR_BLINK;
|
||||||
|
} else if (v === 7) {
|
||||||
|
this.attr |= ATTR_REVERSE;
|
||||||
|
} else if (v === 9) {
|
||||||
|
this.attr |= ATTR_STRIKE;
|
||||||
|
} else if (v === 21) {
|
||||||
|
this.attr |= ATTR_UNDERLINE; // doubly underlined → underline
|
||||||
|
} else if (v === 22) {
|
||||||
|
this.attr &= ~(ATTR_BOLD | ATTR_DIM);
|
||||||
|
} else if (v === 23) {
|
||||||
|
this.attr &= ~ATTR_ITALIC;
|
||||||
|
} else if (v === 24) {
|
||||||
|
this.attr &= ~ATTR_UNDERLINE;
|
||||||
|
} else if (v === 25) {
|
||||||
|
this.attr &= ~ATTR_BLINK;
|
||||||
|
} else if (v === 27) {
|
||||||
|
this.attr &= ~ATTR_REVERSE;
|
||||||
|
} else if (v === 29) {
|
||||||
|
this.attr &= ~ATTR_STRIKE;
|
||||||
|
} else if (v >= 30 && v <= 37) {
|
||||||
|
this.fg = v - 30;
|
||||||
|
} else if (v === 39) {
|
||||||
|
this.fg = COLOR_DEFAULT;
|
||||||
|
} else if (v >= 40 && v <= 47) {
|
||||||
|
this.bg = v - 40;
|
||||||
|
} else if (v === 49) {
|
||||||
|
this.bg = COLOR_DEFAULT;
|
||||||
|
} else if (v === 38 || v === 48 || v === 58) {
|
||||||
|
i = this.sgrExtendedColor(v, params, i);
|
||||||
|
} else if (v >= 90 && v <= 97) {
|
||||||
|
this.fg = v - 90 + 8;
|
||||||
|
} else if (v >= 100 && v <= 107) {
|
||||||
|
this.bg = v - 100 + 8;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Handle SGR 38/48/58; returns the new param index (already past color spec). */
|
||||||
|
private sgrExtendedColor(kind: number, params: number[], i: number): number {
|
||||||
|
if (i + 1 >= params.length) {
|
||||||
|
return params.length;
|
||||||
|
}
|
||||||
|
const mode: number = params[i + 1];
|
||||||
|
if (mode === 5) { // 256-color index
|
||||||
|
if (i + 2 >= params.length) {
|
||||||
|
return params.length;
|
||||||
|
}
|
||||||
|
const idx: number = this.clamp(params[i + 2], 0, 255);
|
||||||
|
if (kind === 38) {
|
||||||
|
this.fg = idx;
|
||||||
|
} else if (kind === 48) {
|
||||||
|
this.bg = idx;
|
||||||
|
}
|
||||||
|
// 58 (underline color): ignored
|
||||||
|
return i + 2;
|
||||||
|
}
|
||||||
|
if (mode === 2) { // truecolor
|
||||||
|
if (i + 4 >= params.length) {
|
||||||
|
return params.length;
|
||||||
|
}
|
||||||
|
const r: number = this.clamp(params[i + 2], 0, 255);
|
||||||
|
const g: number = this.clamp(params[i + 3], 0, 255);
|
||||||
|
const b: number = this.clamp(params[i + 4], 0, 255);
|
||||||
|
const rgb: number = (r << 16) | (g << 8) | b;
|
||||||
|
if (kind === 38) {
|
||||||
|
this.fg = colorTruecolor(rgb);
|
||||||
|
} else if (kind === 48) {
|
||||||
|
this.bg = colorTruecolor(rgb);
|
||||||
|
}
|
||||||
|
return i + 4;
|
||||||
|
}
|
||||||
|
return i + 1; // malformed: skip the mode token
|
||||||
|
}
|
||||||
|
|
||||||
|
private resetStyle(): void {
|
||||||
|
this.fg = COLOR_DEFAULT;
|
||||||
|
this.bg = COLOR_DEFAULT;
|
||||||
|
this.attr = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- modes ----------
|
||||||
|
|
||||||
|
private handleModes(isPrivate: boolean, set: boolean, params: number[]): void {
|
||||||
|
if (!isPrivate) {
|
||||||
|
for (let i: number = 0; i < params.length; i++) {
|
||||||
|
if (params[i] === 4) {
|
||||||
|
this.insertMode = set; // IRM
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (let i: number = 0; i < params.length; i++) {
|
||||||
|
const m: number = params[i];
|
||||||
|
if (m === 1) {
|
||||||
|
this.cursorAppMode = set; // DECCKM
|
||||||
|
} else if (m === 6) {
|
||||||
|
this.originMode = set; // DECOM
|
||||||
|
this.home();
|
||||||
|
} else if (m === 7) {
|
||||||
|
this.autowrap = set; // DECAWM
|
||||||
|
if (!set) {
|
||||||
|
this.clearWrapPending();
|
||||||
|
}
|
||||||
|
} else if (m === 25) {
|
||||||
|
this.showCursor = set; // DECTCEM
|
||||||
|
} else if (m === 47) {
|
||||||
|
if (set) {
|
||||||
|
this.enterAltScreen(false);
|
||||||
|
} else {
|
||||||
|
this.leaveAltScreen(false);
|
||||||
|
}
|
||||||
|
} else if (m === MOUSE_NORMAL || m === MOUSE_BUTTON || m === MOUSE_ANY) {
|
||||||
|
if (set) {
|
||||||
|
this.mouseMode = m;
|
||||||
|
} else if (this.mouseMode === m) {
|
||||||
|
this.mouseMode = MOUSE_NONE;
|
||||||
|
}
|
||||||
|
} else if (m === 1004) {
|
||||||
|
this.focusReportEnabled = set;
|
||||||
|
} else if (m === 1006) {
|
||||||
|
this.sgrMouse = set;
|
||||||
|
} else if (m === 1047) {
|
||||||
|
if (set) {
|
||||||
|
this.enterAltScreen(false);
|
||||||
|
} else {
|
||||||
|
this.leaveAltScreen(true);
|
||||||
|
}
|
||||||
|
} else if (m === 1048) {
|
||||||
|
if (set) {
|
||||||
|
this.decsc();
|
||||||
|
} else {
|
||||||
|
this.decrc();
|
||||||
|
}
|
||||||
|
} else if (m === 1049) {
|
||||||
|
if (set) {
|
||||||
|
this.decsc();
|
||||||
|
this.enterAltScreen(true);
|
||||||
|
} else {
|
||||||
|
this.leaveAltScreen(false);
|
||||||
|
this.decrc();
|
||||||
|
}
|
||||||
|
} else if (m === 2004) {
|
||||||
|
this.bracketedPasteEnabled = set;
|
||||||
|
}
|
||||||
|
// 3 (DECCOLM), 1005 (UTF-8 mouse) and others: ignored
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private enterAltScreen(clearOnEnter: boolean): void {
|
||||||
|
if (this.buf.isAlt()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.buf.switchToAlt();
|
||||||
|
if (clearOnEnter) {
|
||||||
|
this.buf.clearActive();
|
||||||
|
}
|
||||||
|
this.buf.cursorR = 0;
|
||||||
|
this.buf.cursorC = 0;
|
||||||
|
this.wrapPending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private leaveAltScreen(clearAlt: boolean): void {
|
||||||
|
if (!this.buf.isAlt()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.buf.switchToMain();
|
||||||
|
if (clearAlt) {
|
||||||
|
this.buf.clearAlt();
|
||||||
|
}
|
||||||
|
this.wrapPending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- save/restore ----------
|
||||||
|
|
||||||
|
private decsc(): void {
|
||||||
|
this.buf.saveCursor(this.fg, this.bg, this.attr);
|
||||||
|
this.savedOriginMode = this.originMode;
|
||||||
|
this.savedAutowrap = this.autowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
private decrc(): void {
|
||||||
|
const s: SavedCursorState = this.buf.restoreCursor();
|
||||||
|
this.buf.cursorR = this.clamp(s.r, 0, this.buf.rows - 1);
|
||||||
|
this.buf.cursorC = this.clamp(s.c, 0, this.buf.cols - 1);
|
||||||
|
this.fg = s.fg;
|
||||||
|
this.bg = s.bg;
|
||||||
|
this.attr = s.attr;
|
||||||
|
this.originMode = this.savedOriginMode;
|
||||||
|
this.autowrap = this.savedAutowrap;
|
||||||
|
this.wrapPending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- full reset / DECALN ----------
|
||||||
|
|
||||||
|
private fullReset(): void {
|
||||||
|
this.resetStyle();
|
||||||
|
this.originMode = false;
|
||||||
|
this.autowrap = true;
|
||||||
|
this.insertMode = false;
|
||||||
|
this.showCursor = true;
|
||||||
|
this.cursorAppMode = false;
|
||||||
|
this.keypadApp = false;
|
||||||
|
this.mouseMode = MOUSE_NONE;
|
||||||
|
this.sgrMouse = false;
|
||||||
|
this.bracketedPasteEnabled = false;
|
||||||
|
this.focusReportEnabled = false;
|
||||||
|
this.cursorStyle = 0;
|
||||||
|
this.wrapPending = false;
|
||||||
|
this.lastPrintedCp = 0;
|
||||||
|
if (this.buf.isAlt()) {
|
||||||
|
this.buf.switchToMain();
|
||||||
|
}
|
||||||
|
this.buf.resetMargins();
|
||||||
|
this.buf.clearActive();
|
||||||
|
this.buf.clearScrollback();
|
||||||
|
this.buf.cursorR = 0;
|
||||||
|
this.buf.cursorC = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private decaln(): void {
|
||||||
|
this.buf.resetMargins();
|
||||||
|
this.buf.fillActive(0x45); // 'E'
|
||||||
|
this.buf.cursorR = 0;
|
||||||
|
this.buf.cursorC = 0;
|
||||||
|
this.wrapPending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- queries ----------
|
||||||
|
|
||||||
|
private sendDa1(): void {
|
||||||
|
this.writeBack(utf8ToBytes('\u001b[?1;2c'));
|
||||||
|
}
|
||||||
|
|
||||||
|
private sendDa2(): void {
|
||||||
|
this.writeBack(utf8ToBytes('\u001b[>0;100;0c'));
|
||||||
|
}
|
||||||
|
|
||||||
|
private dsr(): void {
|
||||||
|
const code: number = this.p(0, 0);
|
||||||
|
if (code === 5) {
|
||||||
|
this.writeBack(utf8ToBytes('\u001b[0n'));
|
||||||
|
} else if (code === 6) {
|
||||||
|
const r: number = this.buf.cursorR + 1;
|
||||||
|
const c: number = Math.min(this.buf.cursorC, this.buf.cols - 1) + 1;
|
||||||
|
this.writeBack(utf8ToBytes(`\u001b[${r};${c}R`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private windowOp(): void {
|
||||||
|
const op: number = this.p(0, 0);
|
||||||
|
if (op === 18) { // report text area size
|
||||||
|
this.writeBack(utf8ToBytes(`\u001b[8;${this.buf.rows};${this.buf.cols}t`));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private decstbm(): void {
|
||||||
|
const top: number = this.p(0, 1) - 1;
|
||||||
|
const bottom: number = this.p(1, this.buf.rows) - 1;
|
||||||
|
if (top < 0 || bottom >= this.buf.rows || top >= bottom) {
|
||||||
|
return; // need a legal region of at least two rows
|
||||||
|
}
|
||||||
|
this.buf.setMargins(top, bottom);
|
||||||
|
this.home();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- param helpers ----------
|
||||||
|
|
||||||
|
/** Parameter of the CSI being dispatched: -1 (omitted) falls back to def. */
|
||||||
|
private p(idx: number, def: number): number {
|
||||||
|
if (idx < this.curParams.length && this.curParams[idx] >= 0) {
|
||||||
|
return this.curParams[idx];
|
||||||
|
}
|
||||||
|
return def;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Count parameter: omitted or 0 both mean 1. */
|
||||||
|
private n(idx: number): number {
|
||||||
|
const v: number = this.p(idx, 1);
|
||||||
|
return v === 0 ? 1 : v;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,229 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Haven for HarmonyOS contributors.
|
||||||
|
* Port of Haven by GlassHaven (AGPL-3.0). See LICENSE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keyboard / gesture input → terminal byte sequences (xterm-compatible).
|
||||||
|
* Modifier mask bits follow termlib convention: bit0 Shift, bit1 Alt,
|
||||||
|
* bit2 Ctrl. Mouse uses SGR (1006) encoding exclusively.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { utf8ToBytes, concatBytes } from '../ssh/SshWire';
|
||||||
|
|
||||||
|
/** Functional keys encodable by encodeKey(). */
|
||||||
|
export enum VtKey {
|
||||||
|
UP = 0,
|
||||||
|
DOWN = 1,
|
||||||
|
LEFT = 2,
|
||||||
|
RIGHT = 3,
|
||||||
|
HOME = 4,
|
||||||
|
END = 5,
|
||||||
|
PAGE_UP = 6,
|
||||||
|
PAGE_DOWN = 7,
|
||||||
|
INSERT = 8,
|
||||||
|
DELETE = 9,
|
||||||
|
BACKSPACE = 10,
|
||||||
|
ENTER = 11,
|
||||||
|
ESCAPE = 12,
|
||||||
|
TAB = 13,
|
||||||
|
F1 = 14,
|
||||||
|
F2 = 15,
|
||||||
|
F3 = 16,
|
||||||
|
F4 = 17,
|
||||||
|
F5 = 18,
|
||||||
|
F6 = 19,
|
||||||
|
F7 = 20,
|
||||||
|
F8 = 21,
|
||||||
|
F9 = 22,
|
||||||
|
F10 = 23,
|
||||||
|
F11 = 24,
|
||||||
|
F12 = 25,
|
||||||
|
SPACE = 26
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Modifier bit mask (combine with |). */
|
||||||
|
export enum VtModifier {
|
||||||
|
NONE = 0,
|
||||||
|
SHIFT = 1,
|
||||||
|
ALT = 2,
|
||||||
|
CTRL = 4
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Mouse buttons for encodeMouseSgr(); add 32 for motion while held. */
|
||||||
|
export const MOUSE_BTN_LEFT: number = 0;
|
||||||
|
export const MOUSE_BTN_MIDDLE: number = 1;
|
||||||
|
export const MOUSE_BTN_RIGHT: number = 2;
|
||||||
|
export const MOUSE_BTN_RELEASE: number = 3;
|
||||||
|
export const MOUSE_WHEEL_UP: number = 64;
|
||||||
|
export const MOUSE_WHEEL_DOWN: number = 65;
|
||||||
|
export const MOUSE_MOTION_FLAG: number = 32;
|
||||||
|
|
||||||
|
const ESC: number = 0x1b;
|
||||||
|
|
||||||
|
function csiSeq(body: string): Uint8Array {
|
||||||
|
const inner: Uint8Array = utf8ToBytes(body);
|
||||||
|
const out: Uint8Array = new Uint8Array(inner.length + 2);
|
||||||
|
out[0] = ESC;
|
||||||
|
out[1] = 0x5b; // [
|
||||||
|
for (let i: number = 0; i < inner.length; i++) {
|
||||||
|
out[i + 2] = inner[i];
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ss3Seq(body: string): Uint8Array {
|
||||||
|
const inner: Uint8Array = utf8ToBytes(body);
|
||||||
|
const out: Uint8Array = new Uint8Array(inner.length + 2);
|
||||||
|
out[0] = ESC;
|
||||||
|
out[1] = 0x4f; // O
|
||||||
|
for (let i: number = 0; i < inner.length; i++) {
|
||||||
|
out[i + 2] = inner[i];
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
function single(b: number): Uint8Array {
|
||||||
|
const out: Uint8Array = new Uint8Array(1);
|
||||||
|
out[0] = b;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Encode a functional key with modifiers into terminal input bytes. */
|
||||||
|
export function encodeKey(key: VtKey, mods: number, cursorAppMode: boolean): Uint8Array {
|
||||||
|
const shift: boolean = (mods & VtModifier.SHIFT) !== 0;
|
||||||
|
const alt: boolean = (mods & VtModifier.ALT) !== 0;
|
||||||
|
const ctrl: boolean = (mods & VtModifier.CTRL) !== 0;
|
||||||
|
const modParam: number = 1 + (shift ? 1 : 0) + (alt ? 2 : 0) + (ctrl ? 4 : 0);
|
||||||
|
const hasMods: boolean = mods !== VtModifier.NONE;
|
||||||
|
|
||||||
|
let arrow: string = '';
|
||||||
|
if (key === VtKey.UP) {
|
||||||
|
arrow = 'A';
|
||||||
|
} else if (key === VtKey.DOWN) {
|
||||||
|
arrow = 'B';
|
||||||
|
} else if (key === VtKey.RIGHT) {
|
||||||
|
arrow = 'C';
|
||||||
|
} else if (key === VtKey.LEFT) {
|
||||||
|
arrow = 'D';
|
||||||
|
}
|
||||||
|
if (arrow !== '') {
|
||||||
|
if (!hasMods) {
|
||||||
|
return cursorAppMode ? ss3Seq(arrow) : csiSeq(arrow);
|
||||||
|
}
|
||||||
|
return csiSeq(`1;${modParam}${arrow}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key === VtKey.HOME) {
|
||||||
|
return hasMods ? csiSeq(`1;${modParam}H`) : csiSeq('H');
|
||||||
|
}
|
||||||
|
if (key === VtKey.END) {
|
||||||
|
return hasMods ? csiSeq(`1;${modParam}F`) : csiSeq('F');
|
||||||
|
}
|
||||||
|
if (key === VtKey.PAGE_UP) {
|
||||||
|
return hasMods ? csiSeq(`5;${modParam}~`) : csiSeq('5~');
|
||||||
|
}
|
||||||
|
if (key === VtKey.PAGE_DOWN) {
|
||||||
|
return hasMods ? csiSeq(`6;${modParam}~`) : csiSeq('6~');
|
||||||
|
}
|
||||||
|
if (key === VtKey.INSERT) {
|
||||||
|
return hasMods ? csiSeq(`2;${modParam}~`) : csiSeq('2~');
|
||||||
|
}
|
||||||
|
if (key === VtKey.DELETE) {
|
||||||
|
return hasMods ? csiSeq(`3;${modParam}~`) : csiSeq('3~');
|
||||||
|
}
|
||||||
|
if (key === VtKey.BACKSPACE) {
|
||||||
|
if (alt) {
|
||||||
|
const out: Uint8Array = new Uint8Array(2);
|
||||||
|
out[0] = ESC;
|
||||||
|
out[1] = 0x7f;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
return single(0x7f);
|
||||||
|
}
|
||||||
|
if (key === VtKey.ENTER) {
|
||||||
|
return single(0x0d);
|
||||||
|
}
|
||||||
|
if (key === VtKey.ESCAPE) {
|
||||||
|
return single(ESC);
|
||||||
|
}
|
||||||
|
if (key === VtKey.TAB) {
|
||||||
|
return shift ? csiSeq('Z') : single(0x09);
|
||||||
|
}
|
||||||
|
if (key === VtKey.SPACE) {
|
||||||
|
return ctrl ? single(0x00) : single(0x20);
|
||||||
|
}
|
||||||
|
|
||||||
|
let pfKey: string = '';
|
||||||
|
if (key === VtKey.F1) {
|
||||||
|
pfKey = 'P';
|
||||||
|
} else if (key === VtKey.F2) {
|
||||||
|
pfKey = 'Q';
|
||||||
|
} else if (key === VtKey.F3) {
|
||||||
|
pfKey = 'R';
|
||||||
|
} else if (key === VtKey.F4) {
|
||||||
|
pfKey = 'S';
|
||||||
|
}
|
||||||
|
if (pfKey !== '') {
|
||||||
|
return hasMods ? csiSeq(`1;${modParam}${pfKey}`) : ss3Seq(pfKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
let tilde: number = 0;
|
||||||
|
if (key === VtKey.F5) {
|
||||||
|
tilde = 15;
|
||||||
|
} else if (key === VtKey.F6) {
|
||||||
|
tilde = 17;
|
||||||
|
} else if (key === VtKey.F7) {
|
||||||
|
tilde = 18;
|
||||||
|
} else if (key === VtKey.F8) {
|
||||||
|
tilde = 19;
|
||||||
|
} else if (key === VtKey.F9) {
|
||||||
|
tilde = 20;
|
||||||
|
} else if (key === VtKey.F10) {
|
||||||
|
tilde = 21;
|
||||||
|
} else if (key === VtKey.F11) {
|
||||||
|
tilde = 23;
|
||||||
|
} else if (key === VtKey.F12) {
|
||||||
|
tilde = 24;
|
||||||
|
}
|
||||||
|
if (tilde > 0) {
|
||||||
|
return hasMods ? csiSeq(`${tilde};${modParam}~`) : csiSeq(`${tilde}~`);
|
||||||
|
}
|
||||||
|
return new Uint8Array(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Plain text as UTF-8 bytes (soft keyboard / IME commit). */
|
||||||
|
export function encodeText(text: string): Uint8Array {
|
||||||
|
return utf8ToBytes(text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Paste, optionally wrapped in bracketed-paste markers (DECSET 2004). */
|
||||||
|
export function encodePaste(text: string, bracketed: boolean): Uint8Array {
|
||||||
|
if (!bracketed) {
|
||||||
|
return utf8ToBytes(text);
|
||||||
|
}
|
||||||
|
return concatBytes(
|
||||||
|
concatBytes(csiSeq('200~'), utf8ToBytes(text)),
|
||||||
|
csiSeq('201~')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SGR mouse escape (DECSET 1006). col/row are 0-based cell coordinates;
|
||||||
|
* press=true → 'M', release/motion-end → 'm'. Shift/Alt/Ctrl map to
|
||||||
|
* mouse modifier bits 4/8/16 inside the button byte.
|
||||||
|
*/
|
||||||
|
export function encodeMouseSgr(button: number, press: boolean, col: number, row: number, mods: number): Uint8Array {
|
||||||
|
let b: number = button;
|
||||||
|
if ((mods & VtModifier.SHIFT) !== 0) {
|
||||||
|
b += 4;
|
||||||
|
}
|
||||||
|
if ((mods & VtModifier.ALT) !== 0) {
|
||||||
|
b += 8;
|
||||||
|
}
|
||||||
|
if ((mods & VtModifier.CTRL) !== 0) {
|
||||||
|
b += 16;
|
||||||
|
}
|
||||||
|
const suffix: string = press ? 'M' : 'm';
|
||||||
|
return csiSeq(`<${b};${col + 1};${row + 1}${suffix}`);
|
||||||
|
}
|
||||||
@@ -0,0 +1,532 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Haven for HarmonyOS contributors.
|
||||||
|
* Port of Haven by GlassHaven (AGPL-3.0). See LICENSE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* VT500-series parser state machine (after Paul Williams' VT100 parser
|
||||||
|
* state table, trimmed): UTF-8 decoding + C0/C1 controls + ESC / CSI /
|
||||||
|
* OSC / DCS / SS3 sequences, dispatched into a VtSink. The parser owns no
|
||||||
|
* screen state; all interpretation lives in the sink.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface VtSink {
|
||||||
|
/** Printable codepoint (UTF-8 decoding already applied). */
|
||||||
|
print(cp: number): void;
|
||||||
|
/** C0 control executed (BEL/BS/TAB/LF/CR/...). ESC/CAN/SUB/DEL never arrive here. */
|
||||||
|
executeC0(b: number): void;
|
||||||
|
/** ESC <intermediates> <final>; SS3 arrives as intermediates=[0x4f]. */
|
||||||
|
escDispatch(intermediates: Uint8Array, final: number): void;
|
||||||
|
/**
|
||||||
|
* CSI dispatch. Omitted parameter segments are -1 (e.g. "CSI ;5H" →
|
||||||
|
* [-1, 5]); a parameterless CSI has an empty list. ':' sub-parameters
|
||||||
|
* are flattened into the list as separate values.
|
||||||
|
*/
|
||||||
|
csiDispatch(isPrivate: boolean, params: number[], intermediates: Uint8Array, final: number): void;
|
||||||
|
/** OSC body between the introducer and its BEL/ST terminator. */
|
||||||
|
oscDispatch(payload: Uint8Array): void;
|
||||||
|
/** DCS: prefix = param/intermediate bytes + hook byte (>=0x40), data = rest up to ST. */
|
||||||
|
dcsDispatch(prefix: Uint8Array, data: Uint8Array): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum ParserState {
|
||||||
|
GROUND = 0,
|
||||||
|
ESC = 1,
|
||||||
|
ESC_INTERMEDIATE = 2,
|
||||||
|
SS3 = 3,
|
||||||
|
CSI = 4,
|
||||||
|
OSC = 5,
|
||||||
|
OSC_ESC = 6,
|
||||||
|
DCS_PREFIX = 7,
|
||||||
|
DCS_DATA = 8,
|
||||||
|
DCS_ESC = 9,
|
||||||
|
STR = 10,
|
||||||
|
STR_ESC = 11
|
||||||
|
}
|
||||||
|
|
||||||
|
const MAX_PARAMS: number = 32;
|
||||||
|
const MAX_CSI_INTERMEDIATES: number = 8;
|
||||||
|
const MAX_ESC_INTERMEDIATES: number = 8;
|
||||||
|
const OSC_BUF_CAPACITY: number = 65536;
|
||||||
|
const DCS_PREFIX_CAPACITY: number = 32;
|
||||||
|
const DCS_DATA_CAPACITY: number = 16384;
|
||||||
|
const MAX_PARAM_VALUE: number = 65535;
|
||||||
|
|
||||||
|
const EMPTY_BYTES: Uint8Array = new Uint8Array(0);
|
||||||
|
|
||||||
|
export class VtParser {
|
||||||
|
private sink: VtSink;
|
||||||
|
private state: ParserState = ParserState.GROUND;
|
||||||
|
|
||||||
|
// UTF-8 decoding
|
||||||
|
private utfAcc: number = 0;
|
||||||
|
private utfTotal: number = 0;
|
||||||
|
private utfPending: number = 0;
|
||||||
|
private utfMin: number = 0x80;
|
||||||
|
private utfMax: number = 0xbf;
|
||||||
|
|
||||||
|
// ESC intermediates (also reused to synthesize SS3 dispatch)
|
||||||
|
private escIntermediates: Uint8Array = new Uint8Array(MAX_ESC_INTERMEDIATES);
|
||||||
|
private escInterLen: number = 0;
|
||||||
|
|
||||||
|
// CSI collection
|
||||||
|
private csiPrivate: boolean = false;
|
||||||
|
private csiIgnore: boolean = false;
|
||||||
|
private csiParams: number[] = [];
|
||||||
|
private csiCurParam: number = 0;
|
||||||
|
private csiParamStarted: boolean = false;
|
||||||
|
private csiIntermediates: Uint8Array = new Uint8Array(MAX_CSI_INTERMEDIATES);
|
||||||
|
private csiInterLen: number = 0;
|
||||||
|
|
||||||
|
// OSC string
|
||||||
|
private oscBuf: Uint8Array = new Uint8Array(OSC_BUF_CAPACITY);
|
||||||
|
private oscLen: number = 0;
|
||||||
|
private oscOverflow: boolean = false;
|
||||||
|
|
||||||
|
// DCS string
|
||||||
|
private dcsPrefix: Uint8Array = new Uint8Array(DCS_PREFIX_CAPACITY);
|
||||||
|
private dcsPrefixLen: number = 0;
|
||||||
|
private dcsData: Uint8Array = new Uint8Array(DCS_DATA_CAPACITY);
|
||||||
|
private dcsDataLen: number = 0;
|
||||||
|
private dcsDataOverflow: boolean = false;
|
||||||
|
|
||||||
|
constructor(sink: VtSink) {
|
||||||
|
this.sink = sink;
|
||||||
|
}
|
||||||
|
|
||||||
|
getState(): ParserState {
|
||||||
|
return this.state;
|
||||||
|
}
|
||||||
|
|
||||||
|
feed(data: Uint8Array): void {
|
||||||
|
let i: number = 0;
|
||||||
|
while (i < data.length) {
|
||||||
|
if (this.step(data[i])) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Process one byte; false = reprocess the same byte in the new state. */
|
||||||
|
private step(b: number): boolean {
|
||||||
|
// CAN / SUB abort any sequence
|
||||||
|
if (b === 0x18 || b === 0x1a) {
|
||||||
|
this.state = ParserState.GROUND;
|
||||||
|
this.utfPending = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b === 0x1b) {
|
||||||
|
switch (this.state) {
|
||||||
|
case ParserState.OSC:
|
||||||
|
this.state = ParserState.OSC_ESC;
|
||||||
|
return true;
|
||||||
|
case ParserState.DCS_DATA:
|
||||||
|
this.state = ParserState.DCS_ESC;
|
||||||
|
return true;
|
||||||
|
case ParserState.STR:
|
||||||
|
this.state = ParserState.STR_ESC;
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
// Any other state: abort current sequence, start a fresh ESC.
|
||||||
|
this.enterEsc();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch (this.state) {
|
||||||
|
case ParserState.GROUND:
|
||||||
|
return this.stepGround(b);
|
||||||
|
case ParserState.ESC:
|
||||||
|
return this.stepEsc(b);
|
||||||
|
case ParserState.ESC_INTERMEDIATE:
|
||||||
|
return this.stepEscIntermediate(b);
|
||||||
|
case ParserState.SS3:
|
||||||
|
return this.stepSs3(b);
|
||||||
|
case ParserState.CSI:
|
||||||
|
return this.stepCsi(b);
|
||||||
|
case ParserState.OSC:
|
||||||
|
return this.stepOsc(b);
|
||||||
|
case ParserState.OSC_ESC:
|
||||||
|
return this.stepOscEsc(b);
|
||||||
|
case ParserState.DCS_PREFIX:
|
||||||
|
return this.stepDcsPrefix(b);
|
||||||
|
case ParserState.DCS_DATA:
|
||||||
|
return this.stepDcsData(b);
|
||||||
|
case ParserState.DCS_ESC:
|
||||||
|
return this.stepDcsEsc(b);
|
||||||
|
case ParserState.STR:
|
||||||
|
return true; // swallow everything inside SOS/PM/APC
|
||||||
|
case ParserState.STR_ESC:
|
||||||
|
if (b === 0x5c) {
|
||||||
|
this.state = ParserState.GROUND;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
this.enterEsc();
|
||||||
|
return false; // reprocess as new escape sequence
|
||||||
|
default:
|
||||||
|
this.state = ParserState.GROUND;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private stepGround(b: number): boolean {
|
||||||
|
if (b < 0x20) {
|
||||||
|
if (this.utfPending > 0) {
|
||||||
|
this.utfPending = 0;
|
||||||
|
this.sink.print(0xfffd);
|
||||||
|
}
|
||||||
|
this.sink.executeC0(b);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b === 0x7f) {
|
||||||
|
return true; // DEL ignored
|
||||||
|
}
|
||||||
|
if (this.utfPending > 0) {
|
||||||
|
if (b >= 0x80 && b <= 0xbf) {
|
||||||
|
const isFirstContinuation: boolean = this.utfPending === this.utfTotal;
|
||||||
|
if (isFirstContinuation && (b < this.utfMin || b > this.utfMax)) {
|
||||||
|
// Overlong / surrogate / out-of-range sequence head.
|
||||||
|
this.utfPending = 0;
|
||||||
|
this.sink.print(0xfffd);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
this.utfAcc = (this.utfAcc << 6) | (b & 0x3f);
|
||||||
|
this.utfPending--;
|
||||||
|
if (this.utfPending === 0) {
|
||||||
|
this.sink.print(this.utfAcc);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// Interrupted multi-byte sequence: emit FFFD, reprocess this byte.
|
||||||
|
this.utfPending = 0;
|
||||||
|
this.sink.print(0xfffd);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (b >= 0xc2 && b <= 0xdf) {
|
||||||
|
this.utfAcc = b & 0x1f;
|
||||||
|
this.utfTotal = 2;
|
||||||
|
this.utfPending = 1;
|
||||||
|
this.utfMin = 0x80;
|
||||||
|
this.utfMax = 0xbf;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b >= 0xe0 && b <= 0xef) {
|
||||||
|
this.utfAcc = b & 0x0f;
|
||||||
|
this.utfTotal = 3;
|
||||||
|
this.utfPending = 2;
|
||||||
|
this.utfMin = (b === 0xe0) ? 0xa0 : 0x80; // no overlong
|
||||||
|
this.utfMax = (b === 0xed) ? 0x9f : 0xbf; // no surrogates
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b >= 0xf0 && b <= 0xf4) {
|
||||||
|
this.utfAcc = b & 0x07;
|
||||||
|
this.utfTotal = 4;
|
||||||
|
this.utfPending = 3;
|
||||||
|
this.utfMin = (b === 0xf0) ? 0x90 : 0x80; // no overlong
|
||||||
|
this.utfMax = (b === 0xf4) ? 0x8f : 0xbf; // <= U+10FFFF
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// C1 shortcuts (valid in 8-bit streams)
|
||||||
|
if (b === 0x9b) {
|
||||||
|
this.enterCsi();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b === 0x9d) {
|
||||||
|
this.enterOsc();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b === 0x90) {
|
||||||
|
this.enterDcs();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b === 0x84) {
|
||||||
|
this.sink.escDispatch(EMPTY_BYTES, 0x44); // IND
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b === 0x85) {
|
||||||
|
this.sink.escDispatch(EMPTY_BYTES, 0x45); // NEL
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b === 0x88) {
|
||||||
|
this.sink.escDispatch(EMPTY_BYTES, 0x48); // HTS
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b === 0x8d) {
|
||||||
|
this.sink.escDispatch(EMPTY_BYTES, 0x4d); // RI
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b >= 0x80 && b <= 0x9f) {
|
||||||
|
return true; // other C1 ignored
|
||||||
|
}
|
||||||
|
if (b >= 0xa0 && b <= 0xff) {
|
||||||
|
// stray continuation byte or invalid start (0xc0/0xc1/0xf5-0xff)
|
||||||
|
this.sink.print(0xfffd);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
this.sink.print(b); // printable ASCII 0x20-0x7e
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private stepEsc(b: number): boolean {
|
||||||
|
if (b >= 0x20 && b <= 0x2f) {
|
||||||
|
this.pushEscIntermediate(b);
|
||||||
|
this.state = ParserState.ESC_INTERMEDIATE;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b >= 0x30 && b <= 0x7e) {
|
||||||
|
if (this.escInterLen === 0) {
|
||||||
|
if (b === 0x5b) { // [
|
||||||
|
this.enterCsi();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b === 0x5d) { // ]
|
||||||
|
this.enterOsc();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b === 0x50) { // P
|
||||||
|
this.enterDcs();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b === 0x58 || b === 0x5e || b === 0x5f) { // X ^ _
|
||||||
|
this.state = ParserState.STR;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b === 0x4f) { // O
|
||||||
|
this.state = ParserState.SS3;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.sink.escDispatch(this.sliceEscIntermediates(), b);
|
||||||
|
this.state = ParserState.GROUND;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b < 0x20) {
|
||||||
|
this.sink.executeC0(b);
|
||||||
|
return true; // C0 executes inside ESC, state kept
|
||||||
|
}
|
||||||
|
return true; // DEL ignored
|
||||||
|
}
|
||||||
|
|
||||||
|
private stepEscIntermediate(b: number): boolean {
|
||||||
|
if (b >= 0x20 && b <= 0x2f) {
|
||||||
|
this.pushEscIntermediate(b);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b >= 0x30 && b <= 0x7e) {
|
||||||
|
this.sink.escDispatch(this.sliceEscIntermediates(), b);
|
||||||
|
this.state = ParserState.GROUND;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b < 0x20) {
|
||||||
|
this.sink.executeC0(b);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private stepSs3(b: number): boolean {
|
||||||
|
this.state = ParserState.GROUND;
|
||||||
|
if (b >= 0x30 && b <= 0x7e) {
|
||||||
|
const inters: Uint8Array = new Uint8Array(1);
|
||||||
|
inters[0] = 0x4f;
|
||||||
|
this.sink.escDispatch(inters, b);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private stepCsi(b: number): boolean {
|
||||||
|
if (b >= 0x30 && b <= 0x39) {
|
||||||
|
if (this.csiIgnore) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
this.csiParamStarted = true;
|
||||||
|
this.csiCurParam = this.csiCurParam * 10 + (b - 0x30);
|
||||||
|
if (this.csiCurParam > MAX_PARAM_VALUE) {
|
||||||
|
this.csiCurParam = MAX_PARAM_VALUE;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b === 0x3b || b === 0x3a) { // ; or : (sub-params flattened)
|
||||||
|
if (this.csiIgnore) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
this.pushCsiParam(this.csiParamStarted ? this.csiCurParam : -1);
|
||||||
|
this.csiParamStarted = false;
|
||||||
|
this.csiCurParam = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b >= 0x3c && b <= 0x3f) { // < = > ?
|
||||||
|
if (!this.csiPrivate && this.csiParams.length === 0 && !this.csiParamStarted) {
|
||||||
|
this.csiPrivate = true;
|
||||||
|
} else {
|
||||||
|
this.csiIgnore = true; // private marker mid-sequence: ignore all
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b >= 0x20 && b <= 0x2f) {
|
||||||
|
if (this.csiIgnore) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (this.csiInterLen < MAX_CSI_INTERMEDIATES) {
|
||||||
|
this.csiIntermediates[this.csiInterLen] = b;
|
||||||
|
this.csiInterLen++;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b >= 0x40 && b <= 0x7e) {
|
||||||
|
if (!this.csiIgnore) {
|
||||||
|
if (this.csiParamStarted) {
|
||||||
|
this.pushCsiParam(this.csiCurParam);
|
||||||
|
}
|
||||||
|
this.sink.csiDispatch(
|
||||||
|
this.csiPrivate,
|
||||||
|
this.csiParams,
|
||||||
|
this.csiIntermediates.slice(0, this.csiInterLen),
|
||||||
|
b
|
||||||
|
);
|
||||||
|
}
|
||||||
|
this.state = ParserState.GROUND;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b < 0x20) {
|
||||||
|
this.sink.executeC0(b);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return true; // DEL ignored
|
||||||
|
}
|
||||||
|
|
||||||
|
private stepOsc(b: number): boolean {
|
||||||
|
if (b === 0x07) { // BEL terminator
|
||||||
|
this.dispatchOsc();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b >= 0x20 || (b >= 0x80 && b <= 0xff)) {
|
||||||
|
if (this.oscLen < OSC_BUF_CAPACITY) {
|
||||||
|
this.oscBuf[this.oscLen] = b;
|
||||||
|
this.oscLen++;
|
||||||
|
} else {
|
||||||
|
this.oscOverflow = true;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return true; // other C0 ignored
|
||||||
|
}
|
||||||
|
|
||||||
|
private stepOscEsc(b: number): boolean {
|
||||||
|
if (b === 0x5c) { // ST
|
||||||
|
this.dispatchOsc();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// Aborted OSC: the ESC starts a new sequence, this byte reprocesses.
|
||||||
|
this.oscLen = 0;
|
||||||
|
this.oscOverflow = false;
|
||||||
|
this.enterEsc();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private stepDcsPrefix(b: number): boolean {
|
||||||
|
if ((b >= 0x30 && b <= 0x3f) || (b >= 0x20 && b <= 0x2f)) {
|
||||||
|
if (this.dcsPrefixLen < DCS_PREFIX_CAPACITY) {
|
||||||
|
this.dcsPrefix[this.dcsPrefixLen] = b;
|
||||||
|
this.dcsPrefixLen++;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (b >= 0x40 && b <= 0x7e) {
|
||||||
|
// Hook byte joins the prefix; following bytes are data.
|
||||||
|
if (this.dcsPrefixLen < DCS_PREFIX_CAPACITY) {
|
||||||
|
this.dcsPrefix[this.dcsPrefixLen] = b;
|
||||||
|
this.dcsPrefixLen++;
|
||||||
|
}
|
||||||
|
this.dcsDataLen = 0;
|
||||||
|
this.dcsDataOverflow = false;
|
||||||
|
this.state = ParserState.DCS_DATA;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return true; // C0 / DEL ignored
|
||||||
|
}
|
||||||
|
|
||||||
|
private stepDcsData(b: number): boolean {
|
||||||
|
if (b >= 0x20 || (b >= 0x80 && b <= 0xff)) {
|
||||||
|
if (this.dcsDataLen < DCS_DATA_CAPACITY) {
|
||||||
|
this.dcsData[this.dcsDataLen] = b;
|
||||||
|
this.dcsDataLen++;
|
||||||
|
} else {
|
||||||
|
this.dcsDataOverflow = true;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return true; // C0 / DEL ignored
|
||||||
|
}
|
||||||
|
|
||||||
|
private stepDcsEsc(b: number): boolean {
|
||||||
|
if (b === 0x5c) { // ST
|
||||||
|
this.state = ParserState.GROUND;
|
||||||
|
if (!this.dcsDataOverflow && this.dcsPrefixLen > 0) {
|
||||||
|
this.sink.dcsDispatch(
|
||||||
|
this.dcsPrefix.slice(0, this.dcsPrefixLen),
|
||||||
|
this.dcsData.slice(0, this.dcsDataLen)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// Aborted DCS: no dispatch, ESC starts a new sequence.
|
||||||
|
this.enterEsc();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private dispatchOsc(): void {
|
||||||
|
this.state = ParserState.GROUND;
|
||||||
|
const overflow: boolean = this.oscOverflow;
|
||||||
|
const len: number = this.oscLen;
|
||||||
|
this.oscLen = 0;
|
||||||
|
this.oscOverflow = false;
|
||||||
|
if (!overflow) {
|
||||||
|
this.sink.oscDispatch(this.oscBuf.slice(0, len));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private enterEsc(): void {
|
||||||
|
this.escInterLen = 0;
|
||||||
|
this.state = ParserState.ESC;
|
||||||
|
}
|
||||||
|
|
||||||
|
private enterCsi(): void {
|
||||||
|
this.csiPrivate = false;
|
||||||
|
this.csiIgnore = false;
|
||||||
|
this.csiParams = [];
|
||||||
|
this.csiCurParam = 0;
|
||||||
|
this.csiParamStarted = false;
|
||||||
|
this.csiInterLen = 0;
|
||||||
|
this.state = ParserState.CSI;
|
||||||
|
}
|
||||||
|
|
||||||
|
private enterOsc(): void {
|
||||||
|
this.oscLen = 0;
|
||||||
|
this.oscOverflow = false;
|
||||||
|
this.state = ParserState.OSC;
|
||||||
|
}
|
||||||
|
|
||||||
|
private enterDcs(): void {
|
||||||
|
this.dcsPrefixLen = 0;
|
||||||
|
this.dcsDataLen = 0;
|
||||||
|
this.dcsDataOverflow = false;
|
||||||
|
this.state = ParserState.DCS_PREFIX;
|
||||||
|
}
|
||||||
|
|
||||||
|
private pushEscIntermediate(b: number): void {
|
||||||
|
if (this.escInterLen < MAX_ESC_INTERMEDIATES) {
|
||||||
|
this.escIntermediates[this.escInterLen] = b;
|
||||||
|
this.escInterLen++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private sliceEscIntermediates(): Uint8Array {
|
||||||
|
return this.escIntermediates.slice(0, this.escInterLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
private pushCsiParam(v: number): void {
|
||||||
|
if (this.csiParams.length < MAX_PARAMS) {
|
||||||
|
this.csiParams.push(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,501 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Haven for HarmonyOS contributors.
|
||||||
|
* Port of Haven by GlassHaven (AGPL-3.0). See LICENSE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Self-contained emulator tests: drive byte streams through a
|
||||||
|
* TerminalEmulator on small grids and verify buffer state, replies and
|
||||||
|
* input encodings. Used by the diagnostics page (button 11) and runnable
|
||||||
|
* anywhere (no UI or device dependencies).
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { TerminalEmulator } from './TerminalEmulator';
|
||||||
|
import { ATTR_WIDE, ATTR_WIDE_CONT, ATTR_BOLD, ATTR_REVERSE } from './TerminalBuffer';
|
||||||
|
import { VtKey, VtModifier, encodeKey, encodePaste, encodeMouseSgr } from './VtInput';
|
||||||
|
import { wcwidth } from './wcwidth';
|
||||||
|
import { utf8ToBytes, bytesToUtf8 } from '../ssh/SshWire';
|
||||||
|
|
||||||
|
export interface VtTestResult {
|
||||||
|
name: string;
|
||||||
|
ok: boolean;
|
||||||
|
detail: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TestContext {
|
||||||
|
emu: TerminalEmulator;
|
||||||
|
replies: Uint8Array[];
|
||||||
|
}
|
||||||
|
|
||||||
|
function newEmu(cols: number, rows: number): TestContext {
|
||||||
|
const replies: Uint8Array[] = [];
|
||||||
|
const emu: TerminalEmulator = new TerminalEmulator(cols, rows, (data: Uint8Array): void => {
|
||||||
|
replies.push(data);
|
||||||
|
});
|
||||||
|
return { emu: emu, replies: replies };
|
||||||
|
}
|
||||||
|
|
||||||
|
function feedStr(ctx: TestContext, s: string): void {
|
||||||
|
ctx.emu.feed(utf8ToBytes(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
function repliesText(ctx: TestContext): string {
|
||||||
|
let total: number = 0;
|
||||||
|
for (let i: number = 0; i < ctx.replies.length; i++) {
|
||||||
|
total += ctx.replies[i].length;
|
||||||
|
}
|
||||||
|
const all: Uint8Array = new Uint8Array(total);
|
||||||
|
let off: number = 0;
|
||||||
|
for (let i: number = 0; i < ctx.replies.length; i++) {
|
||||||
|
const r: Uint8Array = ctx.replies[i];
|
||||||
|
for (let j: number = 0; j < r.length; j++) {
|
||||||
|
all[off + j] = r[j];
|
||||||
|
}
|
||||||
|
off += r.length;
|
||||||
|
}
|
||||||
|
return bytesToUtf8(all);
|
||||||
|
}
|
||||||
|
|
||||||
|
function cellChar(ctx: TestContext, r: number, c: number): string {
|
||||||
|
return String.fromCharCode(ctx.emu.getBuffer().cellAt(r, c).cp);
|
||||||
|
}
|
||||||
|
|
||||||
|
function cellCp(ctx: TestContext, r: number, c: number): number {
|
||||||
|
return ctx.emu.getBuffer().cellAt(r, c).cp;
|
||||||
|
}
|
||||||
|
|
||||||
|
function cellFg(ctx: TestContext, r: number, c: number): number {
|
||||||
|
return ctx.emu.getBuffer().cellAt(r, c).fg;
|
||||||
|
}
|
||||||
|
|
||||||
|
function cellAttr(ctx: TestContext, r: number, c: number): number {
|
||||||
|
return ctx.emu.getBuffer().cellAt(r, c).attr;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Expect {
|
||||||
|
errs: string[] = [];
|
||||||
|
|
||||||
|
num(actual: number, expected: number, label: string): void {
|
||||||
|
if (actual !== expected) {
|
||||||
|
this.errs.push(`${label}: expected ${expected}, got ${actual}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
str(actual: string, expected: string, label: string): void {
|
||||||
|
if (actual !== expected) {
|
||||||
|
this.errs.push(`${label}: expected "${expected}", got "${actual}"`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool(actual: boolean, expected: boolean, label: string): void {
|
||||||
|
if (actual !== expected) {
|
||||||
|
this.errs.push(`${label}: expected ${expected}, got ${actual}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contains(haystack: string, needle: string, label: string): void {
|
||||||
|
if (haystack.indexOf(needle) < 0) {
|
||||||
|
this.errs.push(`${label}: "${needle}" not found in "${haystack}"`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ok(): boolean {
|
||||||
|
return this.errs.length === 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
detail(): string {
|
||||||
|
if (this.errs.length === 0) {
|
||||||
|
return 'ok';
|
||||||
|
}
|
||||||
|
return this.errs.join('; ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function finish(results: VtTestResult[], name: string, e: Expect): void {
|
||||||
|
results.push({ name: name, ok: e.ok(), detail: e.detail() });
|
||||||
|
}
|
||||||
|
|
||||||
|
function failWith(results: VtTestResult[], name: string, err: Object): void {
|
||||||
|
const msg: string = err instanceof Error ? err.message : String(err);
|
||||||
|
results.push({ name: name, ok: false, detail: `exception: ${msg}` });
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1: print & deferred autowrap
|
||||||
|
function testPrintWrap(results: VtTestResult[]): void {
|
||||||
|
const name: string = 'print & autowrap';
|
||||||
|
try {
|
||||||
|
const ctx: TestContext = newEmu(20, 5);
|
||||||
|
const e: Expect = new Expect();
|
||||||
|
feedStr(ctx, 'Hello');
|
||||||
|
e.str(cellChar(ctx, 0, 0), 'H', 'H at 0,0');
|
||||||
|
e.num(ctx.emu.getCursorRow(), 0, 'cursor row');
|
||||||
|
e.num(ctx.emu.getCursorCol(), 5, 'cursor col');
|
||||||
|
// 15 more chars fill the 20-col row exactly; wrap must be deferred
|
||||||
|
feedStr(ctx, 'AAAAAAAAAAAAAAA');
|
||||||
|
e.num(ctx.emu.getCursorRow(), 0, 'row stays before deferred wrap');
|
||||||
|
e.num(ctx.emu.getCursorCol(), 19, 'cursor clamped to last col');
|
||||||
|
feedStr(ctx, 'B');
|
||||||
|
e.num(ctx.emu.getCursorRow(), 1, 'next print wraps to row 1');
|
||||||
|
e.str(cellChar(ctx, 1, 0), 'B', 'B at 1,0');
|
||||||
|
e.str(cellChar(ctx, 0, 19), 'A', 'A still at 0,19');
|
||||||
|
finish(results, name, e);
|
||||||
|
} catch (err) {
|
||||||
|
failWith(results, name, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2: CJK double-width cells
|
||||||
|
function testCjkWide(results: VtTestResult[]): void {
|
||||||
|
const name: string = 'CJK wide cells';
|
||||||
|
try {
|
||||||
|
const ctx: TestContext = newEmu(20, 5);
|
||||||
|
const e: Expect = new Expect();
|
||||||
|
e.num(wcwidth(0x4f60), 2, 'wcwidth(你)');
|
||||||
|
feedStr(ctx, '你好');
|
||||||
|
e.num(cellCp(ctx, 0, 0), 0x4f60, '你 at 0,0');
|
||||||
|
e.bool((cellAttr(ctx, 0, 0) & ATTR_WIDE) !== 0, true, 'WIDE flag at 0,0');
|
||||||
|
e.num(cellCp(ctx, 0, 1), 0x4f60, 'cont half at 0,1');
|
||||||
|
e.bool((cellAttr(ctx, 0, 1) & ATTR_WIDE_CONT) !== 0, true, 'WIDE_CONT at 0,1');
|
||||||
|
e.num(cellCp(ctx, 0, 2), 0x597d, '好 at 0,2');
|
||||||
|
e.num(ctx.emu.getCursorCol(), 4, 'cursor after 你好');
|
||||||
|
// wide char at the last column wraps instead of splitting
|
||||||
|
feedStr(ctx, '\u001b[1;19H你');
|
||||||
|
e.num(cellCp(ctx, 0, 18), 0x4f60, 'wide head at col 18');
|
||||||
|
feedStr(ctx, '好');
|
||||||
|
e.num(cellCp(ctx, 1, 0), 0x597d, '好 wrapped to next row');
|
||||||
|
finish(results, name, e);
|
||||||
|
} catch (err) {
|
||||||
|
failWith(results, name, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3: SGR attributes and colors (16-color, 256-color, truecolor)
|
||||||
|
function testSgr(results: VtTestResult[]): void {
|
||||||
|
const name: string = 'SGR colors';
|
||||||
|
try {
|
||||||
|
const ctx: TestContext = newEmu(20, 5);
|
||||||
|
const e: Expect = new Expect();
|
||||||
|
feedStr(ctx, '\u001b[1;31mA');
|
||||||
|
e.num(cellFg(ctx, 0, 0), 1, 'fg red');
|
||||||
|
e.bool((cellAttr(ctx, 0, 0) & ATTR_BOLD) !== 0, true, 'bold');
|
||||||
|
feedStr(ctx, '\u001b[38;2;10;20;30mB');
|
||||||
|
e.num(cellFg(ctx, 0, 1), 0x1000000 | 0x0a141e, 'truecolor fg');
|
||||||
|
feedStr(ctx, '\u001b[38;5;196mC');
|
||||||
|
e.num(cellFg(ctx, 0, 2), 196, '256-color fg');
|
||||||
|
feedStr(ctx, '\u001b[48;5;17m\u001b[7mD');
|
||||||
|
e.num(ctx.emu.getBuffer().cellAt(0, 3).bg, 17, '256-color bg');
|
||||||
|
e.bool((cellAttr(ctx, 0, 3) & ATTR_REVERSE) !== 0, true, 'reverse');
|
||||||
|
feedStr(ctx, '\u001b[0mE');
|
||||||
|
e.num(cellFg(ctx, 0, 4), -1, 'fg reset');
|
||||||
|
e.num(cellAttr(ctx, 0, 4), 0, 'attr reset');
|
||||||
|
finish(results, name, e);
|
||||||
|
} catch (err) {
|
||||||
|
failWith(results, name, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4: ED / EL
|
||||||
|
function testEdEl(results: VtTestResult[]): void {
|
||||||
|
const name: string = 'ED / EL';
|
||||||
|
try {
|
||||||
|
const ctx: TestContext = newEmu(20, 3);
|
||||||
|
const e: Expect = new Expect();
|
||||||
|
feedStr(ctx, 'AAAA\r\nBBBB\r\nCCCC');
|
||||||
|
feedStr(ctx, '\u001b[2J');
|
||||||
|
e.str(cellChar(ctx, 0, 0), ' ', 'ED2 clears');
|
||||||
|
e.num(ctx.emu.getCursorRow(), 2, 'ED2 keeps cursor row');
|
||||||
|
feedStr(ctx, 'ZZZZZ');
|
||||||
|
feedStr(ctx, '\u001b[1K');
|
||||||
|
e.str(cellChar(ctx, 2, 0), ' ', 'EL1 start');
|
||||||
|
e.str(cellChar(ctx, 2, 4), ' ', 'EL1 through cursor');
|
||||||
|
e.str(cellChar(ctx, 2, 5), ' ', 'EL1 past cursor untouched');
|
||||||
|
finish(results, name, e);
|
||||||
|
} catch (err) {
|
||||||
|
failWith(results, name, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5: alternate screen round-trip (DECSET/RST 1049)
|
||||||
|
function testAltScreen(results: VtTestResult[]): void {
|
||||||
|
const name: string = 'alt screen 1049';
|
||||||
|
try {
|
||||||
|
const ctx: TestContext = newEmu(20, 5);
|
||||||
|
const e: Expect = new Expect();
|
||||||
|
feedStr(ctx, 'MAIN');
|
||||||
|
feedStr(ctx, '\u001b[?1049h');
|
||||||
|
e.bool(ctx.emu.isAltScreen(), true, 'alt active');
|
||||||
|
e.num(ctx.emu.getCursorRow(), 0, 'alt home row');
|
||||||
|
feedStr(ctx, 'ALT');
|
||||||
|
e.str(cellChar(ctx, 0, 0), 'A', 'alt content');
|
||||||
|
feedStr(ctx, '\u001b[?1049l');
|
||||||
|
e.bool(ctx.emu.isAltScreen(), false, 'main restored');
|
||||||
|
e.str(cellChar(ctx, 0, 0), 'M', 'main preserved');
|
||||||
|
e.str(cellChar(ctx, 0, 3), 'N', 'main tail preserved');
|
||||||
|
e.num(ctx.emu.getCursorRow(), 0, 'cursor restored row');
|
||||||
|
e.num(ctx.emu.getCursorCol(), 4, 'cursor restored col');
|
||||||
|
finish(results, name, e);
|
||||||
|
} catch (err) {
|
||||||
|
failWith(results, name, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6: scrollback growth on the main screen only
|
||||||
|
function testScrollback(results: VtTestResult[]): void {
|
||||||
|
const name: string = 'scrollback';
|
||||||
|
try {
|
||||||
|
const ctx: TestContext = newEmu(10, 4);
|
||||||
|
const e: Expect = new Expect();
|
||||||
|
let all: string = '';
|
||||||
|
for (let i: number = 1; i <= 25; i++) {
|
||||||
|
all += `L${i}`;
|
||||||
|
if (i < 25) {
|
||||||
|
all += '\r\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
feedStr(ctx, all);
|
||||||
|
e.num(ctx.emu.getBuffer().getScrollbackCount(), 21, '21 lines scrolled back');
|
||||||
|
const first: string = cellChar(ctx, 0, 0);
|
||||||
|
const sc0: string = String.fromCharCode(ctx.emu.getBuffer().getScrollbackLine(0)[0].cp);
|
||||||
|
const sc1: string = String.fromCharCode(ctx.emu.getBuffer().getScrollbackLine(0)[1].cp);
|
||||||
|
e.str(sc0 + sc1, 'L1', 'scrollback head is L1');
|
||||||
|
e.str(first, 'L', 'screen top starts with L');
|
||||||
|
e.str(cellChar(ctx, 0, 1), '2', 'screen top is L22');
|
||||||
|
e.str(cellChar(ctx, 3, 2), '5', 'screen bottom is L25');
|
||||||
|
// alt screen scrolls must not touch scrollback
|
||||||
|
feedStr(ctx, '\u001b[?1049h');
|
||||||
|
for (let i: number = 0; i < 20; i++) {
|
||||||
|
feedStr(ctx, 'x\r\n');
|
||||||
|
}
|
||||||
|
e.num(ctx.emu.getBuffer().getScrollbackCount(), 21, 'alt scrolls bypass scrollback');
|
||||||
|
feedStr(ctx, '\u001b[?1049l');
|
||||||
|
e.num(ctx.emu.getBuffer().getScrollbackCount(), 21, 'still 21 after leaving alt');
|
||||||
|
finish(results, name, e);
|
||||||
|
} catch (err) {
|
||||||
|
failWith(results, name, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7: DECSTBM scroll region + RI, lines outside margins untouched
|
||||||
|
function testScrollRegion(results: VtTestResult[]): void {
|
||||||
|
const name: string = 'scroll region + RI';
|
||||||
|
try {
|
||||||
|
const ctx: TestContext = newEmu(20, 6);
|
||||||
|
const e: Expect = new Expect();
|
||||||
|
feedStr(ctx, 'X');
|
||||||
|
feedStr(ctx, '\u001b[6;1HY');
|
||||||
|
feedStr(ctx, '\u001b[2;5r'); // margins rows 2..5 (1-based) = 1..4
|
||||||
|
feedStr(ctx, '\u001b[2;1H');
|
||||||
|
feedStr(ctx, 'A\r\nB\r\nC\r\nD\r\nE');
|
||||||
|
e.str(cellChar(ctx, 0, 0), 'X', 'row 0 untouched');
|
||||||
|
e.str(cellChar(ctx, 5, 0), 'Y', 'row 5 untouched');
|
||||||
|
e.str(cellChar(ctx, 1, 0), 'B', 'region scrolled up: row 1 = B');
|
||||||
|
e.str(cellChar(ctx, 2, 0), 'C', 'row 2 = C');
|
||||||
|
e.str(cellChar(ctx, 3, 0), 'D', 'row 3 = D');
|
||||||
|
e.str(cellChar(ctx, 4, 0), 'E', 'row 4 = E');
|
||||||
|
e.num(ctx.emu.getBuffer().getScrollbackCount(), 0, 'region scroll does not push scrollback');
|
||||||
|
feedStr(ctx, '\u001b[2;1H\u001bM'); // RI at margin top
|
||||||
|
e.str(cellChar(ctx, 1, 0), ' ', 'RI blanked top margin row');
|
||||||
|
e.str(cellChar(ctx, 2, 0), 'B', 'RI shifted B down');
|
||||||
|
e.str(cellChar(ctx, 4, 0), 'D', 'RI shifted D down');
|
||||||
|
e.str(cellChar(ctx, 0, 0), 'X', 'X survives RI');
|
||||||
|
e.str(cellChar(ctx, 5, 0), 'Y', 'Y survives RI');
|
||||||
|
finish(results, name, e);
|
||||||
|
} catch (err) {
|
||||||
|
failWith(results, name, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 8: DA1/DA2/DSR/XTVERSION replies
|
||||||
|
function testQueries(results: VtTestResult[]): void {
|
||||||
|
const name: string = 'terminal query replies';
|
||||||
|
try {
|
||||||
|
const ctx: TestContext = newEmu(20, 5);
|
||||||
|
const e: Expect = new Expect();
|
||||||
|
feedStr(ctx, '\u001b[c');
|
||||||
|
feedStr(ctx, '\u001b[>c');
|
||||||
|
feedStr(ctx, '\u001b[5n');
|
||||||
|
feedStr(ctx, '\u001b[2;3H\u001b[6n');
|
||||||
|
feedStr(ctx, '\u001bP>|q\u001b\\');
|
||||||
|
const replies: string = repliesText(ctx);
|
||||||
|
e.contains(replies, '\u001b[?1;2c', 'DA1 reply');
|
||||||
|
e.contains(replies, '\u001b[>0;100;0c', 'DA2 reply');
|
||||||
|
e.contains(replies, '\u001b[0n', 'DSR 5 reply');
|
||||||
|
e.contains(replies, '\u001b[2;3R', 'DSR 6 reply');
|
||||||
|
e.contains(replies, '\u001bP>|HavenOH(0.1)\u001b\\', 'XTVERSION reply');
|
||||||
|
finish(results, name, e);
|
||||||
|
} catch (err) {
|
||||||
|
failWith(results, name, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 9: VtInput encodings (keys, paste, SGR mouse)
|
||||||
|
function testInputEncodings(results: VtTestResult[]): void {
|
||||||
|
const name: string = 'input encodings';
|
||||||
|
try {
|
||||||
|
const e: Expect = new Expect();
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.UP, VtModifier.NONE, false)), '\u001b[A', 'UP normal');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.UP, VtModifier.NONE, true)), '\u001bOA', 'UP cursor-app');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.UP, VtModifier.SHIFT, false)), '\u001b[1;2A', 'UP shift');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.HOME, VtModifier.NONE, false)), '\u001b[H', 'HOME');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.END, VtModifier.ALT, false)), '\u001b[1;3F', 'END alt');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.PAGE_UP, VtModifier.NONE, false)), '\u001b[5~', 'PgUp');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.DELETE, VtModifier.CTRL | VtModifier.SHIFT, false)), '\u001b[3;6~', 'Del ctrl+shift');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.F1, VtModifier.NONE, false)), '\u001bOP', 'F1');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.F5, VtModifier.CTRL, false)), '\u001b[15;5~', 'F5 ctrl');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.F12, VtModifier.NONE, false)), '\u001b[24~', 'F12');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.ENTER, VtModifier.NONE, false)), '\r', 'Enter');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.BACKSPACE, VtModifier.NONE, false)), '\u007f', 'Backspace');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.BACKSPACE, VtModifier.ALT, false)), '\u001b\u007f', 'Backspace alt');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.TAB, VtModifier.NONE, false)), '\t', 'Tab');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.TAB, VtModifier.SHIFT, false)), '\u001b[Z', 'Shift+Tab');
|
||||||
|
e.str(bytesToUtf8(encodeKey(VtKey.SPACE, VtModifier.CTRL, false)), '\u0000', 'Ctrl+Space');
|
||||||
|
e.str(bytesToUtf8(encodePaste('hi', true)), '\u001b[200~hi\u001b[201~', 'bracketed paste');
|
||||||
|
e.str(bytesToUtf8(encodePaste('hi', false)), 'hi', 'plain paste');
|
||||||
|
e.str(bytesToUtf8(encodeMouseSgr(0, true, 2, 3, 0)), '\u001b[<0;3;4M', 'mouse press');
|
||||||
|
e.str(bytesToUtf8(encodeMouseSgr(3, false, 1, 1, 0)), '\u001b[<3;2;2m', 'mouse release');
|
||||||
|
e.str(bytesToUtf8(encodeMouseSgr(64, true, 0, 0, VtModifier.ALT)), '\u001b[<72;1;1M', 'wheel up + alt');
|
||||||
|
e.str(bytesToUtf8(encodeMouseSgr(1 | 32, true, 4, 5, VtModifier.CTRL)), '\u001b[<49;5;6M', 'motion + ctrl');
|
||||||
|
finish(results, name, e);
|
||||||
|
} catch (err) {
|
||||||
|
failWith(results, name, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 10: "echo 你好 && vim" byte-stream replay (T7 acceptance)
|
||||||
|
function testVimReplay(results: VtTestResult[]): void {
|
||||||
|
const name: string = 'echo 你好 && vim replay';
|
||||||
|
try {
|
||||||
|
const ctx: TestContext = newEmu(30, 8);
|
||||||
|
const e: Expect = new Expect();
|
||||||
|
// shell echo output
|
||||||
|
feedStr(ctx, 'user@host:~$ echo 你好\r\n');
|
||||||
|
feedStr(ctx, '你好\r\n');
|
||||||
|
feedStr(ctx, 'user@host:~$ vim\r\n');
|
||||||
|
e.num(cellCp(ctx, 0, 18), 0x4f60, '你 on prompt line');
|
||||||
|
e.num(cellCp(ctx, 1, 0), 0x4f60, 'echo output 你');
|
||||||
|
e.num(cellCp(ctx, 1, 2), 0x597d, 'echo output 好');
|
||||||
|
// vim enters alt screen
|
||||||
|
feedStr(ctx, '\u001b[?1049h');
|
||||||
|
e.bool(ctx.emu.isAltScreen(), true, 'vim on alt screen');
|
||||||
|
feedStr(ctx, '\u001b[2J\u001b[H');
|
||||||
|
feedStr(ctx, '~\r\n~\r\n~\r\n~\r\n~\r\n');
|
||||||
|
feedStr(ctx, '\u001b[8;1H-- INSERT --');
|
||||||
|
e.str(cellChar(ctx, 0, 0), '~', 'vim tildes');
|
||||||
|
e.str(cellChar(ctx, 4, 0), '~', 'last tilde');
|
||||||
|
e.str(cellChar(ctx, 7, 0), '-', 'status line');
|
||||||
|
e.str(cellChar(ctx, 7, 1), '-', 'status line body');
|
||||||
|
e.str(cellChar(ctx, 6, 0), ' ', 'row 6 blank');
|
||||||
|
// vim leaves: main screen intact, cursor restored
|
||||||
|
feedStr(ctx, '\u001b[?1049l');
|
||||||
|
e.bool(ctx.emu.isAltScreen(), false, 'back on main screen');
|
||||||
|
e.str(cellChar(ctx, 2, 0), 'u', 'prompt line preserved');
|
||||||
|
e.str(cellChar(ctx, 2, 15), 'm', 'vim cmd preserved');
|
||||||
|
e.num(cellCp(ctx, 0, 20), 0x597d, '好 preserved on prompt line');
|
||||||
|
e.num(ctx.emu.getCursorRow(), 3, 'cursor row restored');
|
||||||
|
e.num(ctx.emu.getCursorCol(), 0, 'cursor col restored');
|
||||||
|
finish(results, name, e);
|
||||||
|
} catch (err) {
|
||||||
|
failWith(results, name, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 11: OSC callbacks (title/cwd/clipboard/notification/hyperlink)
|
||||||
|
function testOsc(results: VtTestResult[]): void {
|
||||||
|
const name: string = 'OSC handling';
|
||||||
|
try {
|
||||||
|
const ctx: TestContext = newEmu(40, 5);
|
||||||
|
const e: Expect = new Expect();
|
||||||
|
let title: string = '';
|
||||||
|
let cwd: string = '';
|
||||||
|
let clip: string = '';
|
||||||
|
let notifTitle: string = '';
|
||||||
|
let notifBody: string = '';
|
||||||
|
const links: string[] = [];
|
||||||
|
ctx.emu.onTitle = (t: string): void => {
|
||||||
|
title = t;
|
||||||
|
};
|
||||||
|
ctx.emu.onCwd = (p: string): void => {
|
||||||
|
cwd = p;
|
||||||
|
};
|
||||||
|
ctx.emu.onClipboard = (t: string): void => {
|
||||||
|
clip = t;
|
||||||
|
};
|
||||||
|
ctx.emu.onNotification = (t: string, b: string): void => {
|
||||||
|
notifTitle = t;
|
||||||
|
notifBody = b;
|
||||||
|
};
|
||||||
|
ctx.emu.onHyperlink = (u: string): void => {
|
||||||
|
links.push(u);
|
||||||
|
};
|
||||||
|
feedStr(ctx, '\u001b]0;My Title\u0007');
|
||||||
|
e.str(title, 'My Title', 'OSC 0 title (BEL)');
|
||||||
|
feedStr(ctx, '\u001b]2;Other\u001b\\');
|
||||||
|
e.str(title, 'Other', 'OSC 2 title (ST)');
|
||||||
|
feedStr(ctx, '\u001b]7;file:///home/user\u001b\\');
|
||||||
|
e.str(cwd, 'file:///home/user', 'OSC 7 cwd');
|
||||||
|
feedStr(ctx, '\u001b]52;c;aGk=\u001b\\');
|
||||||
|
e.str(clip, 'hi', 'OSC 52 clipboard');
|
||||||
|
feedStr(ctx, '\u001b]9;Build;Done\u0007');
|
||||||
|
e.str(notifTitle, 'Build', 'OSC 9 title');
|
||||||
|
e.str(notifBody, 'Done', 'OSC 9 body');
|
||||||
|
feedStr(ctx, '\u001b]8;;https://example.com\u0007link\u001b]8;;\u0007');
|
||||||
|
e.num(links.length, 2, 'link set + cleared');
|
||||||
|
e.str(links.length > 0 ? links[0] : '', 'https://example.com', 'link uri');
|
||||||
|
feedStr(ctx, '\u001b]8;;ftp://x\u0007');
|
||||||
|
e.num(links.length, 2, 'non-whitelisted scheme dropped');
|
||||||
|
finish(results, name, e);
|
||||||
|
} catch (err) {
|
||||||
|
failWith(results, name, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 12: DECALN + ICH/DCH/ECH + REP
|
||||||
|
function testDecalnIchDchRep(results: VtTestResult[]): void {
|
||||||
|
const name: string = 'DECALN / ICH / DCH / ECH / REP';
|
||||||
|
try {
|
||||||
|
const ctx: TestContext = newEmu(20, 5);
|
||||||
|
const e: Expect = new Expect();
|
||||||
|
feedStr(ctx, '\u001b#8');
|
||||||
|
e.str(cellChar(ctx, 0, 0), 'E', 'DECALN top-left');
|
||||||
|
e.str(cellChar(ctx, 4, 19), 'E', 'DECALN bottom-right');
|
||||||
|
e.num(ctx.emu.getCursorRow(), 0, 'DECALN homes cursor');
|
||||||
|
feedStr(ctx, '\u001b[2;3HX');
|
||||||
|
e.str(cellChar(ctx, 1, 2), 'X', 'X written');
|
||||||
|
feedStr(ctx, '\u001b[2;3H\u001b[2P');
|
||||||
|
e.str(cellChar(ctx, 1, 2), 'E', 'DCH removed X');
|
||||||
|
e.str(cellChar(ctx, 1, 19), ' ', 'DCH tail blank');
|
||||||
|
feedStr(ctx, '\u001b[2;3H\u001b[2@');
|
||||||
|
e.str(cellChar(ctx, 1, 2), ' ', 'ICH inserted blank');
|
||||||
|
e.str(cellChar(ctx, 1, 4), 'E', 'ICH shifted E right');
|
||||||
|
feedStr(ctx, '\u001b[1;1HZ');
|
||||||
|
feedStr(ctx, '\u001b[3b');
|
||||||
|
e.str(cellChar(ctx, 0, 0), 'Z', 'REP base char');
|
||||||
|
e.str(cellChar(ctx, 0, 3), 'Z', 'REP filled 3');
|
||||||
|
e.str(cellChar(ctx, 0, 4), 'E', 'REP stops after 3');
|
||||||
|
feedStr(ctx, '\u001b[1;5H\u001b[2X');
|
||||||
|
e.str(cellChar(ctx, 0, 4), ' ', 'ECH cleared 1');
|
||||||
|
e.str(cellChar(ctx, 0, 5), ' ', 'ECH cleared 2');
|
||||||
|
e.str(cellChar(ctx, 0, 6), 'E', 'ECH stops');
|
||||||
|
finish(results, name, e);
|
||||||
|
} catch (err) {
|
||||||
|
failWith(results, name, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Run all emulator self-tests. */
|
||||||
|
export function runVtSelfTest(): VtTestResult[] {
|
||||||
|
const results: VtTestResult[] = [];
|
||||||
|
testPrintWrap(results);
|
||||||
|
testCjkWide(results);
|
||||||
|
testSgr(results);
|
||||||
|
testEdEl(results);
|
||||||
|
testAltScreen(results);
|
||||||
|
testScrollback(results);
|
||||||
|
testScrollRegion(results);
|
||||||
|
testQueries(results);
|
||||||
|
testInputEncodings(results);
|
||||||
|
testVimReplay(results);
|
||||||
|
testOsc(results);
|
||||||
|
testDecalnIchDchRep(results);
|
||||||
|
return results;
|
||||||
|
}
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Haven for HarmonyOS contributors.
|
||||||
|
* Port of Haven by GlassHaven (AGPL-3.0). See LICENSE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Character cell width for terminal grids (after Markus Kuhn's wcwidth,
|
||||||
|
* trimmed to what a terminal needs): combining marks are 0, East Asian
|
||||||
|
* Wide/Fullwidth (incl. CJK and emoji) are 2, everything else 1. CJK
|
||||||
|
* must be exactly 2 — the grid depends on it.
|
||||||
|
*/
|
||||||
|
|
||||||
|
interface CharRange {
|
||||||
|
lo: number;
|
||||||
|
hi: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Zero-width: common combining marks, format controls, variation selectors. */
|
||||||
|
const ZERO_WIDTH: CharRange[] = [
|
||||||
|
{ lo: 0x0300, hi: 0x036F },
|
||||||
|
{ lo: 0x0483, hi: 0x0489 },
|
||||||
|
{ lo: 0x0591, hi: 0x05BD },
|
||||||
|
{ lo: 0x05BF, hi: 0x05BF },
|
||||||
|
{ lo: 0x05C1, hi: 0x05C2 },
|
||||||
|
{ lo: 0x05C4, hi: 0x05C5 },
|
||||||
|
{ lo: 0x05C7, hi: 0x05C7 },
|
||||||
|
{ lo: 0x0610, hi: 0x061A },
|
||||||
|
{ lo: 0x064B, hi: 0x065F },
|
||||||
|
{ lo: 0x0670, hi: 0x0670 },
|
||||||
|
{ lo: 0x06D6, hi: 0x06DC },
|
||||||
|
{ lo: 0x06DF, hi: 0x06E4 },
|
||||||
|
{ lo: 0x06E7, hi: 0x06E8 },
|
||||||
|
{ lo: 0x06EA, hi: 0x06ED },
|
||||||
|
{ lo: 0x0711, hi: 0x0711 },
|
||||||
|
{ lo: 0x0730, hi: 0x074A },
|
||||||
|
{ lo: 0x07A6, hi: 0x07B0 },
|
||||||
|
{ lo: 0x07EB, hi: 0x07F3 },
|
||||||
|
{ lo: 0x0816, hi: 0x0819 },
|
||||||
|
{ lo: 0x081B, hi: 0x0823 },
|
||||||
|
{ lo: 0x0825, hi: 0x0827 },
|
||||||
|
{ lo: 0x0829, hi: 0x082D },
|
||||||
|
{ lo: 0x0859, hi: 0x085B },
|
||||||
|
{ lo: 0x0E31, hi: 0x0E31 },
|
||||||
|
{ lo: 0x0E34, hi: 0x0E3A },
|
||||||
|
{ lo: 0x0E47, hi: 0x0E4E },
|
||||||
|
{ lo: 0x200B, hi: 0x200F },
|
||||||
|
{ lo: 0x202A, hi: 0x202E },
|
||||||
|
{ lo: 0x2060, hi: 0x2064 },
|
||||||
|
{ lo: 0xFE00, hi: 0xFE0F },
|
||||||
|
{ lo: 0xFE20, hi: 0xFE2F },
|
||||||
|
{ lo: 0xFEFF, hi: 0xFEFF }
|
||||||
|
];
|
||||||
|
|
||||||
|
/** East Asian Wide + Fullwidth: CJK ideographs, kana, hangul, fullwidth
|
||||||
|
* forms, square emoji. */
|
||||||
|
const WIDE: CharRange[] = [
|
||||||
|
{ lo: 0x1100, hi: 0x115F },
|
||||||
|
{ lo: 0x2E80, hi: 0x303E },
|
||||||
|
{ lo: 0x3041, hi: 0x33FF },
|
||||||
|
{ lo: 0x3400, hi: 0x4DBF },
|
||||||
|
{ lo: 0x4E00, hi: 0x9FFF },
|
||||||
|
{ lo: 0xA000, hi: 0xA4CF },
|
||||||
|
{ lo: 0xA960, hi: 0xA97F },
|
||||||
|
{ lo: 0xAC00, hi: 0xD7A3 },
|
||||||
|
{ lo: 0xF900, hi: 0xFAFF },
|
||||||
|
{ lo: 0xFE10, hi: 0xFE19 },
|
||||||
|
{ lo: 0xFE30, hi: 0xFE6F },
|
||||||
|
{ lo: 0xFF00, hi: 0xFF60 },
|
||||||
|
{ lo: 0xFFE0, hi: 0xFFE6 },
|
||||||
|
{ lo: 0x1F300, hi: 0x1F64F },
|
||||||
|
{ lo: 0x1F680, hi: 0x1F6FF },
|
||||||
|
{ lo: 0x1F900, hi: 0x1F9FF },
|
||||||
|
{ lo: 0x1FA70, hi: 0x1FAFF },
|
||||||
|
{ lo: 0x20000, hi: 0x2FFFD },
|
||||||
|
{ lo: 0x30000, hi: 0x3FFFD }
|
||||||
|
];
|
||||||
|
|
||||||
|
function inRanges(cp: number, ranges: CharRange[]): boolean {
|
||||||
|
let lo: number = 0;
|
||||||
|
let hi: number = ranges.length - 1;
|
||||||
|
while (lo <= hi) {
|
||||||
|
const mid: number = (lo + hi) >> 1;
|
||||||
|
const r: CharRange = ranges[mid];
|
||||||
|
if (cp < r.lo) {
|
||||||
|
hi = mid - 1;
|
||||||
|
} else if (cp > r.hi) {
|
||||||
|
lo = mid + 1;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cell width of one codepoint: 0 (combining/format), 1, or 2 (wide).
|
||||||
|
* Returns -1 for C0/C1 control codepoints (never printed by the emulator).
|
||||||
|
*/
|
||||||
|
export function wcwidth(cp: number): number {
|
||||||
|
if (cp === 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (cp < 0x20 || (cp >= 0x7f && cp < 0xa0)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (inRanges(cp, ZERO_WIDTH)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (inRanges(cp, WIDE)) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user