Color and Text properties not getting restored properly (READ: Support 256 / RGB color in the Console API) #15108

Closed
opened 2026-01-31 04:28:51 +00:00 by claunia · 3 comments
Owner

Originally created by @rashil2000 on GitHub (Sep 6, 2021).

I'm intentionally ignoring the issue template, as this is a consequence of PowerShell/PowerShell#15484:

Steps to reproduce

This is a bug that was recently discovered through a module - GuiCompletion, in nightroman/PS-GuiCompletion#5.

The premise is that GetBUfferContents and SetBufferContents do not fetch and restore all color and text properties properly. See the attached GIF there for a look.

Environment data

This occurs on both Windows PowerShell and PowerShell Core.

Name                           Value
----                           -----
PSVersion                      7.1.3
PSEdition                      Core
GitCommitId                    7.1.3
OS                             Microsoft Windows 10.0.19043
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Name                           Value
----                           -----
PSVersion                      5.1.19041.906
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.906
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

from @stevel-msft

The problem is that GetBufferContents() doesn't understand ANSI. It only works for [ConsoleColor] really. Internally, it's calling GetConsoleScreenBufferInfo() which returns a CONSOLE_SCREEN_BUFFER_INFO which is limited to certain attributes.

The win32 API would need to change to give the full set of attributes supported by ANSI. Best bet would be to ask the https://github.com/Microsoft/Terminal folks (which also owns conhost) to support this.

Originally created by @rashil2000 on GitHub (Sep 6, 2021). I'm intentionally ignoring the issue template, as this is a consequence of PowerShell/PowerShell#15484: > ## Steps to reproduce > This is a bug that was recently discovered through a module - [GuiCompletion](https://github.com/nightroman/PS-GuiCompletion), in [nightroman/PS-GuiCompletion#5](https://github.com/nightroman/PS-GuiCompletion/issues/5). > > The premise is that `GetBUfferContents` and `SetBufferContents` do not fetch and restore all color and text properties properly. See the attached GIF there for a look. > > ## Environment data > This occurs on both Windows PowerShell and PowerShell Core. > > ``` > Name Value > ---- ----- > PSVersion 7.1.3 > PSEdition Core > GitCommitId 7.1.3 > OS Microsoft Windows 10.0.19043 > Platform Win32NT > PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…} > PSRemotingProtocolVersion 2.3 > SerializationVersion 1.1.0.1 > WSManStackVersion 3.0 > ``` > > ``` > Name Value > ---- ----- > PSVersion 5.1.19041.906 > PSEdition Desktop > PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} > BuildVersion 10.0.19041.906 > CLRVersion 4.0.30319.42000 > WSManStackVersion 3.0 > PSRemotingProtocolVersion 2.3 > SerializationVersion 1.1.0.1 > ``` _from @stevel-msft_ > The problem is that `GetBufferContents()` doesn't understand ANSI. It only works for `[ConsoleColor]` really. Internally, it's calling `GetConsoleScreenBufferInfo()` which returns a `CONSOLE_SCREEN_BUFFER_INFO` which is limited to certain [attributes](https://docs.microsoft.com/en-us/windows/console/console-screen-buffers#character-attributes). > > The win32 API would need to change to give the full set of attributes supported by ANSI. Best bet would be to ask the https://github.com/Microsoft/Terminal folks (which also owns conhost) to support this. > > >
claunia added the Issue-QuestionNeeds-TriageArea-VTResolution-AnsweredArea-Server labels 2026-01-31 04:28:52 +00:00
Author
Owner

@zadjii-msft commented on GitHub (Sep 7, 2021):

Yea, that's not wrong. I suppose we figured that if you're going to be doing VT things (like RGB colors), then you're not going to be using the legacy API. I'd recommend moving away from the console API and sticking with just VT if you want to support RGB colors and other advanced attributes. For example, draw that menu in https://github.com/nightroman/PS-GuiCompletion/issues/5 below the prompt, so you don't need to worry about restoring the contents there. When you're done with it, you can just clear it out.

@zadjii-msft commented on GitHub (Sep 7, 2021): Yea, that's not wrong. I suppose we figured that if you're going to be doing VT things (like RGB colors), then you're not going to be using the legacy API. I'd recommend moving away from the console API and sticking with just VT if you want to support RGB colors and other advanced attributes. For example, draw that menu in https://github.com/nightroman/PS-GuiCompletion/issues/5 _below_ the prompt, so you don't need to worry about restoring the contents there. When you're done with it, you can just clear it out.
Author
Owner

@DHowett commented on GitHub (Sep 7, 2021):

(If we're going to change the API, you won't really be able to use it until Windows 12¹ anyway.)

¹Informative, not normative.

@DHowett commented on GitHub (Sep 7, 2021): (If we're going to change the API, you won't really be able to use it until Windows 12¹ anyway.) ¹Informative, not normative.
Author
Owner

@zadjii-msft commented on GitHub (Aug 15, 2022):

You know what, I'm gonna close this out as rejected. We're EXTREMELY reluctant to making any sort of API changes to the Console API. We'd need pretty serious buisness justification to make changes there. Plus, doing something like GetConsoleScreenBufferInfo isn't going to work correctly on other platforms - I'd rather encourage people to write code that will work across different platforms. This kind of menu won't be possible to draw on a *nix terminal, so the design should ultimately be reconsidered, sorry.

(There's also #10810 which we're vaguely using to track "create a screen buffer which is a copy of the current one ... some custom VT sequence for 'enter the alternate buffer (and initialize the contents of the alt buffer to the contents of the main buffer)'". We'd design that in a way that would be possible x-plat, rather than GetConsoleScreenBufferInfo et. at. I'd follow that thread for better developments.)

@zadjii-msft commented on GitHub (Aug 15, 2022): You know what, I'm gonna close this out as rejected. We're EXTREMELY reluctant to making any sort of API changes to the Console API. We'd need pretty serious buisness justification to make changes there. Plus, doing something like `GetConsoleScreenBufferInfo` isn't going to work correctly on other platforms - I'd rather encourage people to write code that will work across different platforms. This kind of menu won't be possible to draw on a \*nix terminal, so the design should ultimately be reconsidered, sorry. (There's also #10810 which we're vaguely using to track "create a screen buffer which is a copy of the current one ... some custom VT sequence for 'enter the alternate buffer (and initialize the contents of the alt buffer to the contents of the main buffer)'". We'd design that in a way that would be possible x-plat, rather than `GetConsoleScreenBufferInfo` et. at. I'd follow that thread for better developments.)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#15108