Skip to content

Commit

Permalink
Create status list
Browse files Browse the repository at this point in the history
Part of #120583
  • Loading branch information
Tyriar committed Apr 5, 2021
1 parent 3028779 commit 7d6c5a0
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/vs/workbench/contrib/terminal/browser/terminalStatusList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*---------------------------------------------------------------------------------------------
* 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 Severity from 'vs/base/common/severity';

export interface ITerminalStatus {
id: string;
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>;

add(status: ITerminalStatus, duration?: number): void;
remove(status: ITerminalStatus): void;
remove(statusId: string): void;
toggle(status: ITerminalStatus, value: boolean): void;
}

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

private readonly _onDidAddStatus = new Emitter<ITerminalStatus>();
get onDidAddStatus(): Event<ITerminalStatus> { return this._onDidAddStatus.event; }
private readonly _onDidRemoveStatus = new Emitter<ITerminalStatus>();
get onDidRemoveStatus(): Event<ITerminalStatus> { return this._onDidRemoveStatus.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) {
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)) {
this._statuses.set(status.id, status);
this._onDidAddStatus.fire(status);
}
}

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)) {
this._statuses.delete(status.id);
this._onDidRemoveStatus.fire(status);
}
}

toggle(status: ITerminalStatus, value: boolean) {
if (value) {
this.add(status);
} else {
this.remove(status);
}
}
}

0 comments on commit 7d6c5a0

Please sign in to comment.