Skip to content

Commit

Permalink
use event from xterm.js instead of own impl
Browse files Browse the repository at this point in the history
  • Loading branch information
jerch committed Jan 7, 2025
1 parent 5badbd3 commit d7cebc3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 46 deletions.
47 changes: 13 additions & 34 deletions addons/addon-progress/src/ProgressAddon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
*/

import type { Terminal, ITerminalAddon, IDisposable } from '@xterm/xterm';

Check warning on line 6 in addons/addon-progress/src/ProgressAddon.ts

View workflow job for this annotation

GitHub Actions / lint

'IDisposable' is defined but never used
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 {
Expand Down Expand Up @@ -32,19 +34,14 @@ function toInt(s: string): number {
}


export class ProgressAddon implements ITerminalAddon, IProgressApi {
private _seqHandler: IDisposable | undefined;
export class ProgressAddon extends Disposable implements ITerminalAddon, IProgressApi {
private _st: ProgressState = ProgressState.REMOVE;
private _pr = 0;
private _handlers: ProgressHandler[] = [];

public dispose(): void {
this._seqHandler?.dispose();
this._handlers.length = 0;
}
private readonly _onChange = this._register(new Emitter<IProgress>());
public readonly onChange = this._onChange.event;

public activate(terminal: Terminal): void {
this._seqHandler = terminal.parser.registerOscHandler(9, data => {
this._register(terminal.parser.registerOscHandler(9, data => {
if (!data.startsWith('4;')) {
return false;
}
Expand Down Expand Up @@ -77,38 +74,20 @@ export class ProgressAddon implements ITerminalAddon, IProgressApi {
break;
}
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 {
return { state: this._st, value: this._pr };
}

public set progress(progress: IProgress) {
if (0 <= progress.state && 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 {
if (progress.state < 0 || progress.state > 4) {
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 });
}
}
9 changes: 8 additions & 1 deletion addons/addon-progress/src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
"removeComments": true,
"strict": true,
"types": [
"../../../node_modules/@types/mocha"
"../../../node_modules/@types/mocha",
"../../../src/vs/typings/thenable"
],
"paths": {
"browser/*": [
"../../../src/browser/*"
],
"vs/*": [
"../../../src/vs/*"
],
"@xterm/addon-progress": [
"../typings/addon-progress.d.ts"
]
Expand All @@ -30,6 +34,9 @@
"references": [
{
"path": "../../../src/browser"
},
{
"path": "../../../src/vs"
}
]
}
8 changes: 2 additions & 6 deletions addons/addon-progress/typings/addon-progress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @license MIT
*/

import { Terminal, ITerminalAddon, IDisposable } from '@xterm/xterm';
import { Terminal, ITerminalAddon, IDisposable, IEvent } from '@xterm/xterm';
import type { ProgressState } from '../src/ProgressAddon';

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

/** register progress handler */
public register(handler: ProgressHandler): IDisposable;
public readonly onChange: IEvent<IProgress>;

/** getter / setter for current progress */
public progress: IProgress;
Expand All @@ -25,7 +24,4 @@ declare module '@xterm/addon-progress' {
state: ProgressState;
value: number;
}

/** Progress handler type */
export type ProgressHandler = (state: ProgressState, value: number) => void;
}
9 changes: 9 additions & 0 deletions addons/addon-progress/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
filename: mainFile,
path: path.resolve('./lib'),
Expand Down
11 changes: 6 additions & 5 deletions demo/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { AttachAddon } from '@xterm/addon-attach';
import { ClipboardAddon } from '@xterm/addon-clipboard';
import { FitAddon } from '@xterm/addon-fit';
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 { SerializeAddon } from '@xterm/addon-serialize';
import { WebLinksAddon } from '@xterm/addon-web-links';
Expand Down Expand Up @@ -1454,7 +1454,8 @@ function progressButtons(): void {
const STATES = { 0: 'remove', 1: 'set', 2: 'error', 3: 'indeterminate', 4: 'pause' };
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:
// 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 %
Expand All @@ -1470,11 +1471,11 @@ function progressButtons(): void {
}

const progressAddon = addons.progress.instance;
progressAddon.register(progressHandler);
progressAddon.onChange(progressHandler);

// apply initial state once to make it visible on page load
const {state, value} = progressAddon.progress;
progressHandler(state, value);
const initialProgress = progressAddon.progress;
progressHandler(initialProgress);

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

0 comments on commit d7cebc3

Please sign in to comment.