Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add terminal status concept #120590

Merged
merged 6 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/vs/workbench/contrib/terminal/browser/terminalInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { IProductService } from 'vs/platform/product/common/productService';
import { formatMessageForTerminal } from 'vs/workbench/contrib/terminal/common/terminalStrings';
import { AutoOpenBarrier } from 'vs/base/common/async';
import { Codicon, iconRegistry } from 'vs/base/common/codicons';
import { ITerminalStatusList, TerminalStatus, TerminalStatusList } from 'vs/workbench/contrib/terminal/browser/terminalStatusList';

// How long in milliseconds should an average frame take to render for a notification to appear
// which suggests the fallback DOM-based renderer
Expand Down Expand Up @@ -132,6 +133,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {

private hasHadInput: boolean;

public readonly statusList: ITerminalStatusList = new TerminalStatusList();
public disableLayout: boolean;
public get instanceId(): number { return this._instanceId; }
public get cols(): number {
Expand Down Expand Up @@ -245,6 +247,8 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {

this._initDimensions();
this._createProcessManager();
this.statusList.onDidAddStatus(e => console.log('add', e));
this.statusList.onDidRemoveStatus(e => console.log('remove', e));

this._containerReadyBarrier = new AutoOpenBarrier(Constants.WaitForContainerThreshold);
this._xtermReadyPromise = this._createXterm();
Expand Down Expand Up @@ -1009,10 +1013,13 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {

this._processManager.onPtyDisconnect(() => {
this._safeSetOption('disableStdin', true);
this.statusList.add({ id: TerminalStatus.Disconnected, severity: Severity.Error });
this._onTitleChanged.fire(this);
});
this._processManager.onPtyReconnect(() => {
this._safeSetOption('disableStdin', false);
this.statusList.remove(TerminalStatus.Disconnected);
// TODO: Remove title change + (disconnected) from title
this._onTitleChanged.fire(this);
});
}
Expand Down Expand Up @@ -1193,6 +1200,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
}

// Dispose the environment info widget if it exists
this.statusList.remove(TerminalStatus.RelaunchNeeded);
this._environmentInfo?.disposable.dispose();
this._environmentInfo = undefined;

Expand Down Expand Up @@ -1596,9 +1604,12 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {

private _refreshEnvironmentVariableInfoWidgetState(info?: IEnvironmentVariableInfo): void {
// Check if the widget should not exist
if (!info ||
if (
!info ||
this._configHelper.config.environmentChangesIndicator === 'off' ||
this._configHelper.config.environmentChangesIndicator === 'warnonly' && !info.requiresAction) {
this._configHelper.config.environmentChangesIndicator === 'warnonly' && !info.requiresAction
) {
this.statusList.remove(TerminalStatus.RelaunchNeeded);
this._environmentInfo?.disposable.dispose();
this._environmentInfo = undefined;
return;
Expand All @@ -1620,6 +1631,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
}

// (Re-)create the widget
this.statusList.add({ id: TerminalStatus.RelaunchNeeded, severity: Severity.Error });
this._environmentInfo?.disposable.dispose();
const widget = this._instantiationService.createInstance(EnvironmentVariableInfoWidget, info);
const disposable = this._widgetManager.attachWidget(widget);
Expand Down
117 changes: 117 additions & 0 deletions src/vs/workbench/contrib/terminal/browser/terminalStatusList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Emitter, Event } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import Severity from 'vs/base/common/severity';

/**
* The set of _internal_ terminal statuses, other components building on the terminal should put
* their statuses within their component.
*/
export const enum TerminalStatus {
Bell = 'bell',
Disconnected = 'disconnected',
RelaunchNeeded = 'relaunch-needed',
}

export interface ITerminalStatus {
/** An internal string ID used to identify the status. */
id: string;
/**
* The severity of the status, this defines both the color and how likely the status is to be
* the "primary status".
*/
severity: Severity;
}

export interface ITerminalStatusList {
/** Gets the most recent, highest severity status. */
readonly primary: ITerminalStatus | undefined;
/** Gets all active statues. */
readonly statuses: ITerminalStatus[];

readonly onDidAddStatus: Event<ITerminalStatus>;
readonly onDidRemoveStatus: Event<ITerminalStatus>;
readonly onDidChangePrimaryStatus: Event<ITerminalStatus | undefined>;

/**
* Adds a status to the list.
* @param duration An optional duration in milliseconds of the status, when specified the status
* will remove itself when the duration elapses unless the status gets re-added.
*/
add(status: ITerminalStatus, duration?: number): void;
remove(status: ITerminalStatus): void;
remove(statusId: string): void;
toggle(status: ITerminalStatus, value: boolean): void;
}

export class TerminalStatusList extends Disposable implements ITerminalStatusList {
private readonly _statuses: Map<string, ITerminalStatus> = new Map();
private readonly _statusTimeouts: Map<string, number> = new Map();

private readonly _onDidAddStatus = this._register(new Emitter<ITerminalStatus>());
get onDidAddStatus(): Event<ITerminalStatus> { return this._onDidAddStatus.event; }
private readonly _onDidRemoveStatus = this._register(new Emitter<ITerminalStatus>());
get onDidRemoveStatus(): Event<ITerminalStatus> { return this._onDidRemoveStatus.event; }
private readonly _onDidChangePrimaryStatus = this._register(new Emitter<ITerminalStatus | undefined>());
get onDidChangePrimaryStatus(): Event<ITerminalStatus | undefined> { return this._onDidChangePrimaryStatus.event; }

get primary(): ITerminalStatus | undefined {
let result: ITerminalStatus | undefined;
for (const s of this._statuses.values()) {
if (!result || s.severity >= result.severity) {
result = s;
}
}
return result;
}

get statuses(): ITerminalStatus[] { return Array.from(this._statuses.values()); }

add(status: ITerminalStatus, duration?: number) {
const outTimeout = this._statusTimeouts.get(status.id);
if (outTimeout) {
window.clearTimeout(outTimeout);
this._statusTimeouts.delete(status.id);
}
if (duration && duration > 0) {
const timeout = window.setTimeout(() => this.remove(status), duration);
this._statusTimeouts.set(status.id, timeout);
}
if (!this._statuses.has(status.id)) {
const oldPrimary = this.primary;
this._statuses.set(status.id, status);
this._onDidAddStatus.fire(status);
const newPrimary = this.primary;
if (oldPrimary !== newPrimary) {
this._onDidChangePrimaryStatus.fire(newPrimary);
}
}
}

remove(status: ITerminalStatus): void;
remove(statusId: string): void;
remove(statusOrId: ITerminalStatus | string): void {
const status = typeof statusOrId === 'string' ? this._statuses.get(statusOrId) : statusOrId;
// Verify the status is the same as the one passed in
if (status && this._statuses.get(status.id)) {
const wasPrimary = this.primary === status;
this._statuses.delete(status.id);
this._onDidRemoveStatus.fire(status);
if (wasPrimary) {
this._onDidChangePrimaryStatus.fire(this.primary);
}
}
}

toggle(status: ITerminalStatus, value: boolean) {
if (value) {
this.add(status);
} else {
this.remove(status);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { deepStrictEqual, strictEqual } from 'assert';
import Severity from 'vs/base/common/severity';
import { ITerminalStatus, TerminalStatusList } from 'vs/workbench/contrib/terminal/browser/terminalStatusList';

function statusesEqual(list: TerminalStatusList, expected: [string, Severity][]) {
deepStrictEqual(list.statuses.map(e => [e.id, e.severity]), expected);
}

suite('Workbench - TerminalStatusList', () => {
let list: TerminalStatusList;

setup(() => {
list = new TerminalStatusList();
});

teardown(() => {
list.dispose();
});

test('primary', () => {
strictEqual(list.primary?.id, undefined);
list.add({ id: 'info1', severity: Severity.Info });
strictEqual(list.primary?.id, 'info1');
list.add({ id: 'warning1', severity: Severity.Warning });
strictEqual(list.primary?.id, 'warning1');
list.add({ id: 'info2', severity: Severity.Info });
strictEqual(list.primary?.id, 'warning1');
list.add({ id: 'warning2', severity: Severity.Warning });
strictEqual(list.primary?.id, 'warning2');
list.add({ id: 'info3', severity: Severity.Info });
strictEqual(list.primary?.id, 'warning2');
list.add({ id: 'error1', severity: Severity.Error });
strictEqual(list.primary?.id, 'error1');
list.add({ id: 'warning3', severity: Severity.Warning });
strictEqual(list.primary?.id, 'error1');
list.add({ id: 'error2', severity: Severity.Error });
strictEqual(list.primary?.id, 'error2');
list.remove('error1');
strictEqual(list.primary?.id, 'error2');
list.remove('error2');
strictEqual(list.primary?.id, 'warning3');
});

test('statuses', () => {
strictEqual(list.statuses.length, 0);
list.add({ id: 'info', severity: Severity.Info });
list.add({ id: 'warning', severity: Severity.Warning });
list.add({ id: 'error', severity: Severity.Error });
strictEqual(list.statuses.length, 3);
statusesEqual(list, [
['info', Severity.Info],
['warning', Severity.Warning],
['error', Severity.Error],
]);
list.remove('info');
list.remove('warning');
list.remove('error');
strictEqual(list.statuses.length, 0);
});

test('onDidAddStatus', async () => {
const result = await new Promise<ITerminalStatus>(r => {
list.onDidAddStatus(r);
list.add({ id: 'test', severity: Severity.Info });
});
deepStrictEqual(result, { id: 'test', severity: Severity.Info });
});

test('onDidRemoveStatus', async () => {
const result = await new Promise<ITerminalStatus>(r => {
list.onDidRemoveStatus(r);
list.add({ id: 'test', severity: Severity.Info });
list.remove('test');
});
deepStrictEqual(result, { id: 'test', severity: Severity.Info });
});

test('onDidChangePrimaryStatus', async () => {
const result = await new Promise<ITerminalStatus>(r => {
list.onDidRemoveStatus(r);
list.add({ id: 'test', severity: Severity.Info });
list.remove('test');
});
deepStrictEqual(result, { id: 'test', severity: Severity.Info });
});

test('add', () => {
statusesEqual(list, []);
list.add({ id: 'info', severity: Severity.Info });
statusesEqual(list, [
['info', Severity.Info]
]);
list.add({ id: 'warning', severity: Severity.Warning });
statusesEqual(list, [
['info', Severity.Info],
['warning', Severity.Warning]
]);
list.add({ id: 'error', severity: Severity.Error });
statusesEqual(list, [
['info', Severity.Info],
['warning', Severity.Warning],
['error', Severity.Error]
]);
});

test('remove', () => {
list.add({ id: 'info', severity: Severity.Info });
list.add({ id: 'warning', severity: Severity.Warning });
list.add({ id: 'error', severity: Severity.Error });
statusesEqual(list, [
['info', Severity.Info],
['warning', Severity.Warning],
['error', Severity.Error]
]);
list.remove('warning');
statusesEqual(list, [
['info', Severity.Info],
['error', Severity.Error]
]);
list.remove('info');
statusesEqual(list, [
['error', Severity.Error]
]);
list.remove('error');
statusesEqual(list, []);
});

test('toggle', () => {
const status = { id: 'info', severity: Severity.Info };
list.toggle(status, true);
statusesEqual(list, [
['info', Severity.Info]
]);
list.toggle(status, false);
statusesEqual(list, []);
});
});