UNPKG

jupyterlab-emrys

Version:

A computational environment for Jupyter. Powered by Emrys

151 lines (150 loc) 4.92 kB
// Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. "use strict"; var jupyter_js_services_1 = require('jupyter-js-services'); var widgettracker_1 = require('../widgettracker'); var index_1 = require('./index'); var plugin_1 = require('../mainmenu/plugin'); var phosphor_menus_1 = require('phosphor-menus'); /** * The default terminal extension. */ exports.terminalExtension = { id: 'jupyter.extensions.terminal', requires: [jupyter_js_services_1.ServiceManager, plugin_1.MainMenu], activate: activateTerminal }; /** * The class name for all main area landscape tab icons. */ var LANDSCAPE_ICON_CLASS = 'jp-MainAreaLandscapeIcon'; /** * The class name for the terminal icon in the default theme. */ var TERMINAL_ICON_CLASS = 'jp-ImageTerminal'; function activateTerminal(app, services, mainMenu) { var newTerminalId = 'terminal:create-new'; var increaseTerminalFontSize = 'terminal:increase-font'; var decreaseTerminalFontSize = 'terminal:decrease-font'; var toggleTerminalTheme = 'terminal:toggle-theme'; var closeAllTerminals = 'terminal:close-all-terminals'; var tracker = new widgettracker_1.WidgetTracker(); var options = { background: 'black', color: 'white', fontSize: 14 }; app.commands.add([ { id: newTerminalId, handler: function () { var term = new index_1.TerminalWidget(options); term.title.closable = true; term.title.icon = LANDSCAPE_ICON_CLASS + " " + TERMINAL_ICON_CLASS; app.shell.addToMainArea(term); tracker.addWidget(term); services.terminals.create().then(function (session) { term.session = session; // Trigger an update of the running kernels. services.terminals.listRunning(); }); } }, { id: increaseTerminalFontSize, handler: increaseFont }, { id: decreaseTerminalFontSize, handler: decreaseFont }, { id: toggleTerminalTheme, handler: toggleTheme } ]); app.palette.add([ { command: newTerminalId, category: 'Terminal', text: 'New Terminal', caption: 'Start a new terminal session' }, { command: increaseTerminalFontSize, category: 'Terminal', text: 'Increase Terminal Font Size', }, { command: decreaseTerminalFontSize, category: 'Terminal', text: 'Decrease Terminal Font Size', }, { command: toggleTerminalTheme, category: 'Terminal', text: 'Toggle Terminal Theme', caption: 'Switch Terminal Background and Font Colors' } ]); var menu = new phosphor_menus_1.Menu([ new phosphor_menus_1.MenuItem({ text: 'New Terminal', handler: function () { app.commands.execute(newTerminalId); } }), new phosphor_menus_1.MenuItem({ text: 'Increase Font Size', handler: increaseFont }), new phosphor_menus_1.MenuItem({ text: 'Decrease Font Size', handler: decreaseFont }), new phosphor_menus_1.MenuItem({ text: 'Toggle Theme', handler: toggleTheme }) ]); var terminalMenu = new phosphor_menus_1.MenuItem({ text: 'Terminal', submenu: menu }); mainMenu.addItem(terminalMenu, { rank: 40 }); function increaseFont() { if (!tracker.isDisposed && options.fontSize < 72) { var widgets = tracker.widgets; options.fontSize++; for (var i = 0; i < widgets.length; i++) { widgets[i].fontSize = options.fontSize; } } } function decreaseFont() { if (!tracker.isDisposed && options.fontSize > 9) { var widgets = tracker.widgets; options.fontSize--; for (var i = 0; i < widgets.length; i++) { widgets[i].fontSize = options.fontSize; } } } function toggleTheme() { if (!tracker.isDisposed) { var widgets = tracker.widgets; if (options.background === 'black') { options.background = 'white'; options.color = 'black'; } else { options.background = 'black'; options.color = 'white'; } for (var i = 0; i < widgets.length; i++) { widgets[i].background = options.background; widgets[i].color = options.color; } } } }