Skip to content
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

Add support for OSC 4 ; color ; color spec #3163

Merged
merged 20 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/browser/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { MouseZoneManager } from 'browser/MouseZoneManager';
import { AccessibilityManager } from './AccessibilityManager';
import { ITheme, IMarker, IDisposable, ISelectionPosition, ILinkProvider } from 'xterm';
import { DomRenderer } from 'browser/renderer/dom/DomRenderer';
import { IKeyboardEvent, KeyboardResultType, CoreMouseEventType, CoreMouseButton, CoreMouseAction, ITerminalOptions } from 'common/Types';
import { IKeyboardEvent, KeyboardResultType, CoreMouseEventType, CoreMouseButton, CoreMouseAction, ITerminalOptions, IAnsiColorChangeEvent } from 'common/Types';
import { evaluateKeyboardEvent } from 'common/input/Keyboard';
import { EventEmitter, IEvent, forwardEvent } from 'common/EventEmitter';
import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine';
Expand All @@ -53,6 +53,7 @@ import { Linkifier2 } from 'browser/Linkifier2';
import { CoreBrowserService } from 'browser/services/CoreBrowserService';
import { CoreTerminal } from 'common/CoreTerminal';
import { ITerminalOptions as IInitializedTerminalOptions } from 'common/services/Services';
import { rgba } from 'browser/Color';

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

