Skip to content

Commit ea1fcc6

Browse files
authored
Merge pull request #3163 from slawekzachcial/add-osc-4
Add support for OSC 4 ; color ; color spec
2 parents 772e446 + 19c14b0 commit ea1fcc6

File tree

5 files changed

+128
-6
lines changed

5 files changed

+128
-6
lines changed

src/browser/Terminal.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import { MouseZoneManager } from 'browser/MouseZoneManager';
3939
import { AccessibilityManager } from './AccessibilityManager';
4040
import { ITheme, IMarker, IDisposable, ISelectionPosition, ILinkProvider } from 'xterm';
4141
import { DomRenderer } from 'browser/renderer/dom/DomRenderer';
42-
import { IKeyboardEvent, KeyboardResultType, CoreMouseEventType, CoreMouseButton, CoreMouseAction, ITerminalOptions } from 'common/Types';
42+
import { IKeyboardEvent, KeyboardResultType, CoreMouseEventType, CoreMouseButton, CoreMouseAction, ITerminalOptions, IAnsiColorChangeEvent } from 'common/Types';
4343
import { evaluateKeyboardEvent } from 'common/input/Keyboard';
4444
import { EventEmitter, IEvent, forwardEvent } from 'common/EventEmitter';
4545
import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine';
@@ -53,6 +53,7 @@ import { Linkifier2 } from 'browser/Linkifier2';
5353
import { CoreBrowserService } from 'browser/services/CoreBrowserService';
5454
import { CoreTerminal } from 'common/CoreTerminal';
5555
import { ITerminalOptions as IInitializedTerminalOptions } from 'common/services/Services';
56+
import { rgba } from 'browser/Color';
5657

