|
| 1 | +// ***************************************************************************** |
| 2 | +// Copyright (C) 2023 TypeFox and others. |
| 3 | +// |
| 4 | +// This program and the accompanying materials are made available under the |
| 5 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | +// http://www.eclipse.org/legal/epl-2.0. |
| 7 | +// |
| 8 | +// This Source Code may also be made available under the following Secondary |
| 9 | +// Licenses when the conditions for such availability set forth in the Eclipse |
| 10 | +// Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 11 | +// with the GNU Classpath Exception which is available at |
| 12 | +// https://www.gnu.org/software/classpath/license.html. |
| 13 | +// |
| 14 | +// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 |
| 15 | +// ***************************************************************************** |
| 16 | +/*--------------------------------------------------------------------------------------------- |
| 17 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 18 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 19 | + * Copied from commit 18b2c92451b076943e5b508380e0eba66ba7d934 from file src\vs\workbench\contrib\notebook\common\notebookCommon.ts |
| 20 | + *--------------------------------------------------------------------------------------------*/ |
| 21 | + |
| 22 | +import { BinaryBuffer } from '@theia/core/lib/common/buffer'; |
| 23 | + |
| 24 | +const textDecoder = new TextDecoder(); |
| 25 | + |
| 26 | +/** |
| 27 | + * Given a stream of individual stdout outputs, this function will return the compressed lines, escaping some of the common terminal escape codes. |
| 28 | + * E.g. some terminal escape codes would result in the previous line getting cleared, such if we had 3 lines and |
| 29 | + * last line contained such a code, then the result string would be just the first two lines. |
| 30 | + * @returns a single VSBuffer with the concatenated and compressed data, and whether any compression was done. |
| 31 | + */ |
| 32 | +export function compressOutputItemStreams(outputs: Uint8Array[]): { data: BinaryBuffer, didCompression: boolean } { |
| 33 | + const buffers: Uint8Array[] = []; |
| 34 | + let startAppending = false; |
| 35 | + |
| 36 | + // Pick the first set of outputs with the same mime type. |
| 37 | + for (const output of outputs) { |
| 38 | + if ((buffers.length === 0 || startAppending)) { |
| 39 | + buffers.push(output); |
| 40 | + startAppending = true; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + let didCompression = compressStreamBuffer(buffers); |
| 45 | + const concatenated = BinaryBuffer.concat(buffers.map(buffer => BinaryBuffer.wrap(buffer))); |
| 46 | + const data = formatStreamText(concatenated); |
| 47 | + didCompression = didCompression || data.byteLength !== concatenated.byteLength; |
| 48 | + return { data, didCompression }; |
| 49 | +} |
| 50 | + |
| 51 | +export const MOVE_CURSOR_1_LINE_COMMAND = `${String.fromCharCode(27)}[A`; |
| 52 | +const MOVE_CURSOR_1_LINE_COMMAND_BYTES = MOVE_CURSOR_1_LINE_COMMAND.split('').map(c => c.charCodeAt(0)); |
| 53 | +const LINE_FEED = 10; |
| 54 | +function compressStreamBuffer(streams: Uint8Array[]): boolean { |
| 55 | + let didCompress = false; |
| 56 | + streams.forEach((stream, index) => { |
| 57 | + if (index === 0 || stream.length < MOVE_CURSOR_1_LINE_COMMAND.length) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const previousStream = streams[index - 1]; |
| 62 | + |
| 63 | + // Remove the previous line if required. |
| 64 | + const command = stream.subarray(0, MOVE_CURSOR_1_LINE_COMMAND.length); |
| 65 | + if (command[0] === MOVE_CURSOR_1_LINE_COMMAND_BYTES[0] && command[1] === MOVE_CURSOR_1_LINE_COMMAND_BYTES[1] && command[2] === MOVE_CURSOR_1_LINE_COMMAND_BYTES[2]) { |
| 66 | + const lastIndexOfLineFeed = previousStream.lastIndexOf(LINE_FEED); |
| 67 | + if (lastIndexOfLineFeed === -1) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + didCompress = true; |
| 72 | + streams[index - 1] = previousStream.subarray(0, lastIndexOfLineFeed); |
| 73 | + streams[index] = stream.subarray(MOVE_CURSOR_1_LINE_COMMAND.length); |
| 74 | + } |
| 75 | + }); |
| 76 | + return didCompress; |
| 77 | +} |
| 78 | + |
| 79 | +const BACKSPACE_CHARACTER = '\b'.charCodeAt(0); |
| 80 | +const CARRIAGE_RETURN_CHARACTER = '\r'.charCodeAt(0); |
| 81 | +function formatStreamText(buffer: BinaryBuffer): BinaryBuffer { |
| 82 | + // We have special handling for backspace and carriage return characters. |
| 83 | + // Don't unnecessary decode the bytes if we don't need to perform any processing. |
| 84 | + if (!buffer.buffer.includes(BACKSPACE_CHARACTER) && !buffer.buffer.includes(CARRIAGE_RETURN_CHARACTER)) { |
| 85 | + return buffer; |
| 86 | + } |
| 87 | + // Do the same thing jupyter is doing |
| 88 | + return BinaryBuffer.fromString(fixCarriageReturn(fixBackspace(textDecoder.decode(buffer.buffer)))); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Took this from jupyter/notebook |
| 93 | + * https://github.com/jupyter/notebook/blob/b8b66332e2023e83d2ee04f83d8814f567e01a4e/notebook/static/base/js/utils.js |
| 94 | + * Remove characters that are overridden by backspace characters |
| 95 | + */ |
| 96 | +function fixBackspace(txt: string): string { |
| 97 | + let tmp = txt; |
| 98 | + do { |
| 99 | + txt = tmp; |
| 100 | + // Cancel out anything-but-newline followed by backspace |
| 101 | + tmp = txt.replace(/[^\n]\x08/gm, ''); |
| 102 | + } while (tmp.length < txt.length); |
| 103 | + return txt; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Remove chunks that should be overridden by the effect of carriage return characters |
| 108 | + * From https://github.com/jupyter/notebook/blob/master/notebook/static/base/js/utils.js |
| 109 | + */ |
| 110 | +function fixCarriageReturn(txt: string): string { |
| 111 | + txt = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline |
| 112 | + while (txt.search(/\r[^$]/g) > -1) { |
| 113 | + const base = txt.match(/^(.*)\r+/m)![1]; |
| 114 | + let insert = txt.match(/\r+(.*)$/m)![1]; |
| 115 | + insert = insert + base.slice(insert.length, base.length); |
| 116 | + txt = txt.replace(/\r+.*$/m, '\r').replace(/^.*\r/m, insert); |
| 117 | + } |
| 118 | + return txt; |
| 119 | +} |
0 commit comments