forked from jupyterlab/jupyterlab
/
widget.ts
393 lines (350 loc) · 8.72 KB
/
widget.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { TerminalSession } from '@jupyterlab/services';
import { Message, MessageLoop } from '@phosphor/messaging';
import { Widget } from '@phosphor/widgets';
import { Terminal as Xterm, ITerminalOptions as IXtermOptions } from 'xterm';
import { fit } from 'xterm/lib/addons/fit';
/**
* The class name added to a terminal widget.
*/
const TERMINAL_CLASS = 'jp-Terminal';
/**
* The class name added to a terminal body.
*/
const TERMINAL_BODY_CLASS = 'jp-Terminal-body';
/**
* A widget which manages a terminal session.
*/
export class Terminal extends Widget {
/**
* Construct a new terminal widget.
*
* @param options - The terminal configuration options.
*/
constructor(options: Partial<Terminal.IOptions> = {}) {
super();
this.addClass(TERMINAL_CLASS);
// Create the xterm.
this._term = new Xterm(Private.getConfig(options));
this._initializeTerm();
// Initialize settings.
let defaults = Terminal.defaultOptions;
this._initialCommand = options.initialCommand || defaults.initialCommand;
this.theme = options.theme || defaults.theme;
this.id = `jp-Terminal-${Private.id++}`;
this.title.label = 'Terminal';
}
/**
* The terminal session associated with the widget.
*/
get session(): TerminalSession.ISession | null {
return this._session;
}
set session(value: TerminalSession.ISession | null) {
if (this._session && !this._session.isDisposed) {
this._session.messageReceived.disconnect(this._onMessage, this);
}
this._session = value || null;
if (!value) {
return;
}
value.ready.then(() => {
if (this.isDisposed || value !== this._session) {
return;
}
value.messageReceived.connect(this._onMessage, this);
this.title.label = `Terminal ${value.name}`;
this._setSessionSize();
if (this._initialCommand) {
this._session.send({
type: 'stdin',
content: [this._initialCommand + '\r']
});
}
});
}
/**
* Get the font size of the terminal in pixels.
*/
get fontSize(): number {
return this._term.getOption('fontSize');
}
/**
* Set the font size of the terminal in pixels.
*/
set fontSize(size: number) {
if (this.fontSize === size) {
return;
}
this._term.setOption('fontSize', size);
this._needsResize = true;
this.update();
}
/**
* Get the current theme, either light or dark.
*/
get theme(): Terminal.Theme {
return this._theme;
}
/**
* Set the current theme, either light or dark.
*/
set theme(value: Terminal.Theme) {
this._theme = value;
if (value === 'light') {
this.addClass('jp-mod-light');
this._term.setOption('theme', Private.lightTheme);
} else {
this.removeClass('jp-mod-light');
this._term.setOption('theme', Private.darkTheme);
}
this.update();
}
/**
* Dispose of the resources held by the terminal widget.
*/
dispose(): void {
this._session = null;
super.dispose();
}
/**
* Refresh the terminal session.
*/
refresh(): Promise<void> {
if (!this._session) {
return Promise.reject(void 0);
}
return this._session.reconnect().then(() => {
this._term.clear();
});
}
/**
* Process a message sent to the widget.
*
* @param msg - The message sent to the widget.
*
* #### Notes
* Subclasses may reimplement this method as needed.
*/
processMessage(msg: Message): void {
super.processMessage(msg);
switch (msg.type) {
case 'fit-request':
this.onFitRequest(msg);
break;
default:
break;
}
}
/**
* Set the size of the terminal when attached if dirty.
*/
protected onAfterAttach(msg: Message): void {
this.update();
}
/**
* Set the size of the terminal when shown if dirty.
*/
protected onAfterShow(msg: Message): void {
this.update();
}
/**
* On resize, use the computed row and column sizes to resize the terminal.
*/
protected onResize(msg: Widget.ResizeMessage): void {
this._offsetWidth = msg.width;
this._offsetHeight = msg.height;
this._needsResize = true;
this.update();
}
/**
* A message handler invoked on an `'update-request'` message.
*/
protected onUpdateRequest(msg: Message): void {
if (!this.isVisible || !this.isAttached) {
return;
}
// Open the terminal if necessary.
if (!this._termOpened) {
this._term.open(this.node);
this._term.element.classList.add(TERMINAL_BODY_CLASS);
this._termOpened = true;
}
if (this._needsResize) {
this._resizeTerminal();
}
}
/**
* A message handler invoked on an `'fit-request'` message.
*/
protected onFitRequest(msg: Message): void {
let resize = Widget.ResizeMessage.UnknownSize;
MessageLoop.sendMessage(this, resize);
}
/**
* Handle `'activate-request'` messages.
*/
protected onActivateRequest(msg: Message): void {
this._term.focus();
}
/**
* Initialize the terminal object.
*/
private _initializeTerm(): void {
this._term.on('data', (data: string) => {
if (this._session) {
this._session.send({
type: 'stdin',
content: [data]
});
}
});
this._term.on('title', (title: string) => {
this.title.label = title;
});
}
/**
* Handle a message from the terminal session.
*/
private _onMessage(
sender: TerminalSession.ISession,
msg: TerminalSession.IMessage
): void {
switch (msg.type) {
case 'stdout':
if (msg.content) {
this._term.write(msg.content[0] as string);
}
break;
case 'disconnect':
this._term.write('\r\n\r\n[Finished... Term Session]\r\n');
break;
default:
break;
}
}
/**
* Resize the terminal based on computed geometry.
*/
private _resizeTerminal() {
fit(this._term);
if (this._offsetWidth === -1) {
this._offsetWidth = this.node.offsetWidth;
}
if (this._offsetHeight === -1) {
this._offsetHeight = this.node.offsetHeight;
}
this._setSessionSize();
this._needsResize = false;
}
/**
* Set the size of the terminal in the session.
*/
private _setSessionSize(): void {
let content = [
this._term.rows,
this._term.cols,
this._offsetHeight,
this._offsetWidth
];
if (this._session) {
this._session.send({ type: 'set_size', content });
}
}
private _term: Xterm;
private _needsResize = true;
private _theme: Terminal.Theme = 'dark';
private _session: TerminalSession.ISession | null = null;
private _initialCommand: string;
private _termOpened = false;
private _offsetWidth = -1;
private _offsetHeight = -1;
}
/**
* The namespace for `Terminal` class statics.
*/
export namespace Terminal {
/**
* Options for the terminal widget.
*/
export interface IOptions {
/**
* The font size of the terminal in pixels.
*/
fontSize: number;
/**
* The theme of the terminal.
*/
theme: Theme;
/**
* Whether to blink the cursor. Can only be set at startup.
*/
cursorBlink: boolean;
/**
* An optional command to run when the session starts.
*/
initialCommand: string;
}
/**
* The default options used for creating terminals.
*/
export const defaultOptions: IOptions = {
theme: 'dark',
fontSize: 13,
cursorBlink: true,
initialCommand: ''
};
/**
* A type for the terminal theme.
*/
export type Theme = 'light' | 'dark';
}
/**
* A namespace for private data.
*/
namespace Private {
/**
* Get term.js options from ITerminalOptions.
*/
export function getConfig(
options: Partial<Terminal.IOptions>
): IXtermOptions {
let config: IXtermOptions = {};
if (options.cursorBlink !== void 0) {
config.cursorBlink = options.cursorBlink;
} else {
config.cursorBlink = Terminal.defaultOptions.cursorBlink;
}
if (options.fontSize !== void 0) {
config.fontSize = options.fontSize;
} else {
config.fontSize = Terminal.defaultOptions.fontSize;
}
return config;
}
/**
* An incrementing counter for ids.
*/
export let id = 0;
/**
* The light terminal theme.
*/
export const lightTheme = {
foreground: '#000',
background: '#fff',
cursor: '#616161', // md-grey-700
cursorAccent: '#F5F5F5', // md-grey-100
selection: 'rgba(97, 97, 97, 0.3)' // md-grey-700
};
/**
* The dark terminal theme.
*/
export const darkTheme = {
foreground: '#fff',
background: '#000',
cursor: '#fff',
cursorAccent: '#000',
selection: 'rgba(255, 255, 255, 0.3)'
};
}