Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. 'use strict';
  2.  
  3. import * as pty from "node-pty";
  4. import * as rpc from "vscode-jsonrpc";
  5.  
  6. const is32ProcessOn64Windows = process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432');
  7.  
  8. const powerShellPath = `${process.env.windir}\\${is32ProcessOn64Windows ? 'Sysnative' : 'System32'}\\WindowsPowerShell\\v1.0\\powershell.exe`;
  9. const cmdPath = `${process.env.windir}\\${is32ProcessOn64Windows ? 'Sysnative' : 'System32'}\\cmd.exe`;
  10. const bashPath = `${process.env.windir}\\${is32ProcessOn64Windows ? 'Sysnative' : 'System32'}\\bash.exe`;
  11.  
  12. export class ServicePty {
  13. private connection: rpc.MessageConnection;
  14. private ptyConnection: pty.IPty;
  15.  
  16. constructor(stream: NodeJS.ReadWriteStream) {
  17. this.connection = rpc.createMessageConnection(new rpc.StreamMessageReader(stream), new rpc.StreamMessageWriter(stream));
  18.  
  19. this.connection.onRequest('initTerm', (shell, cols, rows, start, args) => {
  20. this.initTerm(shell, cols, rows, start, args);
  21. });
  22. }
  23.  
  24. public initTerm(shell: string, cols: number, rows: number, start: string, args: string) {
  25. let shelltospawn: string;
  26. switch (shell) {
  27. case 'Powershell':
  28. shelltospawn = powerShellPath;
  29. break;
  30. case 'CMD':
  31. shelltospawn = cmdPath;
  32. break;
  33. case 'WSLBash':
  34. shelltospawn = bashPath;
  35. break;
  36. default:
  37. shelltospawn = shell;
  38. }
  39.  
  40. this.ptyConnection = pty.spawn(shelltospawn, args, {
  41. name: 'vs-integrated-terminal',
  42. cols: cols,
  43. rows: rows,
  44. cwd: start,
  45. env: process.env,
  46. experimentalUseConpty: true,
  47. });
  48.  
  49. this.ptyConnection.onData(data => this.connection.sendRequest('PtyData', data));
  50. this.ptyConnection.onExit(code => {
  51. this.connection.sendRequest('PtyExit', code);
  52. this.closeTerm();
  53. });
  54. }
  55.  
  56. public closeTerm() {
  57. if (this.ptyConnection !== null) {
  58. this.ptyConnection.kill();
  59. this.ptyConnection = null;
  60. }
  61. }
  62.  
  63. public resizeTerm(cols: number, rows: number) {
  64. if (this.ptyConnection !== null) {
  65. this.ptyConnection.resize(cols, rows);
  66. }
  67. }
  68.  
  69. public termData(data: string) {
  70. if (this.ptyConnection !== null) {
  71. this.ptyConnection.write(data);
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement