ConPTY translating [49m to [m escape sequence #543

Closed
opened 2026-01-30 21:54:50 +00:00 by claunia · 6 comments
Owner

Originally created by @brads55 on GitHub (Jan 27, 2019).

  • Your Windows build number: (Type ver at a Windows Command Prompt)

Microsoft Windows [Version 10.0.17763.292]

  • What you're doing and what's happening: (Copy & paste specific commands and their output, or include screen shots)

When using the new ConPTY feature it seems there is some behind the scenes optimisation happening surrounding colours that is assuming colour state. For example when using the default background/foreground escape sequences ([49m/[39m), they are either getting removed or changed into a reset sequence ([m), this make it difficult to write a frontend to ConPTY that wants to for example have a window background colour that isn't one of the colours specified by the escape sequences. (i.e. where [m and [49m would do different things).

And example of this is at https://github.com/brads55/conpty-testcase (based on the EchoCon sample), where the following can be seen:

testcase

I also suspect this is the cause of #293.

  • What's wrong / what should be happening instead:

It would be great if either these optimisations didn't happen or there was at least a way of disabling them in order to get the real raw escape sequences.

Originally created by @brads55 on GitHub (Jan 27, 2019). * Your Windows build number: (Type `ver` at a Windows Command Prompt) Microsoft Windows [Version 10.0.17763.292] * What you're doing and what's happening: (Copy & paste specific commands and their output, or include screen shots) When using the new ConPTY feature it seems there is some behind the scenes optimisation happening surrounding colours that is assuming colour state. For example when using the default background/foreground escape sequences ([49m/[39m), they are either getting removed or changed into a reset sequence ([m), this make it difficult to write a frontend to ConPTY that wants to for example have a window background colour that isn't one of the colours specified by the escape sequences. (i.e. where [m and [49m would do different things). And example of this is at https://github.com/brads55/conpty-testcase (based on the EchoCon sample), where the following can be seen: ![testcase](https://user-images.githubusercontent.com/8070284/51805191-5766b900-2262-11e9-95a9-ef6268d29990.png) I also suspect this is the cause of #293. * What's wrong / what should be happening instead: It would be great if either these optimisations didn't happen or there was at least a way of disabling them in order to get the real raw escape sequences.
claunia added the Area-VTResolution-DuplicateProduct-Conpty labels 2026-01-30 21:54:51 +00:00
Author
Owner

@DHowett-MSFT commented on GitHub (Jan 28, 2019):

Hey @brads55, thanks for reporting this! I think this is operating mostly as-expected.

Since CSI 49m is "default background", and the only color you've changed is the background (so far), it's functionally identical to CSI m (reset all attributes).

If you set 32;43m, followed by 49m, you do get a 49m out the other side:

Code:

    wchar_t szCommand[]{ L"bash -c \"echo '\x1b[32;41mABCD\x1b[49mEFGH'\"" };

output

^[[32m^[[41mABCD^[[49mEFGH

This optimization shouldn't have any observable side effects.

#293 is caused by our current inability to support a color outside the palette as the default color; this should be somewhat improved in 19H1, but it won't be perfect yet. I'll add a more detailed comment over there.

@DHowett-MSFT commented on GitHub (Jan 28, 2019): Hey @brads55, thanks for reporting this! I think this is operating mostly as-expected. Since CSI `49m` is "default background", and the only color you've changed _is_ the background (so far), it's functionally identical to CSI `m` (reset all attributes). If you set `32;43m`, followed by `49m`, you _do_ get a `49m` out the other side: Code: ```c++ wchar_t szCommand[]{ L"bash -c \"echo '\x1b[32;41mABCD\x1b[49mEFGH'\"" }; ``` output ``` ^[[32m^[[41mABCD^[[49mEFGH ``` This optimization _shouldn't_ have any observable side effects. #293 is caused by our current inability to support a color outside the palette as the default color; this should be somewhat improved in 19H1, but it won't be perfect yet. I'll add a more detailed comment over there.
Author
Owner

@brads55 commented on GitHub (Jan 28, 2019):

Hm, that was perhaps a bad example of what I'm seeing. There is certainly something strange going on with what I'm doing as there are seemingly missing colour codes in the output of what I have when compared to a non-conpty terminal. I'll dig into this a bit more and try and come up with a better example (or prove myself wrong!).

@brads55 commented on GitHub (Jan 28, 2019): Hm, that was perhaps a bad example of what I'm seeing. There is certainly something strange going on with what I'm doing as there are seemingly missing colour codes in the output of what I have when compared to a non-conpty terminal. I'll dig into this a bit more and try and come up with a better example (or prove myself wrong!).
Author
Owner

@zadjii-msft commented on GitHub (Jan 28, 2019):

If I had to guess, I'd think that maybe you're emitting something like 37m or 40m, and that's silently getting translated to 39m/49m (or maybe even 0m). In that case, your client app would say "I would like to write text with a foreground of dark white" (37m), but the conpty's "default foreground" color is also set to "dark white", so conpty then emits "I want default foreground text" (39m).

Part of the trick here is that conpty can't just emit the raw escape sequences that are written to it, because it's possible that another attached client is using the Console API to query the state of the console buffer, so we need to both process the VT sequences written to us, then render the effects of both the VT sequences and the API calls to whoever is attached to the conpty side of the console.

@zadjii-msft commented on GitHub (Jan 28, 2019): If I had to guess, I'd think that maybe you're emitting something like `37m` or `40m`, and that's silently getting translated to `39m`/`49m` (or maybe even `0m`). In that case, your client app would say "I would like to write text with a foreground of dark white" (37m), but the conpty's "default foreground" color is also set to "dark white", so conpty then emits "I want default foreground text" (39m). Part of the trick here is that conpty can't just emit the raw escape sequences that are written to it, because it's possible that another attached client is using the Console API to query the state of the console buffer, so we need to both process the VT sequences written to us, then _render_ the effects of both the VT sequences and the API calls to whoever is attached to the conpty side of the console.
Author
Owner

@brads55 commented on GitHub (Jan 28, 2019):

I haven't had chance to check yet but yes that does seem like exactly what is happening, since what I'm essentially trying to do is exactly what the above comment in the link DHowett-MSFT made describes, that is, set the default background colour to a colour outside of the normal palette.

I see it was noted that this is fixed in 19H1 but not enabled for ConPTY, is this likely to change or will this not be fixed for ConPTY until after 19H1? (I also suppose there is no workaround for this in the mean time?)

@brads55 commented on GitHub (Jan 28, 2019): I haven't had chance to check yet but yes that does seem like exactly what is happening, since what I'm essentially trying to do is exactly what the above comment in the link DHowett-MSFT made describes, that is, set the default background colour to a colour outside of the normal palette. I see it was noted that this is fixed in 19H1 but not enabled for ConPTY, is this likely to change or will this not be fixed for ConPTY until after 19H1? (I also suppose there is no workaround for this in the mean time?)
Author
Owner

@brads55 commented on GitHub (Jan 28, 2019):

Looking at this again, yes this is indeed what is happening, a [40m is being removed. Hence this is issue is just a duplicate of #293.

@brads55 commented on GitHub (Jan 28, 2019): Looking at this again, yes this is indeed what is happening, a [40m is being removed. Hence this is issue is just a duplicate of #293.
Author
Owner

@DHowett-MSFT commented on GitHub (Feb 6, 2019):

@brads55, thanks for confirming! I'll close this out as a dupe then.

@DHowett-MSFT commented on GitHub (Feb 6, 2019): @brads55, thanks for confirming! I'll close this out as a dupe then.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#543