UNPKG

jupyterlab-emrys

Version:

A computational environment for Jupyter. Powered by Emrys

133 lines (132 loc) 4.41 kB
// Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. "use strict"; var phosphor_signaling_1 = require('phosphor-signaling'); /** * A console history manager object. */ var ConsoleHistory = (function () { /** * Construct a new console history object. */ function ConsoleHistory(kernel) { this._cursor = 0; this._history = null; this._kernel = null; this._history = []; if (kernel) { this.kernel = kernel; } } Object.defineProperty(ConsoleHistory.prototype, "isDisposed", { /** * Get whether the console history manager is disposed. * * #### Notes * This is a read-only property. */ get: function () { return this._history === null; }, enumerable: true, configurable: true }); Object.defineProperty(ConsoleHistory.prototype, "kernel", { /** * The current kernel supplying navigation history. */ get: function () { return this._kernel; }, set: function (newValue) { var _this = this; if (newValue === this._kernel) { return; } var contents = Private.initialRequest; this._kernel = newValue; this._kernel.history(contents).then(function (value) { _this._history = []; var last = ''; var current = ''; for (var i = 0; i < value.content.history.length; i++) { // History entries have the shape: // [session: number, line: number, input: string] // Contiguous duplicates are stripped out of the API response. current = value.content.history[i][2]; if (current !== last) { _this._history.push(last = current); } } // Reset the history navigation cursor back to the bottom. _this._cursor = _this._history.length; }); }, enumerable: true, configurable: true }); /** * Get the previous item in the console history. * * @returns A Promise for console command text or `undefined` if unavailable. */ ConsoleHistory.prototype.back = function () { var content = this._history[--this._cursor]; this._cursor = Math.max(0, this._cursor); return Promise.resolve(content); }; /** * Dispose of the resources held by the console history manager. */ ConsoleHistory.prototype.dispose = function () { if (this.isDisposed) { return; } phosphor_signaling_1.clearSignalData(this); this._history.length = 0; this._history = null; }; /** * Get the next item in the console history. * * @returns A Promise for console command text or `undefined` if unavailable. */ ConsoleHistory.prototype.forward = function () { var content = this._history[++this._cursor]; this._cursor = Math.min(this._history.length, this._cursor); return Promise.resolve(content); }; /** * Add a new item to the bottom of history. * * @param item The item being added to the bottom of history. * * #### Notes * If the item being added is undefined or empty, it is ignored. If the item * being added is the same as the last item in history, it is ignored as well * so that the console's history will consist of no contiguous repetitions. * This behavior varies from some shells, but the Jupyter Qt Console is * implemented this way. */ ConsoleHistory.prototype.push = function (item) { if (item && item !== this._history[this._history.length - 1]) { this._history.push(item); } // Reset the history navigation cursor back to the bottom. this._cursor = this._history.length; }; return ConsoleHistory; }()); exports.ConsoleHistory = ConsoleHistory; /** * A namespace for private data. */ var Private; (function (Private) { Private.initialRequest = { output: false, raw: true, hist_access_type: 'tail', n: 500 }; })(Private || (Private = {}));