forked from iann0036/cloud9-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminalManager.ts
176 lines (149 loc) · 6.58 KB
/
terminalManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import * as vscode from 'vscode';
import * as net from 'net';
export class TerminalManager {
private terminals;
private lastSocket;
private lastTid;
private lastCreatedTerminal;
private vfsid;
constructor(
private extensionPath: string,
private eventEmitter
) {
this.terminals = {};
eventEmitter.on('terminal_process_created', (pty) => {
console.log("TERMINAL PROCESS DATA");
console.log(pty);
console.log(this.lastSocket);
this.terminals[pty["id"]] = {
"terminal": this.lastCreatedTerminal,
"pid": parseInt(pty["pid"]),
"tid": this.lastTid,
"socket": this.lastSocket
};
this.eventEmitter.emit('send_ch4_message',
["resize",pty["pid"],159,33]
);
this.eventEmitter.emit('send_ch4_message',
["tmux","",{"capturePane":{"start":-32768,"end":1000,"pane":"cloud9_terminal_" + this.lastTid + ":0.0"},"encoding":"utf8","name":"xterm-color","command":""},{"$":pty["id"]}]
);
});
eventEmitter.on('ch4_data', (data, environmentId) => {
if (Array.isArray(data)) {
if (data.length>2) {
if (data[0] == "onEnd") {
if (Object.keys(this.terminals).map(Number).indexOf(data[1]) != -1) {
console.log("Terminating terminal");
this.closeTerminal(this.terminals[data[1]]);
delete this.terminals[data[1]];
}
} else if (data[0] == "onData") {
if (Object.keys(this.terminals).map(Number).indexOf(data[1]) != -1) {
console.log("Emitting terminal data");
this.emitTerminalData(this.terminals[data[1]], data[2]);
}
} else if (data[0] == 90) { // terminal creation channel
let contents = data[2];
console.log("Terminal Process Created");
eventEmitter.emit('terminal_process_created', contents["pty"]);
}
}
}
});
vscode.window.onDidCloseTerminal((closedTerminal) => {
closedTerminal.processId.then((closedTerminalPid) => {
for (var t in this.terminals) {
this.terminals[t]['terminal'].processId.then((pid) => {
if (pid == closedTerminalPid) {
delete this.terminals[t];
}
});
}
});
});
}
addTerminal(shared: boolean, vfsid: string): void {
if (shared) {
vscode.window.showInformationMessage("Shared terminal requires Visual Studio Code 1.26.0 or above.");
return;
}
let terminalPath = this.getTerminalPath();
this.vfsid = vfsid;
if (terminalPath == null) {
vscode.window.showWarningMessage("Unsupported platform for terminal. Upgrade Visual Studio Code to 1.26.0 or above for full compatibility.");
return;
}
const server = net.createServer((socket) => {
this.lastSocket = socket;
console.log("SOCKET OBTAINED");
socket.on('end', () => {
console.log('socket closed');
});
socket.on('data', (data) => {
let correctTerminalId = null;
for (var t in this.terminals) {
if (this.terminals[t]['socket'] === socket) {
correctTerminalId = t;
}
}
if (correctTerminalId == null) {
console.warn("Missing terminal socket");
return;
}
this.eventEmitter.emit('send_ch4_message',
["write", correctTerminalId.toString(), data.toString()]
); // TODO: Fix
});
this.lastTid = Math.floor(900*Math.random()) + 100;
this.eventEmitter.emit('send_ch4_message',
["tmux","",{"cwd":"/home/ec2-user/environment","cols":125,"rows":33,"name":"xterm-color","base":"/home/ec2-user/.c9","attach":false,"session":"cloud9_terminal_" + this.lastTid,"output":false,"terminal":true,"detachOthers":true,"defaultEditor":false,"encoding":"utf8","command":"bash -l"},{"$":90}]
);
console.log("init'd remote terminal");
}).on('error', (err) => {
console.error(err);
});
server.listen({
host: 'localhost',
port: 0, // ephemeral
exclusive: true
}, () => {
let addr = server.address();
let title = "Cloud9 Terminal";
console.log('opened server on', addr);
if (process.platform == "win32") {
this.lastCreatedTerminal = vscode.window.createTerminal(title, this.extensionPath + "/terminalApp/ansicon/" + process.arch + "/ansicon.exe", [ terminalPath, addr['address'] + ":" + addr['port'] ]);
} else {
this.lastCreatedTerminal = vscode.window.createTerminal(title, terminalPath, [ addr['address'] + ":" + addr['port'] ]);
}
this.lastCreatedTerminal.show();
});
}
getTerminalPath() {
let terminalPath = this.extensionPath + '/terminalApp/terminal-';
if (process.arch == "x64" && process.platform == "darwin") {
return terminalPath + "darwin-amd64";
/*} else if (process.arch == "x32" && process.platform == "win32") {
return terminalPath + "windows-386.exe";
} else if (process.arch == "x64" && process.platform == "win32") {
return terminalPath + "windows-amd64.exe";*/
} else if (process.arch == "x32" && process.platform == "linux") {
return terminalPath + "linux-386";
} else if (process.arch == "x64" && process.platform == "linux") {
return terminalPath + "linux-amd64";
}
return null;
}
closeTerminal(terminal): void {
terminal['socket'].destroy();
}
closeAll(): void {
Object.values(this.terminals).forEach(terminal => {
this.closeTerminal(terminal);
});
}
emitTerminalData(terminal, data): void {
if (typeof data == "string") {
terminal['socket'].write(data);
}
}
}