From 7d6c5a0174e8b1e7fbb01bf7c419817a84b62113 Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Mon, 5 Apr 2021 12:41:16 -0700 Subject: [PATCH] Create status list Part of #120583 --- .../terminal/browser/terminalStatusList.ts | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/vs/workbench/contrib/terminal/browser/terminalStatusList.ts diff --git a/src/vs/workbench/contrib/terminal/browser/terminalStatusList.ts b/src/vs/workbench/contrib/terminal/browser/terminalStatusList.ts new file mode 100644 index 0000000000000..a0172d97cb4a0 --- /dev/null +++ b/src/vs/workbench/contrib/terminal/browser/terminalStatusList.ts @@ -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; + readonly onDidRemoveStatus: Event; + + 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 = new Map(); + private readonly _statusTimeouts: Map = new Map(); + + private readonly _onDidAddStatus = new Emitter(); + get onDidAddStatus(): Event { return this._onDidAddStatus.event; } + private readonly _onDidRemoveStatus = new Emitter(); + get onDidRemoveStatus(): Event { 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); + } + } +}