Archived
1
0
This repository has been archived on 2024-09-09. You can view files and clone it, but cannot push or open issues or pull requests.
code-server/packages/vscode/src/fill/node-pty.ts

65 lines
1.8 KiB
TypeScript
Raw Normal View History

import { client } from "@coder/ide/src/fill/client";
2019-01-08 01:46:19 +01:00
import { EventEmitter } from "events";
import * as nodePty from "node-pty";
import { ChildProcess } from "@coder/protocol/src/browser/command";
2019-01-08 01:46:19 +01:00
type nodePtyType = typeof nodePty;
/**
* Implementation of nodePty for the browser.
*/
class Pty implements nodePty.IPty {
private readonly emitter = new EventEmitter();
private readonly cp: ChildProcess;
2019-01-08 01:46:19 +01:00
public constructor(file: string, args: string[] | string, options: nodePty.IPtyForkOptions) {
this.cp = client.spawn(file, Array.isArray(args) ? args : [args], {
...options,
tty: {
columns: options.cols || 100,
rows: options.rows || 100,
2019-01-08 01:46:19 +01:00
},
});
this.on("write", (d) => this.cp.send(d));
this.on("kill", (exitCode) => this.cp.kill(exitCode));
this.on("resize", (cols, rows) => this.cp.resize!({ columns: cols, rows }));
this.cp.stdout.on("data", (data) => this.emitter.emit("data", data));
this.cp.stderr.on("data", (data) => this.emitter.emit("data", data));
this.cp.on("exit", (code) => this.emitter.emit("exit", code));
2019-01-08 01:46:19 +01:00
}
public get pid(): number {
return this.cp.pid!;
2019-01-08 01:46:19 +01:00
}
public get process(): string {
return this.cp.title!;
2019-01-08 01:46:19 +01:00
}
// tslint:disable-next-line no-any
public on(event: string, listener: (...args: any[]) => void): void {
2019-01-08 01:46:19 +01:00
this.emitter.on(event, listener);
}
public resize(columns: number, rows: number): void {
this.emitter.emit("resize", columns, rows);
}
public write(data: string): void {
this.emitter.emit("write", data);
}
public kill(signal?: string): void {
this.emitter.emit("kill", signal);
}
}
const ptyType: nodePtyType = {
spawn: (file: string, args: string[] | string, options: nodePty.IPtyForkOptions): nodePty.IPty => {
return new Pty(file, args, options);
},
};
module.exports = ptyType;