-
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
Add support for OSC 4 ; color ; color spec #3163
Conversation
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 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: |
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.
Haven't tested it out but the PR looks good, comments are mainly just shuffling a few things around.
Thanks for the review! Let me rework it. |
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. |
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.
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.
I poked around the code some more (without really understanding the details ;-) ) and here is what I found:
But since this 16-color limitation was put there I have no idea what else this will break. I also noticed in the
... and still the config passed to |
I added the new event type and unit tests. I also updated |
If all of this worked, I guess the remaining would be to update documentation. |
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. |
@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? |
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.
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.
src/browser/Terminal.ts
Outdated
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); | ||
} |
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.
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; }
...
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.
Yes, if (!this._colorManager) { return; }
is a good idea 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.
@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 |
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.
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.
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.
@Tyriar Is it safe to expand the atlas here that way?
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.
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.
src/common/InputHandler.test.ts
Outdated
describe('OSC', () => { | ||
it('should parse correct Ansi color change data', () => { | ||
// this is testing a private method | ||
const parseAnsiColorChange = inputHandler['_parseAnsiColorChange']; |
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.
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).
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.
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?
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.
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 { |
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.
👍 this is the recommended way as then TS refactors will just work automatically instead of causing a test runtime error.
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.
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>(); |
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.
Whats the idea for a custom event here? Shall this be interceptable later on from outside?
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.
I'm not sure 😊 I modeled it after "title change".
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.
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.
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.
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.
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.
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.
src/common/InputHandler.ts
Outdated
@@ -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})/; |
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.
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)
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.
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.
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.
@jerch The latest commits allow to pass multiple color values in the same OSC 4 call.
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.
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.
I'll try to look at OSC 104 - I was only after OSC 4 here 😉. Thanks for the review. Let me rework few pieces. |
@jerch with respect to the issue with adding a new method to the |
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 @Tyriar Your thoughts in the regard? |
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.
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.
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. |
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.
Great work @slawekzachcial! Sorry about the delay, I've had very patchy availability for the last couple of months.
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:
After my updates, the output looks like this:
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!