Skip to content

Commit

Permalink
Merge pull request #5251 from jerch/progress-addon
Browse files Browse the repository at this point in the history
progress-addon
  • Loading branch information
Tyriar authored Jan 9, 2025
2 parents 0692d48 + 9caff8d commit 330e91e
Show file tree
Hide file tree
Showing 22 changed files with 720 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"addons/addon-image/src/tsconfig.json",
"addons/addon-image/test/tsconfig.json",
"addons/addon-ligatures/src/tsconfig.json",
"addons/addon-progress/src/tsconfig.json",
"addons/addon-progress/test/tsconfig.json",
"addons/addon-search/src/tsconfig.json",
"addons/addon-search/test/tsconfig.json",
"addons/addon-serialize/src/tsconfig.json",
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ jobs:
./addons/addon-ligatures/lib/* \
./addons/addon-ligatures/out/* \
./addons/addon-ligatures/out-*/* \
./addons/addon-progress/lib/* \
./addons/addon-progress/out/* \
./addons/addon-progress/out-*/* \
./addons/addon-search/lib/* \
./addons/addon-search/out/* \
./addons/addon-search/out-*/* \
Expand Down Expand Up @@ -212,6 +215,8 @@ jobs:
run: yarn test-integration-${{ matrix.browser }} --workers=50% --forbid-only --suite=addon-fit
- name: Integration tests (addon-image)
run: yarn test-integration-${{ matrix.browser }} --workers=50% --forbid-only --suite=addon-image
- name: Integration tests (addon-progress)
run: yarn test-integration-${{ matrix.browser }} --workers=50% --forbid-only --suite=addon-progress
- name: Integration tests (addon-search)
run: yarn test-integration-${{ matrix.browser }} --workers=50% --forbid-only --suite=addon-search
- name: Integration tests (addon-serialize)
Expand Down
2 changes: 2 additions & 0 deletions addons/addon-progress/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib
node_modules
32 changes: 32 additions & 0 deletions addons/addon-progress/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Blacklist - exclude everything except npm defaults such as LICENSE, etc
*
!*/

# Whitelist - lib/
!lib/**/*.d.ts

!lib/**/*.js
!lib/**/*.js.map

!lib/**/*.mjs
!lib/**/*.mjs.map

!lib/**/*.css

# Whitelist - src/
!src/**/*.ts
!src/**/*.d.ts

!src/**/*.js
!src/**/*.js.map

!src/**/*.css

# Blacklist - src/ test files
src/**/*.test.ts
src/**/*.test.d.ts
src/**/*.test.js
src/**/*.test.js.map

# Whitelist - typings/
!typings/*.d.ts
19 changes: 19 additions & 0 deletions addons/addon-progress/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024, The xterm.js authors (https://github.com/xtermjs/xterm.js)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
69 changes: 69 additions & 0 deletions addons/addon-progress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
## @xterm/addon-progress

An xterm.js addon providing an interface for ConEmu's progress sequence.
See https://conemu.github.io/en/AnsiEscapeCodes.html#ConEmu_specific_OSC for sequence details.


### Install

```bash
npm install --save @xterm/addon-progress
```


### Usage

```ts
import { Terminal } from '@xterm/xterm';
import { ProgressAddon, IProgressState } from '@xterm/addon-progress';

const terminal = new Terminal();
const progressAddon = new ProgressAddon();
terminal.loadAddon(progressAddon);
progressAddon.onChange({state, value}: IProgressState) => {
// state: 0-4 integer (see below for meaning)
// value: 0-100 integer (percent value)

// do your visualisation based on state/value here
...
});
```

See the full [API](https://github.com/xtermjs/xterm.js/blob/master/addons/addon-progress/typings/addon-progress.d.ts) for more advanced usage.

### Sequence

The sequence to set progress information has the following format:

```plain
ESC ] 9 ; 4 ; <state> ; <progress value> BEL
```

where state is a decimal number in 0 to 4 and progress value is a decimal number in 0 to 100.
The states have the following meaning:

- 0: Remove any progress indication. Also resets progress value to 0. A given progress value will be ignored.
- 1: Normal state to set a progress value. The value should be in 0..100, greater values are clamped to 100.
If the value is omitted, it will be set to 0.
- 2: Error state with an optional progress value. An omitted value will be set to 0,
which has a special meaning using the last active value.
- 3: Actual progress is "indeterminate", any progress value will be ignored. Meant to be used to indicate
a running task without progress information (e.g. by a spinner). A previously set progress value
by any other state sequence will be left untouched.
- 4: Pause or warning state with an optional progress value. An omitted value will be set to 0,
which has a special meaning using the last active value.

The addon resolves most of those semantic nuances and will provide these ready-to-go values:
- For the remove state (0) any progress value wont be parsed, thus is even allowed to contain garbage.
It will always emit `{state: 0, value: 0}`.
- For the set state (1) an omitted value will be set to 0 emitting `{state: 1, value: 0}`.
If a value was given, it must be decimal digits only, any characters outside will mark the whole sequence
as faulty (no sloppy integer parsing). The value will be clamped to max 100 giving
`{state: 1, value: parsedAndClampedValue}`.
- For the error and pause state (2 & 4) an omitted or zero value will emit `{state: 2|4, value: lastValue}`.
If a value was given, it must be decimal digits only, any characters outside will mark the whole sequence
as faulty (no sloppy integer parsing). The value will be clamped to max 100 giving
`{state: 2|4, value: parsedAndClampedValue}`.
- For the indeterminate state (3) a value notion will be ignored.
It still emits the value as `{state: 3, value: lastValue}`. Keep in mind not use that value while
that state is active, as a task might have entered that state without a proper reset at the beginning.
28 changes: 28 additions & 0 deletions addons/addon-progress/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@xterm/addon-progress",
"version": "0.1.0",
"author": {
"name": "The xterm.js authors",
"url": "https://xtermjs.org/"
},
"main": "lib/addon-progress.js",
"module": "lib/addon-progress.mjs",
"types": "typings/addon-progress.d.ts",
"repository": "https://github.com/xtermjs/xterm.js/tree/master/addons/addon-progress",
"license": "MIT",
"keywords": [
"terminal",
"xterm",
"xterm.js"
],
"scripts": {
"build": "../../node_modules/.bin/tsc -p .",
"prepackage": "npm run build",
"package": "../../node_modules/.bin/webpack",
"prepublishOnly": "npm run package",
"start": "node ../../demo/start"
},
"peerDependencies": {
"@xterm/xterm": "^5.0.0"
}
}
101 changes: 101 additions & 0 deletions addons/addon-progress/src/ProgressAddon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Copyright (c) 2024 The xterm.js authors. All rights reserved.
* @license MIT
*/

