@jupyterlab/terminal-extension 4.0.0-alpha.19 → 4.0.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +16 -15
  2. package/src/index.ts +580 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlab/terminal-extension",
3
- "version": "4.0.0-alpha.19",
3
+ "version": "4.0.0-alpha.20",
4
4
  "description": "JupyterLab - Terminal Emulator Extension",
5
5
  "homepage": "https://github.com/jupyterlab/jupyterlab",
6
6
  "bugs": {
@@ -28,7 +28,8 @@
28
28
  "lib/*.js",
29
29
  "schema/*.json",
30
30
  "style/**/*.css",
31
- "style/index.js"
31
+ "style/index.js",
32
+ "src/**/*.{ts,tsx}"
32
33
  ],
33
34
  "scripts": {
34
35
  "build": "tsc -b",
@@ -37,23 +38,23 @@
37
38
  "watch": "tsc -b --watch"
38
39
  },
39
40
  "dependencies": {
40
- "@jupyterlab/application": "^4.0.0-alpha.19",
41
- "@jupyterlab/apputils": "^4.0.0-alpha.19",
42
- "@jupyterlab/launcher": "^4.0.0-alpha.19",
43
- "@jupyterlab/mainmenu": "^4.0.0-alpha.19",
44
- "@jupyterlab/running": "^4.0.0-alpha.19",
45
- "@jupyterlab/services": "^7.0.0-alpha.19",
46
- "@jupyterlab/settingregistry": "^4.0.0-alpha.19",
47
- "@jupyterlab/terminal": "^4.0.0-alpha.19",
48
- "@jupyterlab/translation": "^4.0.0-alpha.19",
49
- "@jupyterlab/ui-components": "^4.0.0-alpha.34",
50
- "@lumino/widgets": "^2.0.0-beta.1"
41
+ "@jupyterlab/application": "^4.0.0-alpha.20",
42
+ "@jupyterlab/apputils": "^4.0.0-alpha.20",
43
+ "@jupyterlab/launcher": "^4.0.0-alpha.20",
44
+ "@jupyterlab/mainmenu": "^4.0.0-alpha.20",
45
+ "@jupyterlab/running": "^4.0.0-alpha.20",
46
+ "@jupyterlab/services": "^7.0.0-alpha.20",
47
+ "@jupyterlab/settingregistry": "^4.0.0-alpha.20",
48
+ "@jupyterlab/terminal": "^4.0.0-alpha.20",
49
+ "@jupyterlab/translation": "^4.0.0-alpha.20",
50
+ "@jupyterlab/ui-components": "^4.0.0-alpha.35",
51
+ "@lumino/widgets": "^2.0.0-rc.0"
51
52
  },
52
53
  "devDependencies": {
53
54
  "@types/webpack-env": "^1.14.1",
54
55
  "rimraf": "~3.0.0",
55
- "typedoc": "~0.22.10",
56
- "typescript": "~4.7.3"
56
+ "typedoc": "~0.23.25",
57
+ "typescript": "~5.0.0-beta"
57
58
  },
