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

Conversation

slawekzachcial
Copy link
Contributor

This pull request is an attempt to add support for OSC 4, as per #3036.

The code is far from complete. I don't yet understand enough the architecture of the library and so the modified classes may be wrong, or the way they have been modified is wrong (e.g. should update interface rather than coding directly into implementation, should leverage existing helpers to parse colors, etc...). Also I have not created any tests yet - I need to better understand first the right way to modify the code and will add the corresponding tests then. Finally, there are a few console.log calls I'm using to help me understand the flow - those will be removed and/or replaced by calls to _logService.

To test the output I'm using base16-shell colortest script as my original intent has been to make xterm.js work with Base16.

Before my updates, the output of the script in the Demo console looked like this:

Before Update

After my updates, the output looks like this:

After Update

It seems that all the background colors are handled properly.

However, for foreground it only works for colors 1-16 but not above (see the shades of blue on the left). I'm not able to figure out what else needs to be modified to make it work.

Hopefully somebody could provide me some guidance as to how to make it work, and then how to clean up this PR so that it is aligned with the design and the spirit of this project.

Thanks!

@slawekzachcial slawekzachcial changed the title Add support OSC 4 ; color ; color spec Add support for OSC 4 ; color ; color spec Nov 3, 2020
@slawekzachcial
Copy link
Contributor Author

For the blue shades issue I raised above it is something related to my system settings - so no need to worry about it.

I was able to run it successfully in node:current docker container.

Here is the test bash snippet I used in the Demo terminal (same colors as base16-shell colortest above:

printf "\x1b]4;0;rgb:18/18/18\x1b\\x1b]4;1;rgb:ab/46/42\x1b\\x1b]4;2;rgb:a1/b5/6c\x1b\\x1b]4;3;rgb:f7/ca/88\x1b\\x1b]4;4;rgb:7c/af/c2\x1b\\x1b]4;5;rgb:ba/8b/af\x1b\\x1b]4;6;rgb:86/c1/b9\x1b\\x1b]4;7;rgb:d8/d8/d8\x1b\\x1b]4;8;rgb:58/58/58\x1b\\x1b]4;9;rgb:ab/46/42\x1b\\x1b]4;10;rgb:a1/b5/6c\x1b\\x1b]4;11;rgb:f7/ca/88\x1b\\x1b]4;12;rgb:7c/af/c2\x1b\\x1b]4;13;rgb:ba/8b/af\x1b\\x1b]4;14;rgb:86/c1/b9\x1b\\x1b]4;15;rgb:f8/f8/f8\x1b\\x1b]4;16;rgb:dc/96/56\x1b\\x1b]4;17;rgb:a1/69/46\x1b\\x1b]4;18;rgb:28/28/28\x1b\\x1b]4;19;rgb:38/38/38\x1b\\x1b]4;20;rgb:b8/b8/b8\x1b\\x1b]4;21;rgb:e8/e8/e8\x1b\\r\n"
for i in {0..21}; do printf "\x1b[38;5;%dmcolor%02d   \x1b[48;5;%dm_______\x1b[0m\r\n" "$i" "$i" "$i"; done

And here is the output:

Colors working in node:current container

Copy link
Member

@Tyriar Tyriar left a comment

Choose a reason for hiding this comment

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

Haven't tested it out but the PR looks good, comments are mainly just shuffling a few things around.

src/browser/Terminal.ts Outdated Show resolved Hide resolved
src/browser/Terminal.ts Outdated Show resolved Hide resolved
src/browser/Terminal.ts Outdated Show resolved Hide resolved
src/common/InputHandler.ts Outdated Show resolved Hide resolved
@Tyriar Tyriar added this to the 4.10.0 milestone Nov 3, 2020
@slawekzachcial
Copy link
Contributor Author

Thanks for the review! Let me rework it.

@slawekzachcial
Copy link
Contributor Author

I implemented the changes but I do notice that there is something weird going on with foreground colors with indices equal or higher to 16. If I run the shell snippet twice - it works fine the first time but the second time foreground colors above 15 become the shades of blue, as if their original values were somehow cached. Background colors seem to work fine.

Copy link
Member

@Tyriar Tyriar left a comment

Choose a reason for hiding this comment

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

it works fine the first time but the second time foreground colors above 15 become the shades of blue, as if their original values were somehow cached. Background colors seem to work fine.

Colors 16+ are a little different as historically they're currently readonly even to themes. I'd try test the different renderers by changing the rendererType and checking the webgl addon box to narrow down which ones have problems. We may need to tweak the renderer(s) too.

@slawekzachcial
Copy link
Contributor Author

slawekzachcial commented Nov 6, 2020

I poked around the code some more (without really understanding the details ;-) ) and here is what I found:

  • renderer/atlas/DynamicCharAtlas.ts would return DEFAULT_ANSI_COLORS if requested index is bigger than what's configured there.
  • The configuration is generated in browser/renderer/atlas/CharAtlasUtils and there it is limited to 16 colors only
  • I locally replaced the line ansi: colors.ansi.slice(0, 16) with ansi: colors.ansi and now my re-re-rendering works as expected

