Colorful.Console icon indicating copy to clipboard operation
Colorful.Console copied to clipboard

powershell console left with incorrect colors in some situations

Open mitchcapper opened this issue 8 years ago • 13 comments

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.

mitchcapper avatar Jun 26 '17 20:06 mitchcapper

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 avatar Jun 27 '17 02:06 tomakita

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.

mitchcapper avatar Jun 27 '17 02:06 mitchcapper

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

tomakita avatar Jun 27 '17 02:06 tomakita

win 10 1703 15063.413

mitchcapper avatar Jun 27 '17 02:06 mitchcapper

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 avatar Jun 27 '17 02:06 tomakita

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.

tomakita avatar Jun 27 '17 03:06 tomakita

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

mitchcapper avatar Jun 27 '17 03:06 mitchcapper

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.

seanamosw avatar Nov 09 '17 20:11 seanamosw

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~~

najlot avatar Sep 09 '18 20:09 najlot

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?

nzbart avatar Dec 05 '18 23:12 nzbart

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)

AlexMcArdle avatar Mar 05 '19 21:03 AlexMcArdle

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 https://github.com/tomakita/Colorful.Console/issues/24 . There's no support yet, but it's being worked on...very slowly.

tomakita avatar Jun 21 '19 22:06 tomakita

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: https://github.com/microsoft/terminal/issues/2985

dotnetshadow avatar Jan 07 '21 09:01 dotnetshadow