-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of #120583
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/vs/workbench/contrib/terminal/browser/terminalStatusList.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |