Skip to content

Commit 4f8133e

Browse files
committed
handle ansi colors
1 parent f9608b0 commit 4f8133e

4 files changed

Lines changed: 12 additions & 6 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@
709709
"dependencies": {
710710
"@types/jquery": "^1.10.31",
711711
"@types/uuid": "^3.3.27",
712+
"anser": "^1.1.0",
712713
"copy-paste": "^1.3.0",
713714
"diff-match-patch": "^1.0.0",
714715
"fs-extra": "^0.30.0",

src/client/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as telemetryContracts from './common/telemetryContracts';
1919
import {PythonCodeActionsProvider} from './providers/codeActionProvider';
2020
import {activateSimplePythonRefactorProvider} from './providers/simpleRefactorProvider';
2121
import {activateSetInterpreterProvider} from './providers/setInterpreterProvider';
22-
import {activateExecInTerminalProvider} from './providers/execInTerminalProvider'
22+
import {activateExecInTerminalProvider} from './providers/execInTerminalProvider';
2323
import * as tests from './unittests/main';
2424
import * as jup from './jupyter/main';
2525

src/client/jupyter/main.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {KernelManager} from './kernel-manager';
22
import {Kernel} from './kernel';
33
import * as vscode from 'vscode';
44
import {TextDocumentContentProvider} from './resultView';
5+
const anser = require('anser');
56

67
const jupyterSchema = 'jupyter-result-viewer';
78
let previewUri = vscode.Uri.parse(jupyterSchema + '://authority/jupyter');
@@ -87,14 +88,18 @@ export class Jupyter {
8788
let htmlResponse = '';
8889
return kernel.execute(code, (result: { type: string, stream: string, data: { [key: string]: string } | string }) => {
8990
if (result.type === 'text' && result.stream === 'stdout' && typeof result.data['text/plain'] === 'string') {
90-
htmlResponse = htmlResponse + `<p><pre>${result.data['text/plain']}</pre></p>`;
91+
const htmlText = anser.ansiToHtml(anser.escapeForHtml(result.data['text/plain']));
92+
htmlResponse = htmlResponse + `<p><pre>${htmlText}</pre></p>`;
9193
}
9294
if (result.type === 'text' && result.stream === 'pyout' && typeof result.data['text/plain'] === 'string') {
93-
htmlResponse = htmlResponse + `<p><pre>${result.data['text/plain']}</pre></p>`;
95+
const htmlText = anser.ansiToHtml(anser.escapeForHtml(result.data['text/plain']));
96+
htmlResponse = htmlResponse + `<p><pre>${htmlText}</pre></p>`;
9497
}
9598
if (result.type === 'text' && result.stream === 'error' && typeof result.data['text/plain'] === 'string') {
96-
let rawError = (result.data['text/plain'] as string).replace('\n', '<br/>');
97-
htmlResponse = htmlResponse + `<p style="color:red;">${rawError}</p>`;
99+
const htmlText = anser.ansiToHtml(anser.escapeForHtml(result.data['text/plain']));
100+
let rawError = htmlText.replace('\n', '<br/>');
101+
// htmlResponse = htmlResponse + `<p style="color:black;background-color:white">${rawError}</p>`;
102+
htmlResponse = htmlResponse + `<p><pre>${rawError}</pre></p>`;
98103
return resolve(htmlResponse);
99104
}
100105
if (result.type === 'text/html' && result.stream === 'pyout' && typeof result.data['text/html'] === 'string') {

typings/spawnteract.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
declare module "spawnteract" {
1+
declare module 'spawnteract' {
22
import * as child_process from 'child_process';
33
interface LaunchSpecSpawnResult {
44
spawn: child_process.ChildProcess;

0 commit comments

Comments
 (0)