58
59
  "publishConfig": {
59
60
  "access": "public"
package/src/index.ts ADDED
@@ -0,0 +1,580 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+ /**
4
+ * @packageDocumentation
5
+ * @module terminal-extension
6
+ */
7
+
8
+ import {
9
+ ILayoutRestorer,
10
+ JupyterFrontEnd,
11
+ JupyterFrontEndPlugin
12
+ } from '@jupyterlab/application';
13
+ import {
14
+ Clipboard,
15
+ ICommandPalette,
16
+ IThemeManager,
17
+ MainAreaWidget,
18
+ WidgetTracker
19
+ } from '@jupyterlab/apputils';
20
+ import { ILauncher } from '@jupyterlab/launcher';
21
+ import { IMainMenu } from '@jupyterlab/mainmenu';
22
+ import { IRunningSessionManagers, IRunningSessions } from '@jupyterlab/running';
23
+ import { Terminal, TerminalAPI } from '@jupyterlab/services';
24
+ import { ISettingRegistry } from '@jupyterlab/settingregistry';
25
+ import {
26
+ ITerminal,
27
+ ITerminalTracker,
28
+ Terminal as XTerm
29
+ } from '@jupyterlab/terminal';
30
+ import { ITranslator } from '@jupyterlab/translation';
31
+ import {
32
+ copyIcon,
33
+ pasteIcon,
34
+ refreshIcon,
35
+ terminalIcon
36
+ } from '@jupyterlab/ui-components';
37
+ import { Menu, Widget } from '@lumino/widgets';
38
+
39
+ /**
40
+ * The command IDs used by the terminal plugin.
41
+ */
42
+ namespace CommandIDs {
43
+ export const copy = 'terminal:copy';
44
+
45
+ export const createNew = 'terminal:create-new';
46
+
47
+ export const open = 'terminal:open';
48
+
49
+ export const refresh = 'terminal:refresh';
50
+
51
+ export const increaseFont = 'terminal:increase-font';
52
+
53
+ export const decreaseFont = 'terminal:decrease-font';
54
+
55
+ export const paste = 'terminal:paste';
56
+
57
+ export const setTheme = 'terminal:set-theme';
58
+
59
+ export const shutdown = 'terminal:shut-down';
60
+ }
61
+
62
+ /**
63
+ * The default terminal extension.
64
+ */
65
+ const plugin: JupyterFrontEndPlugin<ITerminalTracker> = {
66
+ activate,
67
+ id: '@jupyterlab/terminal-extension:plugin',
68
+ provides: ITerminalTracker,
69
+ requires: [ISettingRegistry, ITranslator],
70
+ optional: [
71
+ ICommandPalette,
72
+ ILauncher,
73
+ ILayoutRestorer,
74
+ IMainMenu,
75
+ IThemeManager,
76
+ IRunningSessionManagers
77
+ ],
78
+ autoStart: true
79
+ };
80
+
81
+ /**
82
+ * Export the plugin as default.
83
+ */
84
+ export default plugin;
85
+
86
+ /**
87
+ * Activate the terminal plugin.
88
+ */
89
+ function activate(
90
+ app: JupyterFrontEnd,
91
+ settingRegistry: ISettingRegistry,
92
+ translator: ITranslator,
93
+ palette: ICommandPalette | null,
94
+ launcher: ILauncher | null,
95
+ restorer: ILayoutRestorer | null,
96
+ mainMenu: IMainMenu | null,
97
+ themeManager: IThemeManager | null,
98
+ runningSessionManagers: IRunningSessionManagers | null
99
+ ): ITerminalTracker {
100
+ const trans = translator.load('jupyterlab');
101
+ const { serviceManager, commands } = app;
102
+ const category = trans.__('Terminal');
103
+ const namespace = 'terminal';
104
+ const tracker = new WidgetTracker<MainAreaWidget<ITerminal.ITerminal>>({
105
+ namespace
106
+ });
107
+
108
+ // Bail if there are no terminals available.
109
+ if (!serviceManager.terminals.isAvailable()) {
110
+ console.warn(
111
+ 'Disabling terminals plugin because they are not available on the server'
112
+ );
113
+ return tracker;
114
+ }
115
+
116
+ // Handle state restoration.
117
+ if (restorer) {
118
+ void restorer.restore(tracker, {
119
+ command: CommandIDs.createNew,
120
+ args: widget => ({ name: widget.content.session.name }),
121
+ name: widget => widget.content.session.name
122
+ });
123
+ }
124
+
125
+ // The cached terminal options from the setting editor.
126
+ const options: Partial<ITerminal.IOptions> = {};
127
+
128
+ /**
129
+ * Update the cached option values.
130
+ */
131
+ function updateOptions(settings: ISettingRegistry.ISettings): void {
132
+ // Update the cached options by doing a shallow copy of key/values.
133
+ // This is needed because options is passed and used in addcommand-palette and needs
134
+ // to reflect the current cached values.
135
+ Object.keys(settings.composite).forEach((key: keyof ITerminal.IOptions) => {
136
+ (options as any)[key] = settings.composite[key];
137
+ });
138
+ }
139
+
140
+ /**
141
+ * Update terminal
142
+ */
143
+ function updateTerminal(widget: MainAreaWidget<ITerminal.ITerminal>): void {
144
+ const terminal = widget.content;
145
+ if (!terminal) {
146
+ return;
147
+ }
148
+ Object.keys(options).forEach((key: keyof ITerminal.IOptions) => {
149
+ terminal.setOption(key, options[key]);
150
+ });
151
+ }
152
+
153
+ /**
154
+ * Update the settings of the current tracker instances.
155
+ */
156
+ function updateTracker(): void {
157
+ tracker.forEach(widget => updateTerminal(widget));
158
+ }
159
+
160
+ // Fetch the initial state of the settings.
161
+ settingRegistry
162
+ .load(plugin.id)
163
+ .then(settings => {
164
+ updateOptions(settings);
165
+ updateTracker();
166
+ settings.changed.connect(() => {
167
+ updateOptions(settings);
168
+ updateTracker();
169
+ });
170
+ })
171
+ .catch(Private.showErrorMessage);
172
+
173
+ // Subscribe to changes in theme. This is needed as the theme
174
+ // is computed dynamically based on the string value and DOM
175
+ // properties.
176
+ themeManager?.themeChanged.connect((sender, args) => {
177
+ tracker.forEach(widget => {
178
+ const terminal = widget.content;
179
+ if (terminal.getOption('theme') === 'inherit') {
180
+ terminal.setOption('theme', 'inherit');
181
+ }
182
+ });
183
+ });
184
+
185
+ addCommands(app, tracker, settingRegistry, translator, options);
186
+
187
+ if (mainMenu) {
188
+ // Add "Terminal Theme" menu below "Theme" menu.
189
+ const themeMenu = new Menu({ commands });
190
+ themeMenu.title.label = trans._p('menu', 'Terminal Theme');
191
+ themeMenu.addItem({
192
+ command: CommandIDs.setTheme,
193
+ args: {
194
+ theme: 'inherit',
195
+ displayName: trans.__('Inherit'),
196
+ isPalette: false
197
+ }
198
+ });
199
+ themeMenu.addItem({
200
+ command: CommandIDs.setTheme,
201
+ args: {
202
+ theme: 'light',
203
+ displayName: trans.__('Light'),
204
+ isPalette: false
205
+ }
206
+ });
207
+ themeMenu.addItem({
208
+ command: CommandIDs.setTheme,
209
+ args: { theme: 'dark', displayName: trans.__('Dark'), isPalette: false }
210
+ });
211
+
212
+ // Add some commands to the "View" menu.
213
+ mainMenu.settingsMenu.addGroup(
214
+ [
215
+ { command: CommandIDs.increaseFont },
216
+ { command: CommandIDs.decreaseFont },
217
+ { type: 'submenu', submenu: themeMenu }
218
+ ],
219
+ 40
220
+ );
221
+
222
+ // Add terminal creation to the file menu.
223
+ mainMenu.fileMenu.newMenu.addItem({
224
+ command: CommandIDs.createNew,
225
+ rank: 20
226
+ });
227
+
228
+ // Add terminal close-and-shutdown to the file menu.
229
+ mainMenu.fileMenu.closeAndCleaners.add({
230
+ id: CommandIDs.shutdown,
231
+ isEnabled: (w: Widget) => tracker.currentWidget !== null && tracker.has(w)
232
+ });
233
+ }
234
+
235
+ if (palette) {
236
+ // Add command palette items.
237
+ [
238
+ CommandIDs.createNew,
239
+ CommandIDs.refresh,
240
+ CommandIDs.increaseFont,
241
+ CommandIDs.decreaseFont
242
+ ].forEach(command => {
243
+ palette.addItem({ command, category, args: { isPalette: true } });
244
+ });
245
+ palette.addItem({
246
+ command: CommandIDs.setTheme,
247
+ category,
248
+ args: {
249
+ theme: 'inherit',
250
+ displayName: trans.__('Inherit'),
251
+ isPalette: true
252
+ }
253
+ });
254
+ palette.addItem({
255
+ command: CommandIDs.setTheme,
256
+ category,
257
+ args: { theme: 'light', displayName: trans.__('Light'), isPalette: true }
258
+ });
259
+ palette.addItem({
260
+ command: CommandIDs.setTheme,
261
+ category,
262
+ args: { theme: 'dark', displayName: trans.__('Dark'), isPalette: true }
263
+ });
264
+ }
265
+
266
+ // Add a launcher item if the launcher is available.
267
+ if (launcher) {
268
+ launcher.add({
269
+ command: CommandIDs.createNew,
270
+ category: trans.__('Other'),
271
+ rank: 0
272
+ });
273
+ }
274
+
275
+ // Add a sessions manager if the running extension is available
276
+ if (runningSessionManagers) {
277
+ addRunningSessionManager(runningSessionManagers, app, translator);
278
+ }
279
+
280
+ return tracker;
281
+ }
282
+
283
+ /**
284
+ * Add the running terminal manager to the running panel.
285
+ */
286
+ function addRunningSessionManager(
287
+ managers: IRunningSessionManagers,
288
+ app: JupyterFrontEnd,
289
+ translator: ITranslator
290
+ ) {
291
+ const trans = translator.load('jupyterlab');
292
+ const manager = app.serviceManager.terminals;
293
+
294
+ class RunningTerminal implements IRunningSessions.IRunningItem {
295
+ constructor(model: Terminal.IModel) {
296
+ this._model = model;
297
+ }
298
+ open() {
299
+ void app.commands.execute('terminal:open', { name: this._model.name });
300
+ }
301
+ icon() {
302
+ return terminalIcon;
303
+ }
304
+ label() {
305
+ return `terminals/${this._model.name}`;
306
+ }
307
+ shutdown() {
308
+ return manager.shutdown(this._model.name);
309
+ }
310
+
311
+ private _model: Terminal.IModel;
312
+ }
313
+
314
+ managers.add({
315
+ name: trans.__('Terminals'),
316
+ running: () =>
317
+ Array.from(manager.running()).map(model => new RunningTerminal(model)),
318
+ shutdownAll: () => manager.shutdownAll(),
319
+ refreshRunning: () => manager.refreshRunning(),
320
+ runningChanged: manager.runningChanged,
321
+ shutdownLabel: trans.__('Shut Down'),
322
+ shutdownAllLabel: trans.__('Shut Down All'),
323
+ shutdownAllConfirmationText: trans.__(
324
+ 'Are you sure you want to permanently shut down all running terminals?'
325
+ )
326
+ });
327
+ }
328
+
329
+ /**
330
+ * Add the commands for the terminal.
331
+ */
332
+ export function addCommands(
333
+ app: JupyterFrontEnd,
334
+ tracker: WidgetTracker<MainAreaWidget<ITerminal.ITerminal>>,
335
+ settingRegistry: ISettingRegistry,
336
+ translator: ITranslator,
337
+ options: Partial<ITerminal.IOptions>
338
+ ): void {
339
+ const trans = translator.load('jupyterlab');
340
+ const { commands, serviceManager } = app;
341
+
342
+ const isEnabled = () =>
343
+ tracker.currentWidget !== null &&
344
+ tracker.currentWidget === app.shell.currentWidget;
345
+
346
+ // Add terminal commands.
347
+ commands.addCommand(CommandIDs.createNew, {
348
+ label: args =>
349
+ args['isPalette'] ? trans.__('New Terminal') : trans.__('Terminal'),
350
+ caption: trans.__('Start a new terminal session'),
351
+ icon: args => (args['isPalette'] ? undefined : terminalIcon),
352
+ execute: async args => {
353
+ const name = args['name'] as string;
354
+ const cwd = args['cwd'] as string;
355
+
356
+ let session;
357
+ if (name) {
358
+ const models = await TerminalAPI.listRunning();
359
+ if (models.map(d => d.name).includes(name)) {
360
+ // we are restoring a terminal widget and the corresponding terminal exists
361
+ // let's connect to it
362
+ session = serviceManager.terminals.connectTo({ model: { name } });
363
+ } else {
364
+ // we are restoring a terminal widget but the corresponding terminal was closed
365
+ // let's start a new terminal with the original name
366
+ session = await serviceManager.terminals.startNew({ name, cwd });
367
+ }
368
+ } else {
369
+ // we are creating a new terminal widget with a new terminal
370
+ // let the server choose the terminal name
371
+ session = await serviceManager.terminals.startNew({ cwd });
372
+ }
373
+
374
+ const term = new XTerm(session, options, translator);
375
+
376
+ term.title.icon = terminalIcon;
377
+ term.title.label = '...';
378
+
379
+ const main = new MainAreaWidget({ content: term, reveal: term.ready });
380
+ app.shell.add(main, 'main', { type: 'Terminal' });
381
+ void tracker.add(main);
382
+ app.shell.activateById(main.id);
383
+ return main;
384
+ }
385
+ });
386
+
387
+ commands.addCommand(CommandIDs.open, {
388
+ label: trans.__('Open a terminal by its `name`.'),
389
+ execute: args => {
390
+ const name = args['name'] as string;
391
+ // Check for a running terminal with the given name.
392
+ const widget = tracker.find(value => {
393
+ const content = value.content;
394
+ return content.session.name === name || false;
395
+ });
396
+ if (widget) {
397
+ app.shell.activateById(widget.id);
398
+ } else {
399
+ // Otherwise, create a new terminal with a given name.
400
+ return commands.execute(CommandIDs.createNew, { name });
401
+ }
402
+ }
403
+ });
404
+
405
+ commands.addCommand(CommandIDs.refresh, {
406
+ label: trans.__('Refresh Terminal'),
407
+ caption: trans.__('Refresh the current terminal session'),
408
+ execute: async () => {
409
+ const current = tracker.currentWidget;
410
+ if (!current) {
411
+ return;
412
+ }
413
+ app.shell.activateById(current.id);
414
+ try {
415
+ await current.content.refresh();
416
+ if (current) {
417
+ current.content.activate();
418
+ }
419
+ } catch (err) {
420
+ Private.showErrorMessage(err);
421
+ }
422
+ },
423
+ icon: args =>
424
+ args['isPalette']
425
+ ? undefined
426
+ : refreshIcon.bindprops({ stylesheet: 'menuItem' }),
427
+ isEnabled
428
+ });
429
+
430
+ /**
431
+ * Add copy command
432
+ */
433
+ commands.addCommand(CommandIDs.copy, {
434
+ execute: () => {
435
+ const widget = tracker.currentWidget?.content;
436
+
437
+ if (!widget) {
438
+ return;
439
+ }
440
+
441
+ const text = widget.getSelection();
442
+
443
+ if (text) {
444
+ Clipboard.copyToSystem(text);
445
+ }
446
+ },
447
+ isEnabled: () => {
448
+ if (!isEnabled()) {
449
+ return false;
450
+ }
451
+
452
+ const widget = tracker.currentWidget?.content;
453
+
454
+ if (!widget) {
455
+ return false;
456
+ }
457
+
458
+ // Enable command if there is a text selection in the terminal
459
+ return widget.hasSelection();
460
+ },
461
+ icon: copyIcon.bindprops({ stylesheet: 'menuItem' }),
462
+ label: trans.__('Copy')
463
+ });
464
+
465
+ /**
466
+ * Add paste command
467
+ */
468
+ commands.addCommand(CommandIDs.paste, {
469
+ execute: async () => {
470
+ const widget = tracker.currentWidget?.content;
471
+
472
+ if (!widget) {
473
+ return;
474
+ }
475
+
476
+ // Get data from clipboard
477
+ const clipboard = window.navigator.clipboard;
478
+ const clipboardData: string = await clipboard.readText();
479
+
480
+ if (clipboardData) {
481
+ // Paste data to the terminal
482
+ widget.paste(clipboardData);
483
+ }
484
+ },
485
+ isEnabled: () => Boolean(isEnabled() && tracker.currentWidget?.content),
486
+ icon: pasteIcon.bindprops({ stylesheet: 'menuItem' }),
487
+ label: trans.__('Paste')
488
+ });
489
+
490
+ commands.addCommand(CommandIDs.shutdown, {
491
+ label: trans.__('Shutdown Terminal'),
492
+ execute: () => {
493
+ const current = tracker.currentWidget;
494
+ if (!current) {
495
+ return;
496
+ }
497
+
498
+ // The widget is automatically disposed upon session shutdown.
499
+ return current.content.session.shutdown();
500
+ },
501
+ isEnabled
502
+ });
503
+
504
+ commands.addCommand(CommandIDs.increaseFont, {
505
+ label: trans.__('Increase Terminal Font Size'),
506
+ execute: async () => {
507
+ const { fontSize } = options;
508
+ if (fontSize && fontSize < 72) {
509
+ try {
510
+ await settingRegistry.set(plugin.id, 'fontSize', fontSize + 1);
511
+ } catch (err) {
512
+ Private.showErrorMessage(err);
513
+ }
514
+ }
515
+ }
516
+ });
517
+
518
+ commands.addCommand(CommandIDs.decreaseFont, {
519
+ label: trans.__('Decrease Terminal Font Size'),
520
+ execute: async () => {
521
+ const { fontSize } = options;
522
+ if (fontSize && fontSize > 9) {
523
+ try {
524
+ await settingRegistry.set(plugin.id, 'fontSize', fontSize - 1);
525
+ } catch (err) {
526
+ Private.showErrorMessage(err);
527
+ }
528
+ }
529
+ }
530
+ });
531
+
532
+ const themeDisplayedName = {
533
+ inherit: trans.__('Inherit'),
534
+ light: trans.__('Light'),
535
+ dark: trans.__('Dark')
536
+ };
537
+
538
+ commands.addCommand(CommandIDs.setTheme, {
539
+ label: args => {
540
+ if (args.theme === undefined) {
541
+ return trans.__('Set terminal theme to the provided `theme`.');
542
+ }
543
+ const theme = args['theme'] as string;
544
+ const displayName =
545
+ theme in themeDisplayedName
546
+ ? themeDisplayedName[theme as keyof typeof themeDisplayedName]
547
+ : trans.__(theme[0].toUpperCase() + theme.slice(1));
548
+ return args['isPalette']
549
+ ? trans.__('Use Terminal Theme: %1', displayName)
550
+ : displayName;
551
+ },
552
+ caption: trans.__('Set the terminal theme'),
553
+ isToggled: args => {
554
+ const { theme } = options;
555
+ return args['theme'] === theme;
556
+ },
557
+ execute: async args => {
558
+ const theme = args['theme'] as ITerminal.Theme;
559
+ try {
560
+ await settingRegistry.set(plugin.id, 'theme', theme);
561
+ commands.notifyCommandChanged(CommandIDs.setTheme);
562
+ } catch (err) {
563
+ console.log(err);
564
+ Private.showErrorMessage(err);
565
+ }
566
+ }
567
+ });
568
+ }
569
+
570
+ /**
571
+ * A namespace for private data.
572
+ */
573
+ namespace Private {
574
+ /**
575
+ * Utility function for consistent error reporting
576
+ */
577
+ export function showErrorMessage(error: Error): void {
578
+ console.error(`Failed to configure ${plugin.id}: ${error.message}`);
579
+ }
580
+ }