Skip to content

Commit 5cef0d9

Browse files
authored
Let VSCode translate ascii/terminal characters in output (#12095)
For #10496
1 parent 1a4acc7 commit 5cef0d9

File tree

2 files changed

+8
-90
lines changed

2 files changed

+8
-90
lines changed

src/client/datascience/notebook/helpers.ts

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ import { createCodeCell, createMarkdownCell } from '../../../datascience-ui/comm
2525
import { MARKDOWN_LANGUAGE, PYTHON_LANGUAGE } from '../../common/constants';
2626
import { traceError, traceWarning } from '../../logging';
2727
import { CellState, ICell, INotebookModel } from '../types';
28-
// tslint:disable-next-line: no-var-requires no-require-imports
29-
const ansiToHtml = require('ansi-to-html');
30-
// tslint:disable-next-line: no-var-requires no-require-imports
31-
const ansiRegex = require('ansi-regex');
3228

3329
/**
3430
* Converts a NotebookModel into VSCode friendly format.
@@ -200,43 +196,14 @@ function isImagePngOrJpegMimeType(mimeType: string) {
200196
return mimeType === 'image/png' || mimeType === 'image/jpeg';
201197
}
202198
function translateStreamOutput(output: nbformat.IStream): CellStreamOutput | CellDisplayOutput {
203-
const text = concatMultilineStringOutput(output.text);
204-
const hasAngleBrackets = text.includes('<');
205-
const hasAnsiChars = ansiRegex().test(text);
206-
207-
if (!hasAngleBrackets && !hasAnsiChars) {
208-
// Plain text output.
209-
return {
210-
outputKind: vscodeNotebookEnums.CellOutputKind.Text,
211-
text
212-
};
213-
}
214-
215-
// Format the output, but ensure we have the plain text output as well.
216-
const richOutput: CellDisplayOutput = {
199+
// Do not return as `CellOutputKind.Text`. VSC will not translate ascii output correctly.
200+
// Instead format the output as rich.
201+
return {
217202
outputKind: vscodeNotebookEnums.CellOutputKind.Rich,
218203
data: {
219-
['text/plain']: text
204+
['text/plain']: concatMultilineStringOutput(output.text)
220205
}
221206
};
222-
223-
if (hasAngleBrackets) {
224-
// Stream output needs to be wrapped in xmp so it
225-
// show literally. Otherwise < chars start a new html element.
226-
richOutput.data['text/html'] = `<xmp>${text}</xmp>`;
227-
}
228-
if (hasAnsiChars) {
229-
// ansiToHtml is different between the tests running and webpack. figure out which one
230-
try {
231-
const ctor = ansiToHtml instanceof Function ? ansiToHtml : ansiToHtml.default;
232-
const converter = new ctor(getAnsiToHtmlOptions());
233-
richOutput.data['text/html'] = converter.toHtml(text);
234-
} catch (ex) {
235-
traceError(`Failed to convert Ansi text to HTML, ${text}`, ex);
236-
}
237-
}
238-
239-
return richOutput;
240207
}
241208
export function translateErrorOutput(output: nbformat.IError): CellErrorOutput {
242209
return {
@@ -246,48 +213,3 @@ export function translateErrorOutput(output: nbformat.IError): CellErrorOutput {
246213
traceback: output.traceback
247214
};
248215
}
249-
250-
function getAnsiToHtmlOptions(): { fg: string; bg: string; colors: string[] } {
251-
// Here's the default colors for ansiToHtml. We need to use the
252-
// colors from our current theme.
253-
// const colors = {
254-
// 0: '#000',
255-
// 1: '#A00',
256-
// 2: '#0A0',
257-
// 3: '#A50',
258-
// 4: '#00A',
259-
// 5: '#A0A',
260-
// 6: '#0AA',
261-
// 7: '#AAA',
262-
// 8: '#555',
263-
// 9: '#F55',
264-
// 10: '#5F5',
265-
// 11: '#FF5',
266-
// 12: '#55F',
267-
// 13: '#F5F',
268-
// 14: '#5FF',
269-
// 15: '#FFF'
270-
// };
271-
return {
272-
fg: 'var(--vscode-terminal-foreground)',
273-
bg: 'var(--vscode-terminal-background)',
274-
colors: [
275-
'var(--vscode-terminal-ansiBlack)', // 0
276-
'var(--vscode-terminal-ansiBrightRed)', // 1
277-
'var(--vscode-terminal-ansiGreen)', // 2
278-
'var(--vscode-terminal-ansiYellow)', // 3
279-
'var(--vscode-terminal-ansiBrightBlue)', // 4
280-
'var(--vscode-terminal-ansiMagenta)', // 5
281-
'var(--vscode-terminal-ansiCyan)', // 6
282-
'var(--vscode-terminal-ansiBrightBlack)', // 7
283-
'var(--vscode-terminal-ansiWhite)', // 8
284-
'var(--vscode-terminal-ansiRed)', // 9
285-
'var(--vscode-terminal-ansiBrightGreen)', // 10
286-
'var(--vscode-terminal-ansiBrightYellow)', // 11
287-
'var(--vscode-terminal-ansiBlue)', // 12
288-
'var(--vscode-terminal-ansiBrightMagenta)', // 13
289-
'var(--vscode-terminal-ansiBrightCyan)', // 14
290-
'var(--vscode-terminal-ansiBrightWhite)' // 15
291-
]
292-
};
293-
}

src/test/datascience/notebook/helpers.unit.test.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ suite('Data Science - NativeNotebook helpers', () => {
124124
],
125125
[
126126
{
127-
outputKind: vscodeNotebookEnums.CellOutputKind.Text,
128-
text: 'Error'
127+
outputKind: vscodeNotebookEnums.CellOutputKind.Rich,
128+
data: { 'text/plain': 'Error' }
129129
},
130130
{
131-
outputKind: vscodeNotebookEnums.CellOutputKind.Text,
132-
text: 'NoError'
131+
outputKind: vscodeNotebookEnums.CellOutputKind.Rich,
132+
data: { 'text/plain': 'NoError' }
133133
}
134134
]
135135
);
@@ -147,7 +147,6 @@ suite('Data Science - NativeNotebook helpers', () => {
147147
{
148148
outputKind: vscodeNotebookEnums.CellOutputKind.Rich,
149149
data: {
150-
'text/html': '<span style="color:var(--vscode-terminal-ansiYellow)">✅ </span> Loading\n',
151150
'text/plain': '\u001b[K\u001b[33m✅ \u001b[0m Loading\n'
152151
}
153152
}
@@ -167,7 +166,6 @@ suite('Data Science - NativeNotebook helpers', () => {
167166
{
168167
outputKind: vscodeNotebookEnums.CellOutputKind.Rich,
169168
data: {
170-
'text/html': '<xmp>1 is < 2</xmp>',
171169
'text/plain': '1 is < 2'
172170
}
173171
}
@@ -187,8 +185,6 @@ suite('Data Science - NativeNotebook helpers', () => {
187185
{
188186
outputKind: vscodeNotebookEnums.CellOutputKind.Rich,
189187
data: {
190-
'text/html':
191-
'1 is < 2<span style="color:var(--vscode-terminal-ansiYellow)">✅ </span> Loading\n',
192188
'text/plain': '1 is < 2\u001b[K\u001b[33m✅ \u001b[0m Loading\n'
193189
}
194190
}

0 commit comments

Comments
 (0)