-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
progress-addon #5251
Merged
Merged
progress-addon #5251
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6bb2210
initial commit
jerch 80c5722
fix esbuild script
jerch f0fd368
add to publish script
jerch 569e854
add to workflow bundle
jerch 94ca05c
fix handler example in readme
jerch a1e85a4
Merge branch 'master' into progress-addon
jerch 5badbd3
import ProgressState type to d.ts
jerch d7cebc3
use event from xterm.js instead of own impl
jerch 828603c
fix integration test
jerch c207edd
make linter happy
jerch f516346
fix docs
jerch 6a23276
borrow emitter ctor from xterm
jerch 75e5cc1
Update addons/addon-progress/typings/addon-progress.d.ts
jerch 96a3be3
polish docs
jerch 8101132
rename types, remove rc import in d.ts
jerch 6f6ce33
Merge branch 'master' into progress-addon
jerch 9caff8d
Add note in readme about full API
Tyriar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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,2 @@ | ||
lib | ||
node_modules |
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,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 |
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,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. |
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,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. |
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,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" | ||
} | ||
} |
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,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 }); | ||
} | ||
} |
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,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" | ||
} | ||
] | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don' think the
!
is needed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesnt work without. My guess - the any cast above keeps the
undefined
as part of the type, so TS doesnt see this as undefined-guarded here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All good, this is temporary