Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- import * as pty from "node-pty";
- import * as rpc from "vscode-jsonrpc";
- const is32ProcessOn64Windows = process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432');
- const powerShellPath = `${process.env.windir}\\${is32ProcessOn64Windows ? 'Sysnative' : 'System32'}\\WindowsPowerShell\\v1.0\\powershell.exe`;
- const cmdPath = `${process.env.windir}\\${is32ProcessOn64Windows ? 'Sysnative' : 'System32'}\\cmd.exe`;
- const bashPath = `${process.env.windir}\\${is32ProcessOn64Windows ? 'Sysnative' : 'System32'}\\bash.exe`;
- export class ServicePty {
- private connection: rpc.MessageConnection;
- private ptyConnection: pty.IPty;
- constructor(stream: NodeJS.ReadWriteStream) {
- this.connection = rpc.createMessageConnection(new rpc.StreamMessageReader(stream), new rpc.StreamMessageWriter(stream));
- this.connection.onRequest('initTerm', (shell, cols, rows, start, args) => {
- this.initTerm(shell, cols, rows, start, args);
- });
- }
- public initTerm(shell: string, cols: number, rows: number, start: string, args: string) {
- let shelltospawn: string;
- switch (shell) {
- case 'Powershell':
- shelltospawn = powerShellPath;
- break;
- case 'CMD':
- shelltospawn = cmdPath;
- break;
- case 'WSLBash':
- shelltospawn = bashPath;
- break;
- default:
- shelltospawn = shell;
- }
- this.ptyConnection = pty.spawn(shelltospawn, args, {
- name: 'vs-integrated-terminal',
- cols: cols,
- rows: rows,
- cwd: start,
- env: process.env,
- experimentalUseConpty: true,
- });
- this.ptyConnection.onData(data => this.connection.sendRequest('PtyData', data));
- this.ptyConnection.onExit(code => {
- this.connection.sendRequest('PtyExit', code);
- this.closeTerm();
- });
- }
- public closeTerm() {
- if (this.ptyConnection !== null) {
- this.ptyConnection.kill();
- this.ptyConnection = null;
- }
- }
- public resizeTerm(cols: number, rows: number) {
- if (this.ptyConnection !== null) {
- this.ptyConnection.resize(cols, rows);
- }
- }
- public termData(data: string) {
- if (this.ptyConnection !== null) {
- this.ptyConnection.write(data);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement