Skip to content

Commit f0c760f

Browse files
committed
Add PseudoTerminal class and global typings
Introduces the PseudoTerminal class for managing PTY instances and event handling using webuix. Adds global typings for WXEventHandler and event map, and updates dependencies to include webuix in package.json.
1 parent 1bb5073 commit f0c760f

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

ts/package-lock.json

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ts/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@
2121
},
2222
"peerDependencies": {
2323
"typescript": "^5.0.0"
24+
},
25+
"dependencies": {
26+
"webuix": "^0.0.18"
2427
}
2528
}

ts/src/classes/PseudoTerminal.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { WXEventHandler } from "webuix";
2+
import type { Pty, PtyInstance } from "../types";
3+
4+
export type EnvironmentVariables = {
5+
[key: string]: string | null;
6+
};
7+
8+
export class PseudoTerminal {
9+
private readonly _instance: PtyInstance | null = null;
10+
private readonly _shell: string;
11+
private readonly _args: string[];
12+
private readonly _env: EnvironmentVariables;
13+
private readonly _eventHandler: WXEventHandler | null = null;
14+
15+
constructor(shell: string, args: Array<string>, env: EnvironmentVariables) {
16+
this._shell = shell;
17+
this._args = args;
18+
this._env = env;
19+
20+
if (window._wxEventHandler instanceof WXEventHandler) {
21+
this._eventHandler = window._wxEventHandler;
22+
} else {
23+
this._eventHandler = new WXEventHandler();
24+
}
25+
26+
let impl: Pty | null = null;
27+
try {
28+
impl = global.require("pty") as Pty | null;
29+
} catch (error) {
30+
console.error("Failed to load pty:", error);
31+
return;
32+
}
33+
34+
if (!impl || typeof impl.start !== "function") {
35+
console.error("Invalid pty implementation");
36+
return;
37+
}
38+
39+
const _args = JSON.stringify(this._args);
40+
const _env = JSON.stringify(this._env);
41+
42+
this._instance = impl.start(this._shell, _args, _env);
43+
}
44+
45+
public on(name: "data", callback: (data: string) => void): void;
46+
public on(
47+
name: "exit",
48+
callback: (data: { code: number; signal?: string }) => void
49+
): void;
50+
public on(name: "data" | "exit", callback: (data: any) => void): void {
51+
const eventMap: Record<"data" | "exit", string> = {
52+
data: "pty-data",
53+
exit: "pty-exit",
54+
};
55+
56+
const eventType: any = eventMap[name];
57+
if (!eventType) {
58+
console.warn(`Unsupported event name: ${name}`);
59+
return;
60+
}
61+
62+
if (!this._eventHandler) {
63+
throw new Error("WXEventHandler is not initialized");
64+
}
65+
66+
this._eventHandler.on(window, eventType, (event: any) => {
67+
if (name === "data") {
68+
callback(event?.wx as string);
69+
} else if (name === "exit") {
70+
callback(event?.wx as { code: number; signal?: string });
71+
}
72+
});
73+
}
74+
75+
public write(data: string): void {
76+
this._instance?.write(data);
77+
}
78+
79+
public resize(cols: number, rows: number): void {
80+
this._instance?.resize(cols, rows);
81+
}
82+
83+
public kill(): void {
84+
this._instance?.kill();
85+
}
86+
}

ts/src/global.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { WXEventHandler } from "webuix";
2+
3+
export {};
4+
5+
declare global {
6+
var _wxEventHandler: WXEventHandler | undefined | null;
7+
8+
interface Window {
9+
_wxEventHandler?: WXEventHandler | undefined | null;
10+
}
11+
}
12+
13+
declare module "webuix" {
14+
interface WXEventMap {
15+
["pty-data"]: string;
16+
["pty-exit"]: number;
17+
}
18+
}

0 commit comments

Comments
 (0)