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

powershell console left with incorrect colors in some situations #32

Open
mitchcapper opened this issue Jun 26, 2017 · 13 comments
Open
Assignees

Comments

@mitchcapper
Copy link

mitchcapper commented Jun 26, 2017

Having an issue where sometimes the console colors are getting screwed up. Here is a code sample that shows the point:

var styleSheetGlobalStatus = new StyleSheet(Color.White);
			styleSheetGlobalStatus.AddStyle("\\*CG[^*]+\\*", Color.Green, a => a.Substring(3, a.Length - 4));
			styleSheetGlobalStatus.AddStyle("\\*CY[^*]+\\*", Color.Yellow, a => a.Substring(3, a.Length - 4));
			styleSheetGlobalStatus.AddStyle("\\*CR[^*]+\\*", Color.Red, a => a.Substring(3, a.Length - 4));
			styleSheetGlobalStatus.AddStyle("\\*CC[^*]+\\*", Color.Cyan, a => a.Substring(3, a.Length - 4));
			Console.WriteLineStyled("*CC1* *CG2* *CY3* *CR4*", styleSheetGlobalStatus);
			Console.BackgroundColor = Color.Black;

What is interesting is that only when the background color is set to black does it screw up (and that is with the background set to black). In addition if you leave the writelinestyled off it doesn't happen.

Standard command shell is fine. Trying to use the app with a powershell script combo so just not using powershell is not an option.

The behavior I see is that the powershell window starts out with background black, foreground white. After running this the foreground color is set to black.

@tomakita
Copy link
Owner

tomakita commented Jun 27, 2017

Thanks for raising this issue!

Does the code listing that you've included above show the only writes to the console that have occurred in your application prior to the time at which you see the unexpected behavior (i.e. the foreground color being set to black)? In other words, is your application only making the 4 color changes (Green, Yellow, Red, Cyan) before the issue occurs, or is it making more color changes that aren't shown in your code listing? I've attempted to reproduce this, and haven't been able to. I have two thoughts:

  1. I doubt this is due to your application being executed from Powershell, so no worries there.
  2. My guess is that your application is attempting to use more than 16 different colors in the same console session (see the end of the Usage Notes if you were unaware of this limitation). This is likely to have unexpected consequences, as it can overwrite certain important colors in the console's color table -- colors such as the console's foreground color and background color.

I can't say much more than that without having a better idea of how you're using Colorful.Console.

@tomakita tomakita self-assigned this Jun 27, 2017
@mitchcapper
Copy link
Author

Well my code snippet above is the only thing needed in static void Main(string[] args) to cause the foreground color to screw up. I commented out my entire powershell user profile as well to make sure it wasn't another 3rd party item. I do have the powershell colors set to the colors of cmd.exe (white foreground black background) but even if you count that as 2 colors, the above code as 6 colors, that is only 8 colors total.

@tomakita
Copy link
Owner

Interesting. May I ask what operating system you're using?

@mitchcapper
Copy link
Author

win 10 1703 15063.413

@tomakita
Copy link
Owner

Thanks. I'll spend more time looking into this tomorrow. In the meantime, let me know if you observe anything else that may help us to understand what is going wrong, here.

@tomakita
Copy link
Owner

One more thought: have you tried a more minimal use-case, e.g.

var styleSheetGlobalStatus = new StyleSheet(Color.White);
styleSheetGlobalStatus.AddStyle("\\*CG[^*]+\\*", Color.Green, a => a.Substring(3, a.Length - 4));
Console.WriteLineStyled("*CC1* *CG2* *CY3* *CR4*", styleSheetGlobalStatus);
Console.BackgroundColor = Color.Black;

Maybe only start with one color change, and then increase to two, and so on, until the issue occurs.

@mitchcapper
Copy link
Author

I did, 4 is my actual use case, but 4 seems to be the magic number as well (at least for the above test case). I saw other irregularities when mixing with posh-git and several runs but those were likely due to the 16 color issue eventually kicking in. (Note here posh git is not enabled).

@seanamosw
Copy link

I ran into a similar issue with the default powershell console.
I couldn't replicate this in any other console, including powershell in cmder/conemu or any other linux/mac terminal.

Console.ForegroundColor = Color.Yellow;
var test = Console.ReadKey(true);

This set the entire background of the console to yellow until exiting that terminal.

@najlot
Copy link

najlot commented Sep 9, 2018

Hello @tomakita,

im writing a logging library and want to use your library to bring some colors to the output and got the same issue with PowerShell.

powershell

For sourcecode see https://github.com/najlot/Log

@nzbart
Copy link

nzbart commented Dec 5, 2018

Unfortunately, this is a show-stopper for my use of this library. Is it possible to use VT codes where supported by later versions of Windows 10?

@AlexMcArdle
Copy link

I have this issue as well. It only appears in the actual powershell.exe console. When opening a cmd.exe console and running powershell followed by dotnet .\Program.dll the program behaves as expected.

No matter how many times you switch between 1-4 colors, upon using a 5th color powershell.exe's background changes to the color that was just used. (The actual colors don't seem to matter)

@tomakita
Copy link
Owner

tomakita commented Jun 21, 2019

Thanks for all of these reports. I'm guessing that the way in which Colorful.Console writes colors into the console buffer breaks some aspect of the way in which PowerShell depends on the console buffer. I'm not sure if I'll be able to fix this without knowing more about how PowerShell depends on the buffer, and I'm a little too busy to do that right now, but will put this on my to-do list.

Regarding VT codes in Win 10, see #24 . There's no support yet, but it's being worked on...very slowly.

@dotnetshadow
Copy link

dotnetshadow commented Jan 7, 2021

I am experiencing the same issue, when using Windows Terminal with Powershell Core.
Just a simple console app with this code

Console.BackgroundColor = Color.MidnightBlue;
Console.ForegroundColor = Color.White;
Console.Clear();
Console.WriteLine("some text");

Powershell (showing green text when it should be white)
image

Cmd
image

Could be related to this:
microsoft/terminal#2985

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

No branches or pull requests

7 participants