ANSI Escape code are counted as characters hwne #3876

Closed
opened 2026-01-30 23:32:18 +00:00 by claunia · 5 comments
Owner

Originally created by @floppyman on GitHub (Sep 14, 2019).

Environment

Microsoft Windows NT 10.0.18362.0

Version of Terminal
2019-09-14 17_08_16-Window

Steps to reproduce

When the following C# code it executed it will produce all the 255 color palette colors, in rows of 20 colors.
When running it it Windows Console (Developer Debug Conosle), it works just fine.
But when running it in Windows Terminal the lines are cut much earlier, and the output is not uniform.

using System;
namespace App
{
	internal class Program
	{
		internal static void Main(string[] args) {
			var rowSplitCount = 20;
			var count = 1;
			for (var i = 0; i < 256; i++) {
				if (count < rowSplitCount) {
					Console.Write($"\x1b[38;5;{i}m\x1b[48;5;0m {i.ToString().PadLeft(3, '0')} \x1b[0m");
					count++;
				} else {
					Console.WriteLine($"\x1b[38;5;{i}m\x1b[48;5;0m {i.ToString().PadLeft(3, '0')} \x1b[0m");
					count = 1;
				}
			}

			Console.WriteLine("");
		}
	}
}

Expected behavior

As seen in the attached picture on the Right side we have the normal Windows Console (developer debug console)
And on the Left side we have the Windows Terminal

The expected output is what is shown in the Windows Console.

2019-09-14 16_41_58-Window

Actual behavior

My suspicion is that the Windows Terminal is counting the ANSI characters when drawing the text, but at the same time correctly interpreting the escape sequences and changing the text.

Originally created by @floppyman on GitHub (Sep 14, 2019). # Environment Microsoft Windows NT 10.0.18362.0 Version of Terminal ![2019-09-14 17_08_16-Window](https://user-images.githubusercontent.com/932288/64909967-4e075f80-d712-11e9-9915-00602ff40eed.png) # Steps to reproduce When the following C# code it executed it will produce all the 255 color palette colors, in rows of 20 colors. When running it it Windows Console (Developer Debug Conosle), it works just fine. But when running it in Windows Terminal the lines are cut much earlier, and the output is not uniform. ``` using System; namespace App { internal class Program { internal static void Main(string[] args) { var rowSplitCount = 20; var count = 1; for (var i = 0; i < 256; i++) { if (count < rowSplitCount) { Console.Write($"\x1b[38;5;{i}m\x1b[48;5;0m {i.ToString().PadLeft(3, '0')} \x1b[0m"); count++; } else { Console.WriteLine($"\x1b[38;5;{i}m\x1b[48;5;0m {i.ToString().PadLeft(3, '0')} \x1b[0m"); count = 1; } } Console.WriteLine(""); } } } ``` # Expected behavior As seen in the attached picture on the Right side we have the normal Windows Console (developer debug console) And on the Left side we have the Windows Terminal The expected output is what is shown in the Windows Console. ![2019-09-14 16_41_58-Window](https://user-images.githubusercontent.com/932288/64909881-ab4ee100-d711-11e9-8e5e-827b93fab792.png) # Actual behavior My suspicion is that the Windows Terminal is counting the ANSI characters when drawing the text, but at the same time correctly interpreting the escape sequences and changing the text.
claunia added the Needs-TriageNeeds-Tag-Fix labels 2026-01-30 23:32:19 +00:00
Author
Owner

@floppyman commented on GitHub (Sep 14, 2019):

Sorry for the inconvenience, it seems i was to quick to post this bug, as it seems it was not entirely a bug after all.

It seems that you need to enable ENABLE_VIRTUAL_TERMINAL_PROCESSING on the console to make it behave correctly to the ANSI Escape codes.

And this seems to be automatically enabled in the Developer Debug Console, but not in the normal Power Shell or CMD.

So after enabling, it worked just fine. :-)

But of cause it could be cool if that is automatically enabled in the new Windows Terminal ;-)
And also supporting the full set of escape codes as only Bold and Underline is working.

@floppyman commented on GitHub (Sep 14, 2019): Sorry for the inconvenience, it seems i was to quick to post this bug, as it seems it was not entirely a bug after all. It seems that you need to enable `ENABLE_VIRTUAL_TERMINAL_PROCESSING` on the console to make it behave correctly to the ANSI Escape codes. And this seems to be automatically enabled in the Developer Debug Console, but not in the normal Power Shell or CMD. So after enabling, it worked just fine. :-) But of cause it could be cool if that is automatically enabled in the new Windows Terminal ;-) And also supporting the full set of escape codes as only Bold and Underline is working.
Author
Owner

@DHowett-MSFT commented on GitHub (Sep 14, 2019):

@floppyman thanks for following up! There's a bit of nuance here 😄

  • There's a registry key that enables it by default
  • ConPTY (the infrastructure ssh, terminal, etc. are built on) ignores this registry key on startup
  • Applications should enable it, but we should also fix the bug that makes us ignore the registry key (#1965)
@DHowett-MSFT commented on GitHub (Sep 14, 2019): @floppyman thanks for following up! There's a bit of nuance here :smile: * There's a registry key that enables it by default * ConPTY (the infrastructure ssh, terminal, etc. are built on) ignores this registry key on startup * Applications _should_ enable it, but we should also fix the bug that makes us ignore the registry key (#1965)
Author
Owner

@trajano commented on GitHub (Nov 22, 2020):

If you got to this issue when searching on Google. To work around this (I had to do it with AWS CLI) I used a program called ansicon which is available on scoop. I have opened a new issue in scoop to see if they can provide a capability in their shims to inject the ansicon logic for packages that would have the issue https://github.com/lukesampson/scoop/issues/4182

More information can be found here https://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences

@trajano commented on GitHub (Nov 22, 2020): If you got to this issue when searching on Google. To work around this (I had to do it with AWS CLI) I used a program called `ansicon` which is available on scoop. I have opened a new issue in scoop to see if they can provide a capability in their shims to inject the ansicon logic for packages that would have the issue https://github.com/lukesampson/scoop/issues/4182 More information can be found here https://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences
Author
Owner

@DHowett commented on GitHub (Nov 22, 2020):

@trajano from a Terminal perspective, this has been fixed for over a year. The only applications for which this is an issue are now the ones who explicitly disable control sequence parsing. Those applications may not even be shimmable, if they’re disabling VT mode themselves.

@DHowett commented on GitHub (Nov 22, 2020): @trajano from a Terminal perspective, this has been fixed for over a year. The only applications for which this is an issue are now _the ones who explicitly disable control sequence parsing_. Those applications may not even be shimmable, if they’re disabling VT mode themselves.
Author
Owner

@trajano commented on GitHub (Nov 22, 2020):

Hard to say, I know got this problem with older versions of Gradle (since some of my older services have not updated their gradle wrapper) which they have fixed the issue already and aws-cli (present version) at least there's a workaround to make it work in Terminal because I was looking for a tool that would "shim" the mode change which I recently discovered to be ANSICON.

But it could also be a combination of Git for Windows bash as well. Since running CMD may be doing the mode switch and then aws logs tail works properly.

@trajano commented on GitHub (Nov 22, 2020): Hard to say, I know got this problem with older versions of Gradle (since some of my older services have not updated their gradle wrapper) which they have fixed the issue already and aws-cli (present version) at least there's a workaround to make it work in Terminal because I was looking for a tool that would "shim" the mode change which I recently discovered to be ANSICON. But it could also be a combination of Git for Windows bash as well. Since running CMD may be doing the mode switch and then `aws logs tail` works properly.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#3876