5758
// Let it work inside Node.js for automated testing purposes.
5859
const document: Document = (typeof window !== 'undefined') ? window.document : null as any;
@@ -148,6 +149,7 @@ export class Terminal extends CoreTerminal implements ITerminal {
148149
this.register(this._inputHandler.onRequestReset(() => this.reset()));
149150
this.register(this._inputHandler.onRequestScroll((eraseAttr, isWrapped) => this.scroll(eraseAttr, isWrapped || undefined)));
150151
this.register(this._inputHandler.onRequestWindowsOptionsReport(type => this._reportWindowsOptions(type)));
152+
this.register(this._inputHandler.onAnsiColorChange((event) => this._changeAnsiColor(event)));
151153
this.register(forwardEvent(this._inputHandler.onCursorMove, this._onCursorMove));
152154
this.register(forwardEvent(this._inputHandler.onTitleChange, this._onTitleChange));
153155
this.register(forwardEvent(this._inputHandler.onA11yChar, this._onA11yCharEmitter));
@@ -157,6 +159,19 @@ export class Terminal extends CoreTerminal implements ITerminal {
157159
this.register(this._bufferService.onResize(e => this._afterResize(e.cols, e.rows)));
158160
}
159161

162+
private _changeAnsiColor(event: IAnsiColorChangeEvent): void {
163+
if (!this._colorManager) { return; }
164+
165+
event.colors.forEach(ansiColor => {
166+
const color = rgba.toColor(ansiColor.red, ansiColor.green, ansiColor.blue);
167+
168+
this._colorManager!.colors.ansi[ansiColor.colorIndex] = color;
169+
});
170+
171+
this._renderService?.setColors(this._colorManager!.colors);
172+
this.viewport?.onThemeChange(this._colorManager!.colors);
173+
}
174+
160175
public dispose(): void {
161176
if (this._isDisposed) {
162177
return;

src/browser/renderer/atlas/CharAtlasUtils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ export function generateConfig(scaledCharWidth: number, scaledCharHeight: number
1616
cursor: undefined,
1717
cursorAccent: undefined,
1818
selection: undefined,
19-
// For the static char atlas, we only use the first 16 colors, but we need all 256 for the
20-
// dynamic character atlas.
21-
ansi: colors.ansi.slice(0, 16)
19+
ansi: colors.ansi
2220
};
2321
return {
2422
devicePixelRatio: window.devicePixelRatio,

src/common/InputHandler.test.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { assert, expect } from 'chai';
77
import { InputHandler } from 'common/InputHandler';
8-
import { IBufferLine, IAttributeData } from 'common/Types';
8+
import { IBufferLine, IAttributeData, IAnsiColorChangeEvent } from 'common/Types';
99
import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine';
1010
import { CellData } from 'common/buffer/CellData';
1111
import { Attributes, UnderlineStyle } from 'common/buffer/Constants';
@@ -40,6 +40,7 @@ class TestInputHandler extends InputHandler {
4040
public get curAttrData(): IAttributeData { return (this as any)._curAttrData; }
4141
public get windowTitleStack(): string[] { return this._windowTitleStack; }
4242
public get iconNameStack(): string[] { return this._iconNameStack; }
43+
public parseAnsiColorChange(data: string): IAnsiColorChangeEvent | null { return this._parseAnsiColorChange(data); }
4344
}
4445

4546
describe('InputHandler', () => {
@@ -1692,4 +1693,55 @@ describe('InputHandler', () => {
16921693
assert.equal(coreService.decPrivateModes.origin, false);
16931694
});
16941695
});
1696+
describe('OSC', () => {
1697+
it('4: should parse correct Ansi color change data', () => {
1698+
// this is testing a private method
1699+
const event = inputHandler.parseAnsiColorChange('19;rgb:a1/b2/c3');
1700+
1701+
assert.isNotNull(event);
1702+
assert.deepEqual(event!.colors[0], { colorIndex: 19, red: 0xa1, green: 0xb2, blue: 0xc3 });
1703+
}),
1704+
it('4: should ignore incorrect Ansi color change data', () => {
1705+
// this is testing a private method
1706+
assert.isNull(inputHandler.parseAnsiColorChange('17;rgb:a/b/c'));
1707+
assert.isNull(inputHandler.parseAnsiColorChange('17;rgb:#aabbcc'));
1708+
assert.isNull(inputHandler.parseAnsiColorChange('17;rgba:aa/bb/cc'));
1709+
assert.isNull(inputHandler.parseAnsiColorChange('rgb:aa/bb/cc'));
1710+
});
1711+
it('4: should parse a list of Ansi color changes', () => {
1712+
// this is testing a private method
1713+
const event = inputHandler.parseAnsiColorChange('19;rgb:a1/b2/c3;17;rgb:00/11/22;255;rgb:01/ef/2d');
1714+
1715+
assert.isNotNull(event);
1716+
assert.equal(event!.colors.length, 3);
1717+
assert.deepEqual(event!.colors[0], { colorIndex: 19, red: 0xa1, green: 0xb2, blue: 0xc3 });
1718+
assert.deepEqual(event!.colors[1], { colorIndex: 17, red: 0x00, green: 0x11, blue: 0x22 });
1719+
assert.deepEqual(event!.colors[2], { colorIndex: 255, red: 0x01, green: 0xef, blue: 0x2d });
1720+
});
1721+
it('4: should ignore incorrect colors in a list of Ansi color changes', () => {
1722+
// this is testing a private method
1723+
const event = inputHandler.parseAnsiColorChange('19;rgb:a1/b2/c3;17;rgb:WR/ON/G;255;rgb:01/ef/2d');
1724+
1725+
assert.equal(event!.colors.length, 2);
1726+
assert.deepEqual(event!.colors[0], { colorIndex: 19, red: 0xa1, green: 0xb2, blue: 0xc3 });
1727+
assert.deepEqual(event!.colors[1], { colorIndex: 255, red: 0x01, green: 0xef, blue: 0x2d });
1728+
});
1729+
it('4: should be case insensitive when parsing Ansi color changes', () => {
1730+
// this is testing a private method
1731+
const event = inputHandler.parseAnsiColorChange('19;rGb:A1/b2/C3');
1732+
1733+
assert.equal(event!.colors.length, 1);
1734+
assert.deepEqual(event!.colors[0], { colorIndex: 19, red: 0xa1, green: 0xb2, blue: 0xc3 });
1735+
});
1736+
it('4: should fire event on Ansi color change', (done) => {
1737+
inputHandler.onAnsiColorChange(e => {
1738+
assert.isNotNull(e);
1739+
assert.isNotNull(e!.colors);
1740+
assert.deepEqual(e!.colors[0], { colorIndex: 17, red: 0x1a, green: 0x2b, blue: 0x3c });
1741+
assert.deepEqual(e!.colors[1], { colorIndex: 12, red: 0x11, green: 0x22, blue: 0x33 });
1742+
done();
1743+
});
1744+
inputHandler.parse('\x1b]4;17;rgb:1a/2b/3c;12;rgb:11/22/33\x1b\\');
1745+
});
1746+
});
16951747
});

src/common/InputHandler.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @license MIT
55
*/
66

7-
import { IInputHandler, IAttributeData, IDisposable, IWindowOptions } from 'common/Types';
7+
import { IInputHandler, IAttributeData, IDisposable, IWindowOptions, IAnsiColorChangeEvent } from 'common/Types';
88
import { C0, C1 } from 'common/data/EscapeSequences';
99
import { CHARSETS, DEFAULT_CHARSET } from 'common/data/Charsets';
1010
import { EscapeSequenceParser } from 'common/parser/EscapeSequenceParser';
@@ -250,6 +250,8 @@ export class InputHandler extends Disposable implements IInputHandler {
250250
public get onScroll(): IEvent<number> { return this._onScroll.event; }
251251
private _onTitleChange = new EventEmitter<string>();
252252
public get onTitleChange(): IEvent<string> { return this._onTitleChange.event; }
253+
private _onAnsiColorChange = new EventEmitter<IAnsiColorChangeEvent>();
254+
public get onAnsiColorChange(): IEvent<IAnsiColorChangeEvent> { return this._onAnsiColorChange.event; }
253255

254256
constructor(
255257
private readonly _bufferService: IBufferService,
@@ -372,6 +374,7 @@ export class InputHandler extends Disposable implements IInputHandler {
372374
this._parser.setOscHandler(2, new OscHandler((data: string) => this.setTitle(data)));
373375
// 3 - set property X in the form "prop=value"
374376
// 4 - Change Color Number
377+
this._parser.setOscHandler(4, new OscHandler((data: string) => this.setAnsiColor(data)));
375378
// 5 - Change Special Color Number
376379
// 6 - Enable/disable Special Color Number c
377380
// 7 - current directory? (not in xterm spec, see https://gitlab.com/gnachman/iterm2/issues/3939)
@@ -2711,6 +2714,45 @@ export class InputHandler extends Disposable implements IInputHandler {
27112714
this._iconName = data;
27122715
}
27132716

2717+
protected _parseAnsiColorChange(data: string): IAnsiColorChangeEvent | null {
2718+
const result: IAnsiColorChangeEvent = { colors: [] };
2719+
// example data: 5;rgb:aa/bb/cc
2720+
const regex = /(\d+);rgb:([0-9a-f]{2})\/([0-9a-f]{2})\/([0-9a-f]{2})/gi;
2721+
let match;
2722+
2723+
while ((match = regex.exec(data)) !== null) {
2724+
result.colors.push({
2725+
colorIndex: parseInt(match[1]),
2726+
red: parseInt(match[2], 16),
2727+
green: parseInt(match[3], 16),
2728+
blue: parseInt(match[4], 16)
2729+
});
2730+
}
2731+
2732+
if (result.colors.length === 0) {
2733+
return null;
2734+
}
2735+
2736+
return result;
2737+
}
2738+
2739+
/**
2740+
* OSC 4; <num> ; <text> ST (set ANSI color <num> to <text>)
2741+
*
2742+
* @vt: #Y OSC 4 "Set ANSI color" "OSC 4 ; c ; spec BEL" "Change color number `c` to the color specified by `spec`."
2743+
* `c` is the color index between 0 and 255. `spec` color format is 'rgb:hh/hh/hh' where `h` are hexadecimal digits.
2744+
* There may be multipe c ; spec elements present in the same instruction, e.g. 1;rgb:10/20/30;2;rgb:a0/b0/c0.
2745+
*/
2746+
public setAnsiColor(data: string): void {
2747+
const event = this._parseAnsiColorChange(data);
2748+
if (event) {
2749+
this._onAnsiColorChange.fire(event);
2750+
}
2751+
else {
2752+
this._logService.warn(`Expected format <num>;rgb:<rr>/<gg>/<bb> but got data: ${data}`);
2753+
}
2754+
}
2755+
27142756
/**
27152757
* ESC E
27162758
* C1.NEL

src/common/Types.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,20 @@ export interface IWindowOptions {
328328
setWinLines?: boolean;
329329
}
330330

331+
export interface IAnsiColorChangeEventColor {
332+
colorIndex: number;
333+
red: number;
334+
green: number;
335+
blue: number;
336+
}
337+
338+
/**
339+
* Event fired for OSC 4 command - to change ANSI color based on its index.
340+
*/
341+
export interface IAnsiColorChangeEvent {
342+
colors: IAnsiColorChangeEventColor[];
343+
}
344+
331345
/**
332346
* Calls the parser and handles actions generated by the parser.
333347
*/
@@ -389,6 +403,7 @@ export interface IInputHandler {
389403
/** CSI ' ~ */ deleteColumns(params: IParams): void;
390404
/** OSC 0
391405
OSC 2 */ setTitle(data: string): void;
406+
/** OSC 4 */ setAnsiColor(data: string): void;
392407
/** ESC E */ nextLine(): void;
393408
/** ESC = */ keypadApplicationMode(): void;
394409
/** ESC > */ keypadNumericMode(): void;

0 commit comments

Comments
 (0)