private _changeAnsiColor(event: IAnsiColorChangeEvent): void {
const color = rgba.toColor(event.red, event.green, event.blue);

this._colorManager!.colors.ansi[event.colorIndex] = color;
this._renderService?.setColors(this._colorManager!.colors);
this.viewport?.onThemeChange(this._colorManager!.colors);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An additional method on Terminal is an unlucky choice, as we try to get rid of helpers on terminal in favor of more direct service usage. Can we get this moved to the color manager directly? Not sure if it is properly service'ified yet, if not we can keep it here and move it later on...

I dont like the ! much, as it might lead to runtime errors in case the manager is indeed not there for whatever reason. Lets better hide the access behind an initial condition test, something like if (!this._colorManager) { return; }...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if (!this._colorManager) { return; } is a good idea here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jerch The function now exists early if _colorManager is undefined.


public dispose(): void {
if (this._isDisposed) {
return;
Expand Down
4 changes: 3 additions & 1 deletion src/browser/renderer/atlas/CharAtlasUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export function generateConfig(scaledCharWidth: number, scaledCharHeight: number
selection: undefined,
// For the static char atlas, we only use the first 16 colors, but we need all 256 for the
// dynamic character atlas.
ansi: colors.ansi.slice(0, 16)
// ansi: colors.ansi.slice(0, 16)
// TODO: Using entire array to support OSC 4; can this break anything?
ansi: colors.ansi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yes, it basically increases the atlas size from 16 * 256 pixmaps to 256 * 256. Not sure thought if it will cause problems in terms of browser limits. This needs some serious testing by someone with more atlas insights than me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tyriar Is it safe to expand the atlas here that way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code it seems like this should be fine, I think the slice is there as previously it was static but it ends up falling back to DEFAULT_ANSI_COLORS in ColorManager.

};
return {
devicePixelRatio: window.devicePixelRatio,
Expand Down
29 changes: 28 additions & 1 deletion src/common/InputHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { assert, expect } from 'chai';
import { InputHandler } from 'common/InputHandler';
import { IBufferLine, IAttributeData } from 'common/Types';
import { IBufferLine, IAttributeData, IAnsiColorChangeEvent } from 'common/Types';
import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine';
import { CellData } from 'common/buffer/CellData';
import { Attributes, UnderlineStyle } from 'common/buffer/Constants';
Expand Down Expand Up @@ -1692,4 +1692,31 @@ describe('InputHandler', () => {
assert.equal(coreService.decPrivateModes.origin, false);
});
});
describe('OSC', () => {
it('should parse correct Ansi color change data', () => {
// this is testing a private method
const parseAnsiColorChange = inputHandler['_parseAnsiColorChange'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha - thats a quite hacky access, but okish for tests. I normally subclass things to be tested with privates exposed in test files to still get the type checking done (which will be completely skipped with your way).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's for tests only. Was suggested by @kena0ki as I was using as any. Could you point me to an example of subclassing? I thought for a subclass to get access to the private parent members some sort of a hack like the above would also be required?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought for a subclass to get access to the private parent members some sort of a hack like the above would also be required?

Yes thats true. Either you can go with protected in the original class and just expose that particular property from the subclass, or do the the any trick in the subclass, but with proper typing of the result (here the function type decl).

Could you point me to an example of subclassing?

I did something similar with protected and subclassing in the parser tests here:

class TestEscapeSequenceParser extends EscapeSequenceParser {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 this is the recommended way as then TS refactors will just work automatically instead of causing a test runtime error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Done. I did not realize there was already TestInputHandler. I made the method protected in the base class and exposed it as public in the test class. Most of other fields in the test class used the same approach.


assert.deepEqual(
parseAnsiColorChange('19;rgb:a1/b2/c3'),
{ colorIndex: 19, red: 0xa1, green: 0xb2, blue: 0xc3 }
);
}),
it('should ignore incorrect Ansi color change data', () => {
// this is testing a private method
const parseAnsiColorChange = inputHandler['_parseAnsiColorChange'];

assert.isNull(parseAnsiColorChange('17;rgb:a/b/c'));
assert.isNull(parseAnsiColorChange('17;rgb:#aabbcc'));
assert.isNull(parseAnsiColorChange('17;rgba:aa/bb/cc'));
assert.isNull(parseAnsiColorChange('rgb:aa/bb/cc'));
});
it('should fire event on Ansi color change', (done) => {
inputHandler.onAnsiColorChange(e => {
assert.deepEqual(e, { colorIndex: 17, red: 0x1a, green: 0x2b, blue: 0x3c });
done();
});
inputHandler.parse('\x1b]4;17;rgb:1a/2b/3c\x1b\\');
});
});
});
37 changes: 36 additions & 1 deletion src/common/InputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @license MIT
*/

import { IInputHandler, IAttributeData, IDisposable, IWindowOptions } from 'common/Types';
import { IInputHandler, IAttributeData, IDisposable, IWindowOptions, IAnsiColorChangeEvent } from 'common/Types';
import { C0, C1 } from 'common/data/EscapeSequences';
import { CHARSETS, DEFAULT_CHARSET } from 'common/data/Charsets';
import { EscapeSequenceParser } from 'common/parser/EscapeSequenceParser';
Expand Down Expand Up @@ -250,6 +250,8 @@ export class InputHandler extends Disposable implements IInputHandler {
public get onScroll(): IEvent<number> { return this._onScroll.event; }
private _onTitleChange = new EventEmitter<string>();
public get onTitleChange(): IEvent<string> { return this._onTitleChange.event; }
private _onAnsiColorChange = new EventEmitter<IAnsiColorChangeEvent>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the idea for a custom event here? Shall this be interceptable later on from outside?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure 😊 I modeled it after "title change".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it some more, using the event is the way that InputHandler can notify the Terminal that the colors have changes, so that the Terminal can then update _colorManager colors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sounds good that way. I still wonder, if we would need a stricter coupling of the color manager to the input handler, esp. for those ? sequences. For those the input handler needs to query the colors.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @slawekzachcial! Thanks for the great PR!

Would it be possible to also make onAnsiColorChange a part of a public API? Just like onTitleChange is done.

public get onAnsiColorChange(): IEvent<IAnsiColorChangeEvent> { return this._onAnsiColorChange.event; }

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

private _parseAnsiColorChange(data: string): IAnsiColorChangeEvent | null {
// example data: 5;rgb:aa/bb/cc
const regex = /(\d+);rgb:([0-9a-fA-F]{2})\/([0-9a-fA-F]{2})\/([0-9a-fA-F]{2})/;
Copy link
Member

@jerch jerch Dec 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this does not enough for OSC 4. As far as I get it from the xterm spec it should be capable to handle arbitrary long index ; spec pairs, also case should not matter. Thus this should be a valid sequence:

OSC 4 ; 1 ; rgb:00/00/00 ; 2 ; rgb:ff/00/00 ; 3 ; RGB:00/FF/00 BEL

Furthermore xterm supports a ? instead of a valid color spec and would respond with a sequence to set this color register with the current registered value:

echo -e '\x1b]4;255;?\a'
--> sends OSC 4;255;rgb:eeee/eeee/eeee BEL

(also note the double width of the response for the color spec, not sure if we have to support that as well for input)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indeed OSC 4 allows to set multiple colors in the same command. This can be easily fixed I think IAnsiColorChangeEvent could carry an array of colors instead of one right now.

I'll admit I did not understand the ? comment. Would that mean that if it comes as ? it can be simply ignored as there will be no change?

Interestingly enough, in your ? example you note the 4-digit hex numbers. Looking at xparsecolor (see "Color Names" section) this can come a either 1, 2, 3 or 4-digit hex number. But I did not quite understand what does "the value scaled in N bits" mean so left it at 2 for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jerch The latest commits allow to pass multiple color values in the same OSC 4 call.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ? means, that the application wants to get the current color from terminal, thus the terminal should respond with the color currently set.

Idc much if we dont support that yet, we can add it by a later PR. In the end we should be capable to handle OSC 4 as xterm would, otherwise we have a partly broken implementation.

const match = data.match(regex);

if (match) {
return {
colorIndex: parseInt(match[1]),
red: parseInt(match[2], 16),
green: parseInt(match[3], 16),
blue: parseInt(match[4], 16)
};
}
return null;
}

/**
* OSC 4; <num> ; <text> ST (set ANSI color <num> to <text>)
*
* @vt: #Y OSC 4 "Set ANSI color" "OSC 4 ; c ; spec BEL" "Change color number `c` to the color specified by `spec`."
* `c` is the color index between 0 and 255. `spec` color format is 'rgb:hh/hh/hh' where `h` are hexadecimal digits.
*/
public setAnsiColor(data: string): void {
const event = this._parseAnsiColorChange(data);
if (event) {
this._onAnsiColorChange.fire(event);
}
else {
this._logService.warn(`Expected format <num>;rgb:<rr>/<gg>/<bb> but got data: ${data}`);
}
}

/**
* ESC E
* C1.NEL
Expand Down
11 changes: 11 additions & 0 deletions src/common/Types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,16 @@ export interface IWindowOptions {
setWinLines?: boolean;
}

/**
* Event fired for OSC 4 command - to change ANSI color based on its index.
*/
export interface IAnsiColorChangeEvent {
colorIndex: number;
red: number;
green: number;
blue: number;
}

/**
* Calls the parser and handles actions generated by the parser.
*/
Expand Down Expand Up @@ -389,6 +399,7 @@ export interface IInputHandler {
/** CSI ' ~ */ deleteColumns(params: IParams): void;
/** OSC 0
OSC 2 */ setTitle(data: string): void;
/** OSC 4 */ setAnsiColor(data: string): void;
/** ESC E */ nextLine(): void;
/** ESC = */ keypadApplicationMode(): void;
/** ESC > */ keypadNumericMode(): void;
Expand Down