import type { Terminal, ITerminalAddon, IDisposable } from '@xterm/xterm';
import type { ProgressAddon as IProgressApi, IProgressState } from '@xterm/addon-progress';
import type { Emitter, Event } from 'vs/base/common/event';


const enum ProgressType {
REMOVE = 0,
SET = 1,
ERROR = 2,
INDETERMINATE = 3,
PAUSE = 4
}


/**
* Strict integer parsing, only decimal digits allowed.
*/
function toInt(s: string): number {
let v = 0;
for (let i = 0; i < s.length; ++i) {
const c = s.charCodeAt(i);
if (c < 0x30 || 0x39 < c) {
return -1;
}
v = v * 10 + c - 48;
}
return v;
}


export class ProgressAddon implements ITerminalAddon, IProgressApi {
private _seqHandler: IDisposable | undefined;
private _st: ProgressType = ProgressType.REMOVE;
private _pr = 0;
private _onChange: Emitter<IProgressState> | undefined;
public onChange: Event<IProgressState> | undefined;

public dispose(): void {
this._seqHandler?.dispose();
this._onChange?.dispose();
}

public activate(terminal: Terminal): void {
this._seqHandler = terminal.parser.registerOscHandler(9, data => {
if (!data.startsWith('4;')) {
return false;
}
const parts = data.split(';');

if (parts.length > 3) {
return true; // faulty sequence, just exit
}
if (parts.length === 2) {
parts.push('');
}
const st = toInt(parts[1]);
const pr = toInt(parts[2]);

switch (st) {
case ProgressType.REMOVE:
this.progress = { state: st, value: 0 };
break;
case ProgressType.SET:
if (pr < 0) return true; // faulty sequence, just exit
this.progress = { state: st, value: pr };
break;
case ProgressType.ERROR:
case ProgressType.PAUSE:
if (pr < 0) return true; // faulty sequence, just exit
this.progress = { state: st, value: pr || this._pr };
break;
case ProgressType.INDETERMINATE:
this.progress = { state: st, value: this._pr };
break;
}
return true;
});
// FIXME: borrow emitter ctor from xterm, to be changed once #5283 is resolved
this._onChange = new (terminal as any)._core._onData.constructor();
this.onChange = this._onChange!.event;
}

public get progress(): IProgressState {
return { state: this._st, value: this._pr };
}

public set progress(progress: IProgressState) {
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 });
}
}
42 changes: 42 additions & 0 deletions addons/addon-progress/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es2021",
"lib": [
"dom",
"es2015"
],
"rootDir": ".",
"outDir": "../out",
"sourceMap": true,
"removeComments": true,
"strict": true,
"types": [
"../../../node_modules/@types/mocha",
"../../../src/vs/typings/thenable"
],
"paths": {
"browser/*": [
"../../../src/browser/*"
],
"vs/*": [
"../../../src/vs/*"
],
"@xterm/addon-progress": [
"../typings/addon-progress.d.ts"
]
}
},
"include": [
"./**/*",
"../../../typings/xterm.d.ts"
],
"references": [
{
"path": "../../../src/browser"
},
{
"path": "../../../src/vs"
}
]
}
Loading

0 comments on commit 330e91e

Please sign in to comment.