1- import { KernelManager } from './kernel-manager' ;
1+ import { KernelManagerImpl } from './kernel-manager' ;
22import { Kernel } from './kernel' ;
33import * as vscode from 'vscode' ;
44import { TextDocumentContentProvider } from './resultView' ;
5+ import { JupyterDisplay } from './display/main' ;
6+ import { KernelStatus } from './display/kernelStatus' ;
7+
58const anser = require ( 'anser' ) ;
69
710const jupyterSchema = 'jupyter-result-viewer' ;
811let previewUri = vscode . Uri . parse ( jupyterSchema + '://authority/jupyter' ) ;
912
1013let previewWindow : TextDocumentContentProvider ;
11- export function activate ( ) : vscode . Disposable {
14+ let display : JupyterDisplay ;
15+ export function activate ( ) : vscode . Disposable [ ] {
1216 previewWindow = new TextDocumentContentProvider ( ) ;
13- let registration = vscode . workspace . registerTextDocumentContentProvider ( jupyterSchema , previewWindow ) ;
14- return registration ;
17+ let disposables : vscode . Disposable [ ] = [ ] ;
18+ disposables . push ( vscode . workspace . registerTextDocumentContentProvider ( jupyterSchema , previewWindow ) ) ;
19+ display = new JupyterDisplay ( ) ;
20+ disposables . push ( display ) ;
21+ return disposables ;
1522}
1623
1724let displayed = false ;
@@ -26,29 +33,33 @@ function showResults(result: any): any {
2633 vscode . window . showErrorMessage ( reason ) ;
2734 } ) ;
2835}
29- export class Jupyter {
30- public subscriptions = null ;
31- public kernelManager : KernelManager ;
36+ export class Jupyter extends vscode . Disposable {
37+ public kernelManager : KernelManagerImpl ;
3238 public editor : vscode . TextEditor ;
3339 public kernel : Kernel = null ;
3440 public markerBubbleMap = null ;
35- public statusBarElement = null ;
36- public statusBarTile = null ;
3741 public watchSidebar = null ;
3842 public watchSidebarIsVisible = false ;
43+ private status : KernelStatus ;
44+ private disposables : vscode . Disposable [ ] ;
3945
46+ constructor ( ) {
47+ super ( ( ) => { } ) ;
48+ this . disposables = [ ] ;
49+ }
4050 activate ( state ) {
41- activate ( ) ;
42- this . kernelManager = new KernelManager ( ) ;
51+ this . disposables . push ( ...activate ( ) ) ;
52+ this . kernelManager = new KernelManagerImpl ( ) ;
53+ this . disposables . push ( this . kernelManager ) ;
4354 this . markerBubbleMap = { } ;
44- vscode . window . onDidChangeActiveTextEditor ( this . onEditorChanged . bind ( this ) ) ;
55+ this . disposables . push ( vscode . window . onDidChangeActiveTextEditor ( this . onEditorChanged . bind ( this ) ) ) ;
56+ this . status = new KernelStatus ( ) ;
57+ this . disposables . push ( this . status ) ;
4558 }
46- deactivate ( ) {
47- this . subscriptions . dispose ( ) ;
48- this . kernelManager . destroy ( ) ;
49- return this . statusBarTile . destroy ( ) ;
59+ public dispose ( ) {
60+ this . disposables . forEach ( d => d . dispose ( ) ) ;
5061 }
51- onEditorChanged ( editor ) {
62+ private onEditorChanged ( editor ) {
5263 // Opening display (results) documents causes event to fire
5364 if ( ! editor ) {
5465 return ;
@@ -62,8 +73,20 @@ export class Jupyter {
6273 return this . onKernelChanged ( kernel ) ;
6374 }
6475 }
76+ private onKernalStatusChangeHandler : vscode . Disposable ;
6577 onKernelChanged ( kernel : Kernel ) {
78+ // Unbind any previous handlers
79+ if ( this . onKernalStatusChangeHandler ) {
80+ this . onKernalStatusChangeHandler . dispose ( ) ;
81+ this . onKernalStatusChangeHandler = null ;
82+ }
83+ if ( kernel ) {
84+ this . onKernalStatusChangeHandler = kernel . onStatusChange ( statusInfo => {
85+ this . status . setKernelStatus ( statusInfo [ 1 ] ) ;
86+ } ) ;
87+ }
6688 this . kernel = kernel ;
89+ this . status . setActiveKernel ( this . kernel ? this . kernel . kernelSpec : null ) ;
6790 }
6891 createResultBubble ( code ) : Promise < any > {
6992 if ( this . kernel ) {
@@ -87,20 +110,16 @@ export class Jupyter {
87110 return new Promise < any > ( ( resolve , reject ) => {
88111 let htmlResponse = '' ;
89112 return kernel . execute ( code , ( result : { type : string , stream : string , data : { [ key : string ] : string } | string } ) => {
90- if ( result . type === 'text' && result . stream === 'stdout' && typeof result . data [ 'text/plain' ] === 'string' ) {
113+ if ( ( result . type === 'text' && result . stream === 'stdout' && typeof result . data [ 'text/plain' ] === 'string' ) ||
114+ ( result . type === 'text' && result . stream === 'pyout' && typeof result . data [ 'text/plain' ] === 'string' ) ||
115+ ( result . type === 'text' && result . stream === 'error' && typeof result . data [ 'text/plain' ] === 'string' ) ) {
91116 const htmlText = anser . ansiToHtml ( anser . escapeForHtml ( result . data [ 'text/plain' ] ) ) ;
117+ // let rawError = htmlText.replace('\n', '<br/>');
92118 htmlResponse = htmlResponse + `<p><pre>${ htmlText } </pre></p>` ;
93- }
94- if ( result . type === 'text' && result . stream === 'pyout' && typeof result . data [ 'text/plain' ] === 'string' ) {
95- const htmlText = anser . ansiToHtml ( anser . escapeForHtml ( result . data [ 'text/plain' ] ) ) ;
96- htmlResponse = htmlResponse + `<p><pre>${ htmlText } </pre></p>` ;
97- }
98- if ( result . type === 'text' && result . stream === 'error' && typeof result . data [ 'text/plain' ] === 'string' ) {
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>` ;
103- return resolve ( htmlResponse ) ;
119+
120+ if ( result . stream === 'error' ) {
121+ return resolve ( htmlResponse ) ;
122+ }
104123 }
105124 if ( result . type === 'text/html' && result . stream === 'pyout' && typeof result . data [ 'text/html' ] === 'string' ) {
106125 htmlResponse = htmlResponse + result . data [ 'text/html' ] ;
0 commit comments