|
@@ -30,11 +30,18 @@ export class Terminal extends Widget implements ITerminal.ITerminal {
|
|
|
/**
|
|
|
* Construct a new terminal widget.
|
|
|
*
|
|
|
+ * @param session - The terminal session object.
|
|
|
+ *
|
|
|
* @param options - The terminal configuration options.
|
|
|
*/
|
|
|
- constructor(options: Partial<ITerminal.IOptions> = {}) {
|
|
|
+ constructor(
|
|
|
+ session: TerminalSession.ISession,
|
|
|
+ options: Partial<ITerminal.IOptions> = {}
|
|
|
+ ) {
|
|
|
super();
|
|
|
|
|
|
+ this.session = session;
|
|
|
+
|
|
|
// Initialize settings.
|
|
|
this._options = { ...ITerminal.defaultOptions, ...options };
|
|
|
|
|
@@ -49,31 +56,19 @@ export class Terminal extends Widget implements ITerminal.ITerminal {
|
|
|
|
|
|
this.id = `jp-Terminal-${Private.id++}`;
|
|
|
this.title.label = 'Terminal';
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * The terminal session associated with the widget.
|
|
|
- */
|
|
|
- get session(): TerminalSession.ISession | null {
|
|
|
- return this._session;
|
|
|
- }
|
|
|
- set session(value: TerminalSession.ISession | null) {
|
|
|
- if (this._session && !this._session.isDisposed) {
|
|
|
- this._session.messageReceived.disconnect(this._onMessage, this);
|
|
|
- }
|
|
|
- this._session = value || null;
|
|
|
- if (!value) {
|
|
|
- return;
|
|
|
- }
|
|
|
- void value.ready.then(() => {
|
|
|
- if (this.isDisposed || value !== this._session) {
|
|
|
+ session.messageReceived.connect(this._onMessage, this);
|
|
|
+ session.terminated.connect(this.dispose, this);
|
|
|
+
|
|
|
+ void session.ready.then(() => {
|
|
|
+ if (this.isDisposed) {
|
|
|
return;
|
|
|
}
|
|
|
- value.messageReceived.connect(this._onMessage, this);
|
|
|
- this.title.label = `Terminal ${value.name}`;
|
|
|
+
|
|
|
+ this.title.label = `Terminal ${session.name}`;
|
|
|
this._setSessionSize();
|
|
|
if (this._options.initialCommand) {
|
|
|
- this._session.send({
|
|
|
+ this.session.send({
|
|
|
type: 'stdin',
|
|
|
content: [this._options.initialCommand + '\r']
|
|
|
});
|
|
@@ -81,6 +76,11 @@ export class Terminal extends Widget implements ITerminal.ITerminal {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * The terminal session associated with the widget.
|
|
|
+ */
|
|
|
+ readonly session: TerminalSession.ISession;
|
|
|
+
|
|
|
/**
|
|
|
* Get a config option for the terminal.
|
|
|
*/
|
|
@@ -128,14 +128,13 @@ export class Terminal extends Widget implements ITerminal.ITerminal {
|
|
|
* Dispose of the resources held by the terminal widget.
|
|
|
*/
|
|
|
dispose(): void {
|
|
|
- if (this._session) {
|
|
|
+ if (!this.session.isDisposed) {
|
|
|
if (this.getOption('shutdownOnClose')) {
|
|
|
- this._session.shutdown().catch(reason => {
|
|
|
+ this.session.shutdown().catch(reason => {
|
|
|
console.error(`Terminal not shut down: ${reason}`);
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
- this._session = null;
|
|
|
this._term.dispose();
|
|
|
super.dispose();
|
|
|
}
|
|
@@ -147,8 +146,8 @@ export class Terminal extends Widget implements ITerminal.ITerminal {
|
|
|
* Failure to reconnect to the session should be caught appropriately
|
|
|
*/
|
|
|
async refresh(): Promise<void> {
|
|
|
- if (this._session) {
|
|
|
- await this._session.reconnect();
|
|
|
+ if (!this.isDisposed) {
|
|
|
+ await this.session.reconnect();
|
|
|
this._term.clear();
|
|
|
}
|
|
|
}
|
|
@@ -236,12 +235,13 @@ export class Terminal extends Widget implements ITerminal.ITerminal {
|
|
|
*/
|
|
|
private _initializeTerm(): void {
|
|
|
this._term.on('data', (data: string) => {
|
|
|
- if (this._session) {
|
|
|
- this._session.send({
|
|
|
- type: 'stdin',
|
|
|
- content: [data]
|
|
|
- });
|
|
|
+ if (this.isDisposed) {
|
|
|
+ return;
|
|
|
}
|
|
|
+ this.session.send({
|
|
|
+ type: 'stdin',
|
|
|
+ content: [data]
|
|
|
+ });
|
|
|
});
|
|
|
|
|
|
this._term.on('title', (title: string) => {
|
|
@@ -295,14 +295,13 @@ export class Terminal extends Widget implements ITerminal.ITerminal {
|
|
|
this._offsetHeight,
|
|
|
this._offsetWidth
|
|
|
];
|
|
|
- if (this._session) {
|
|
|
- this._session.send({ type: 'set_size', content });
|
|
|
+ if (!this.isDisposed) {
|
|
|
+ this.session.send({ type: 'set_size', content });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private _term: Xterm;
|
|
|
private _needsResize = true;
|
|
|
- private _session: TerminalSession.ISession | null = null;
|
|
|
private _termOpened = false;
|
|
|
private _offsetWidth = -1;
|
|
|
private _offsetHeight = -1;
|