use event from xterm.js instead of own impl

This commit is contained in:
Jörg Breitbart
2025-01-07 19:21:51 +01:00
parent 5badbd3a6f
commit d7cebc312e
5 changed files with 38 additions and 46 deletions
+13 -34
View File
@@ -4,7 +4,9 @@
*/ */
import type { Terminal, ITerminalAddon, IDisposable } from '@xterm/xterm'; import type { Terminal, ITerminalAddon, IDisposable } from '@xterm/xterm';
import type { ProgressAddon as IProgressApi, IProgress, ProgressHandler } from '@xterm/addon-progress'; import type { ProgressAddon as IProgressApi, IProgress } from '@xterm/addon-progress';
import { Emitter } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
export const enum ProgressState { export const enum ProgressState {
@@ -32,19 +34,14 @@ function toInt(s: string): number {
} }
export class ProgressAddon implements ITerminalAddon, IProgressApi { export class ProgressAddon extends Disposable implements ITerminalAddon, IProgressApi {
private _seqHandler: IDisposable | undefined;
private _st: ProgressState = ProgressState.REMOVE; private _st: ProgressState = ProgressState.REMOVE;
private _pr = 0; private _pr = 0;
private _handlers: ProgressHandler[] = []; private readonly _onChange = this._register(new Emitter<IProgress>());
public readonly onChange = this._onChange.event;
public dispose(): void {
this._seqHandler?.dispose();
this._handlers.length = 0;
}
public activate(terminal: Terminal): void { public activate(terminal: Terminal): void {
this._seqHandler = terminal.parser.registerOscHandler(9, data => { this._register(terminal.parser.registerOscHandler(9, data => {
if (!data.startsWith('4;')) { if (!data.startsWith('4;')) {
return false; return false;
} }
@@ -77,20 +74,7 @@ export class ProgressAddon implements ITerminalAddon, IProgressApi {
break; break;
} }
return true; return true;
}); }));
}
public register(handler: ProgressHandler): IDisposable {
const handlers = this._handlers;
handlers.push(handler);
return {
dispose: () => {
const idx = handlers.indexOf(handler);
if (idx !== -1) {
handlers.splice(idx, 1);
}
}
};
} }
public get progress(): IProgress { public get progress(): IProgress {
@@ -98,17 +82,12 @@ export class ProgressAddon implements ITerminalAddon, IProgressApi {
} }
public set progress(progress: IProgress) { public set progress(progress: IProgress) {
if (0 <= progress.state && progress.state <= 4) if (progress.state < 0 || progress.state > 4) {
{
this._st = progress.state;
this._pr = Math.min(Math.max(progress.value, 0), 100);
// call progress handlers
for (let i = 0; i < this._handlers.length; ++i) {
this._handlers[i](this._st, this._pr);
}
} else {
console.warn(`progress state out of bounds, not applied`); console.warn(`progress state out of bounds, not applied`);
return;
} }
this._st = progress.state;
this._pr = Math.min(Math.max(progress.value, 0), 100);
this._onChange.fire({ state: this._st, value: this._pr });
} }
} }
+8 -1
View File
@@ -12,12 +12,16 @@
"removeComments": true, "removeComments": true,
"strict": true, "strict": true,
"types": [ "types": [
"../../../node_modules/@types/mocha" "../../../node_modules/@types/mocha",
"../../../src/vs/typings/thenable"
], ],
"paths": { "paths": {
"browser/*": [ "browser/*": [
"../../../src/browser/*" "../../../src/browser/*"
], ],
"vs/*": [
"../../../src/vs/*"
],
"@xterm/addon-progress": [ "@xterm/addon-progress": [
"../typings/addon-progress.d.ts" "../typings/addon-progress.d.ts"
] ]
@@ -30,6 +34,9 @@
"references": [ "references": [
{ {
"path": "../../../src/browser" "path": "../../../src/browser"
},
{
"path": "../../../src/vs"
} }
] ]
} }
+2 -6
View File
@@ -3,7 +3,7 @@
* @license MIT * @license MIT
*/ */
import { Terminal, ITerminalAddon, IDisposable } from '@xterm/xterm'; import { Terminal, ITerminalAddon, IDisposable, IEvent } from '@xterm/xterm';
import type { ProgressState } from '../src/ProgressAddon'; import type { ProgressState } from '../src/ProgressAddon';
declare module '@xterm/addon-progress' { declare module '@xterm/addon-progress' {
@@ -13,8 +13,7 @@ declare module '@xterm/addon-progress' {
public activate(terminal: Terminal): void; public activate(terminal: Terminal): void;
public dispose(): void; public dispose(): void;
/** register progress handler */ public readonly onChange: IEvent<IProgress>;
public register(handler: ProgressHandler): IDisposable;
/** getter / setter for current progress */ /** getter / setter for current progress */
public progress: IProgress; public progress: IProgress;
@@ -25,7 +24,4 @@ declare module '@xterm/addon-progress' {
state: ProgressState; state: ProgressState;
value: number; value: number;
} }
/** Progress handler type */
export type ProgressHandler = (state: ProgressState, value: number) => void;
} }
+9
View File
@@ -21,6 +21,15 @@ module.exports = {
} }
] ]
}, },
resolve: {
modules: ['./node_modules'],
extensions: [ '.js' ],
alias: {
common: path.resolve('../../out/common'),
browser: path.resolve('../../out/browser'),
vs: path.resolve('../../out/vs')
}
},
output: { output: {
filename: mainFile, filename: mainFile,
path: path.resolve('./lib'), path: path.resolve('./lib'),
+6 -5
View File
@@ -21,7 +21,7 @@ import { AttachAddon } from '@xterm/addon-attach';
import { ClipboardAddon } from '@xterm/addon-clipboard'; import { ClipboardAddon } from '@xterm/addon-clipboard';
import { FitAddon } from '@xterm/addon-fit'; import { FitAddon } from '@xterm/addon-fit';
import { LigaturesAddon } from '@xterm/addon-ligatures'; import { LigaturesAddon } from '@xterm/addon-ligatures';
import { ProgressAddon } from '@xterm/addon-progress'; import { ProgressAddon, IProgress } from '@xterm/addon-progress';
import { SearchAddon, ISearchOptions } from '@xterm/addon-search'; import { SearchAddon, ISearchOptions } from '@xterm/addon-search';
import { SerializeAddon } from '@xterm/addon-serialize'; import { SerializeAddon } from '@xterm/addon-serialize';
import { WebLinksAddon } from '@xterm/addon-web-links'; import { WebLinksAddon } from '@xterm/addon-web-links';
@@ -1454,7 +1454,8 @@ function progressButtons(): void {
const STATES = { 0: 'remove', 1: 'set', 2: 'error', 3: 'indeterminate', 4: 'pause' }; const STATES = { 0: 'remove', 1: 'set', 2: 'error', 3: 'indeterminate', 4: 'pause' };
const COLORS = { 0: '', 1: 'green', 2: 'red', 3: '', 4: 'yellow' }; const COLORS = { 0: '', 1: 'green', 2: 'red', 3: '', 4: 'yellow' };
function progressHandler(state: number, value: number) { function progressHandler(progress: IProgress) {
let {state, value} = progress;
// Simulate windows taskbar hack by windows terminal: // Simulate windows taskbar hack by windows terminal:
// Since the taskbar has no means to indicate error/pause state other than by coloring // Since the taskbar has no means to indicate error/pause state other than by coloring
// the current progress, we move 0 to 10% and distribute higher values in the remaining 90 % // the current progress, we move 0 to 10% and distribute higher values in the remaining 90 %
@@ -1470,11 +1471,11 @@ function progressButtons(): void {
} }
const progressAddon = addons.progress.instance; const progressAddon = addons.progress.instance;
progressAddon.register(progressHandler); progressAddon.onChange(progressHandler);
// apply initial state once to make it visible on page load // apply initial state once to make it visible on page load
const {state, value} = progressAddon.progress; const initialProgress = progressAddon.progress;
progressHandler(state, value); progressHandler(initialProgress);
document.getElementById('progress-run').addEventListener('click', async () => { document.getElementById('progress-run').addEventListener('click', async () => {
term.write('\x1b]9;4;0\x1b\\'); term.write('\x1b]9;4;0\x1b\\');