But since this 16-color limitation was put there I have no idea what else this will break.

I also noticed in the CharAtlasUtils the following comment:

// 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)

... and still the config passed to DynamicCharAtlas has only 16 colors.

@slawekzachcial
Copy link
Contributor Author

I added the new event type and unit tests. I also updated CharAtlasUtils to use ALL Ansi colors and not just the first 16. Even though all tests pass on my workstation I'm conscious that this may break other things and certainly requires more ad-hoc testing.

@slawekzachcial slawekzachcial requested a review from Tyriar November 7, 2020 20:46
@slawekzachcial
Copy link
Contributor Author

If all of this worked, I guess the remaining would be to update documentation.

@slawekzachcial slawekzachcial marked this pull request as ready for review November 8, 2020 09:19
@slawekzachcial
Copy link
Contributor Author

After all the changes it's now possible to run base16-shell colortest multiple times and the colors for foreground and background are consistently correct.

Screenshot 2020-11-08 at 10 23 46

@slawekzachcial
Copy link
Contributor Author

@Tyriar I'm sure you must be busy with other work. Do you think there is anything else I should be looking at in this PR?

src/common/InputHandler.test.ts Outdated Show resolved Hide resolved
src/common/InputHandler.ts Outdated Show resolved Hide resolved
Copy link
Member

@jerch jerch left a comment

Choose a reason for hiding this comment

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

Sorry for taking us so long to get it merged.

I still have a few questions / remarks (also see below), mainly I wonder how to go about OSC 104 later on, which is meant to restore initial values after changes made from OSC 4. I think we would need some sort of stored default colors in the color manager, prolly pulled from the initial theme settings.

Comment on lines 162 to 168
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.

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.

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.

@@ -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.

@@ -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.

@slawekzachcial
Copy link
Contributor Author

I'll try to look at OSC 104 - I was only after OSC 4 here 😉. Thanks for the review. Let me rework few pieces.

@slawekzachcial
Copy link
Contributor Author

@jerch with respect to the issue with adding a new method to the Terminal, how otherwise could InputHandler interact with ColorManager instance?

@jerch
Copy link
Member

jerch commented Jan 2, 2021

@jerch with respect to the issue with adding a new method to the Terminal, how otherwise could InputHandler interact with ColorManager instance?

Yes thats where the nasty part begins. Imho we need a closer coupling between input handler and color manager to get full support for OSC 4 with ? as well as for OSC 104 later on. Closer coupling means - the color manager would need to get servicified and given to the input handler at the ctor. Not sure if we should aim for that in this PR or postpone it to a later code shift when getting down to ? and OSC 104.

@Tyriar Your thoughts in the regard?

Copy link
Member

@Tyriar Tyriar left a comment

Choose a reason for hiding this comment

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

Yes thats where the nasty part begins. Imho we need a closer coupling between input handler and color manager to get full support for OSC 4 with ? as well as for OSC 104 later on. Closer coupling means - the color manager would need to get servicified and given to the input handler at the ctor. Not sure if we should aim for that in this PR or postpone it to a later code shift when getting down to ? and OSC 104.

@Tyriar Your thoughts in the regard?

What is the problem we're trying to solve here? If InputHandler needs to tell ColorManager something, an event should work. If we need something else/more extensive I think passing an IColorManager to InputHandler should be fine, it's important not to introduce cyclic dependencies though.

@slawekzachcial
Copy link
Contributor Author

If I may chime in... If deeper refactoring is required to get InputHandler and ColorManager more tightly together, that's certainly a topic for a different PR as its impact will be much broader than the OSC 4 implementation. I would keep this PR focused on OSC4 - if there are other things related to this feature missing, please let me know and I'll fix it.

@slawekzachcial slawekzachcial requested a review from Tyriar January 9, 2021 15:58
Copy link
Member

@Tyriar Tyriar left a comment

Choose a reason for hiding this comment

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

Great work @slawekzachcial! Sorry about the delay, I've had very patchy availability for the last couple of months.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants