Skip to content

Commit d7cebc3

Browse files
committed
use event from xterm.js instead of own impl
1 parent 5badbd3 commit d7cebc3

5 files changed

Lines changed: 38 additions & 46 deletions

File tree

addons/addon-progress/src/ProgressAddon.ts

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55

66
import type { Terminal, ITerminalAddon, IDisposable } from '@xterm/xterm';
7-
import type { ProgressAddon as IProgressApi, IProgress, ProgressHandler } from '@xterm/addon-progress';
7+
import type { ProgressAddon as IProgressApi, IProgress } from '@xterm/addon-progress';
8+
import { Emitter } from 'vs/base/common/event';
9+
import { Disposable } from 'vs/base/common/lifecycle';
810

911

1012
export const enum ProgressState {
@@ -32,19 +34,14 @@ function toInt(s: string): number {
3234
}
3335

3436

35-
export class ProgressAddon implements ITerminalAddon, IProgressApi {
36-
private _seqHandler: IDisposable | undefined;
37+
export class ProgressAddon extends Disposable implements ITerminalAddon, IProgressApi {
3738
private _st: ProgressState = ProgressState.REMOVE;
3839
private _pr = 0;
39-
private _handlers: ProgressHandler[] = [];
40-
41-
public dispose(): void {
42-
this._seqHandler?.dispose();
43-
this._handlers.length = 0;
44-
}
40+
private readonly _onChange = this._register(new Emitter<IProgress>());
41+
public readonly onChange = this._onChange.event;
4542

4643
public activate(terminal: Terminal): void {
47-
this._seqHandler = terminal.parser.registerOscHandler(9, data => {
44+
this._register(terminal.parser.registerOscHandler(9, data => {
4845
if (!data.startsWith('4;')) {
4946
return false;
5047
}
@@ -77,38 +74,20 @@ export class ProgressAddon implements ITerminalAddon, IProgressApi {
7774
break;
7875
}
7976
return true;
80-
});
81-
}
82-
83-
public register(handler: ProgressHandler): IDisposable {
84-
const handlers = this._handlers;
85-
handlers.push(handler);
86-
return {
87-
dispose: () => {
88-
const idx = handlers.indexOf(handler);
89-
if (idx !== -1) {
90-
handlers.splice(idx, 1);
91-
}
92-
}
93-
};
77+
}));
9478
}
9579

9680
public get progress(): IProgress {
9781
return { state: this._st, value: this._pr };
9882
}
9983

10084
public set progress(progress: IProgress) {
101-
if (0 <= progress.state && progress.state <= 4)
102-
{
103-
this._st = progress.state;
104-
this._pr = Math.min(Math.max(progress.value, 0), 100);
105-
106-
// call progress handlers
107-
for (let i = 0; i < this._handlers.length; ++i) {
108-
this._handlers[i](this._st, this._pr);
109-
}
110-
} else {
85+
if (progress.state < 0 || progress.state > 4) {
11186
console.warn(`progress state out of bounds, not applied`);
87+
return;
11288
}
89+
this._st = progress.state;
90+
this._pr = Math.min(Math.max(progress.value, 0), 100);
91+
this._onChange.fire({ state: this._st, value: this._pr });
11392
}
11493
}

addons/addon-progress/src/tsconfig.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
"removeComments": true,
1313
"strict": true,
1414
"types": [
15-
"../../../node_modules/@types/mocha"
15+
"../../../node_modules/@types/mocha",
16+
"../../../src/vs/typings/thenable"
1617
],
1718
"paths": {
1819
"browser/*": [
1920
"../../../src/browser/*"
2021
],
22+
"vs/*": [
23+
"../../../src/vs/*"
24+
],
2125
"@xterm/addon-progress": [
2226
"../typings/addon-progress.d.ts"
2327
]
@@ -30,6 +34,9 @@
3034
"references": [
3135
{
3236
"path": "../../../src/browser"
37+
},
38+
{
39+
"path": "../../../src/vs"
3340
}
3441
]
3542
}

addons/addon-progress/typings/addon-progress.d.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @license MIT
44
*/
55

6-
import { Terminal, ITerminalAddon, IDisposable } from '@xterm/xterm';
6+
import { Terminal, ITerminalAddon, IDisposable, IEvent } from '@xterm/xterm';
77
import type { ProgressState } from '../src/ProgressAddon';
88

99
declare module '@xterm/addon-progress' {
@@ -13,8 +13,7 @@ declare module '@xterm/addon-progress' {
1313
public activate(terminal: Terminal): void;
1414
public dispose(): void;
1515

16-
/** register progress handler */
17-
public register(handler: ProgressHandler): IDisposable;
16+
public readonly onChange: IEvent<IProgress>;
1817

1918
/** getter / setter for current progress */
2019
public progress: IProgress;
@@ -25,7 +24,4 @@ declare module '@xterm/addon-progress' {
2524
state: ProgressState;
2625
value: number;
2726
}
28-
29-
/** Progress handler type */
30-
export type ProgressHandler = (state: ProgressState, value: number) => void;
3127
}

addons/addon-progress/webpack.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ module.exports = {
2121
}
2222
]
2323
},
24+
resolve: {
25+
modules: ['./node_modules'],
26+
extensions: [ '.js' ],
27+
alias: {
28+
common: path.resolve('../../out/common'),
29+
browser: path.resolve('../../out/browser'),
30+
vs: path.resolve('../../out/vs')
31+
}
32+
},
2433
output: {
2534
filename: mainFile,
2635
path: path.resolve('./lib'),

demo/client.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { AttachAddon } from '@xterm/addon-attach';
2121
import { ClipboardAddon } from '@xterm/addon-clipboard';
2222
import { FitAddon } from '@xterm/addon-fit';
2323
import { LigaturesAddon } from '@xterm/addon-ligatures';
24-
import { ProgressAddon } from '@xterm/addon-progress';
24+
import { ProgressAddon, IProgress } from '@xterm/addon-progress';
2525
import { SearchAddon, ISearchOptions } from '@xterm/addon-search';
2626
import { SerializeAddon } from '@xterm/addon-serialize';
2727
import { WebLinksAddon } from '@xterm/addon-web-links';
@@ -1454,7 +1454,8 @@ function progressButtons(): void {
14541454
const STATES = { 0: 'remove', 1: 'set', 2: 'error', 3: 'indeterminate', 4: 'pause' };
14551455
const COLORS = { 0: '', 1: 'green', 2: 'red', 3: '', 4: 'yellow' };
14561456

1457-
function progressHandler(state: number, value: number) {
1457+
function progressHandler(progress: IProgress) {
1458+
let {state, value} = progress;
14581459
// Simulate windows taskbar hack by windows terminal:
14591460
// Since the taskbar has no means to indicate error/pause state other than by coloring
14601461
// the current progress, we move 0 to 10% and distribute higher values in the remaining 90 %
@@ -1470,11 +1471,11 @@ function progressButtons(): void {
14701471
}
14711472

14721473
const progressAddon = addons.progress.instance;
1473-
progressAddon.register(progressHandler);
1474+
progressAddon.onChange(progressHandler);
14741475

14751476
// apply initial state once to make it visible on page load
1476-
const {state, value} = progressAddon.progress;
1477-
progressHandler(state, value);
1477+
const initialProgress = progressAddon.progress;
1478+
progressHandler(initialProgress);
14781479

14791480
document.getElementById('progress-run').addEventListener('click', async () => {
14801481
term.write('\x1b]9;4;0\x1b\\');

0 commit comments

Comments
 (0)