This repository was archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathshell.js
More file actions
197 lines (181 loc) · 5.84 KB
/
shell.js
File metadata and controls
197 lines (181 loc) · 5.84 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*******************************************************************************
* @license
* Copyright (c) 2016, 2017 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors: IBM Corporation - initial API and implementation
******************************************************************************/
/*eslint-env browser, amd*/
define([
"socket.io/socket.io",
"requirejs/domReady",
'orion/widgets/input/DropDownMenu',
'orion/widgets/input/SettingsSelect',
'orion/commands',
"orion/PageUtil",
"xterm/xterm",
'tty/shellIntegrationCmd'
], function(io, onReady, DropDownMenu, SettingsSelect, mCommands, PageUtil, Terminal, mShellIntegrationCmd) {
var term, serviceRegistry;
var colorScheme = "Dark";
onReady(function() {
var socket, charWidth, charHeight, rows = 24, cols = 80;
var fitToDiv = function(term) {
if (socket === null) return;
var termContainer = document.getElementById("terminalBox"),
newWidth = termContainer.clientWidth,
newHeight = termContainer.clientHeight;
if (charWidth === undefined) {
var span = document.createElement("span");
span.textContent = "X";
termContainer.appendChild(span);
var rect = span.getBoundingClientRect();
charWidth = rect.right - rect.left;
charHeight = rect.bottom - rect.top + 2;
termContainer.removeChild(span);
}
var newRows = (newHeight - 10) / (charHeight || 12);
var newCols = Math.max(80, (newWidth - 10) / (charWidth || 12));
if (newRows === rows && newCols !== cols) return;
rows = newRows;
cols = newCols;
term.resize(Math.floor(newCols), Math.floor(newRows));
socket.emit('resize', Math.floor(newCols), Math.floor(newRows));
};
var socketioPath = location.pathname.substr(0, location.pathname.indexOf('tty')) + 'socket.io/';
socket = io.connect('/tty', { path: socketioPath });
socket.on('connect', function() {
socket.emit('start', getCWD());
});
socket.on('fail', function(error) {
console.log(error);
});
socket.on('ready', function() {
term = new Terminal({
cols: cols,
rows: rows,
cursorBlink: true
});
term.on('data', function(data) {
socket.emit('data', data);
});
term.on('title', function(title) {
document.title = title;
});
var termContainer = document.getElementById("terminalBox");
term.open(termContainer);
fitToDiv(term);
var timeout;
window.addEventListener("resize", function() {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(function() {
fitToDiv(term);
timeout = null;
}, 500);
});
socket.on('data', function(data) {
term.write(data);
var apc = parseAPC(data);
if (apc) {
mShellIntegrationCmd.execute(socket, apc.split('\r\n'));
}
});
socket.on('disconnect', function() {
term.destroy();
});
changeScheme(colorScheme);
});
});
function getCWD() {
var result = PageUtil.matchResourceParameters(window.location.href).resource;
return result.length > 0 ? result : null;
}
var pendingAPC = "";
function parseAPC(data) {
if (pendingAPC) {
var end = data.indexOf(String.fromCharCode(27));
if (end > -1) {
var bakPendingAPC = pendingAPC;
pendingAPC = "";
return bakPendingAPC + data.substr(0, end)
} else {
pendingAPC += data;
}
} else {
var apcStart = data.indexOf(String.fromCharCode(27) + '_');
if (apcStart > -1) {
var end = data.indexOf(String.fromCharCode(27), apcStart + 2);
if (end > -1) {
return data.substr(apcStart + 2, end - apcStart - 2);
} else {
pendingAPC += data.substr(apcStart + 2);
}
}
}
return null;
}
function changeScheme(schemeName) {
var t;
if (term !== null) {
t = document.querySelector('.terminal');
t.setAttribute('scheme', schemeName);
}
if (serviceRegistry) {
serviceRegistry.getService("orion.core.preference").put("/orion/console", {"colorScheme": schemeName});
}
}
function createCommands(registry, commandRegistry) {
serviceRegistry = registry;
serviceRegistry.getService("orion.core.preference").get("/orion/console").then(function(prefs) {
colorScheme = prefs.colorScheme || colorScheme;
changeScheme(colorScheme);
});
var that = this;
var settingsCommand = new mCommands.Command({
imageClass: "core-sprite-wrench", //$NON-NLS-0$
tooltip: "Settings",
id: "orion.term.settings", //$NON-NLS-0$
visibleWhen: /** @callback */ function(items, data) {
return true;
},
callback: function(data) {
var dropDown = settingsCommand.settingsDropDown;
if (!dropDown || dropDown.isDestroyed()) {
dropDown = settingsCommand.settingsDropDown = new DropDownMenu(data.domNode.parentNode, data.domNode, {
noClick: true,
selectionClass: 'dropdownSelection', //$NON-NLS-0$
onShow: function() {
dropDown.focus();
},
onHide: function() {
if (that.editor) {
that.editor.focus();
}
}
});
var menu = dropDown.getContentNode();
var select = new SettingsSelect({
options: ["Dark", "Light", "Solarized"].map(function(l) {
return {label: l, value: l, selected: colorScheme === l};
}),
fieldlabel: "Color Scheme:",
local: true
});
menu.appendChild(select.node);
select.postChange = changeScheme;
select.show();
menu.tabIndex = menu.style.marginTop = 0;
}
dropDown.click();
}
});
commandRegistry.addCommand(settingsCommand);
commandRegistry.registerCommandContribution("settingsActions", "orion.term.settings", 1, null, false, null, null, this); //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-0$
}
return {
createCommands: createCommands
};
});