Search code examples
c#windows.net-corecolorsnon-ascii-characters

Printing colorful non-ASCII characters to CLI


I have utilised the Colorful Console nuget:

<PackageReference Include="Colorful.Console" Version="1.2.15" />

With the intention of printing non-ASCII characters to a console app in Powershell:

using Console = Colorful.Console;
...
Console.Write("•", Color.Gray);

But this does not display anything in powershell or the VSCode Terminal.

Am I missing something or is powershell simply not able to display that character when colorised?


Solution

  • For full Unicode support on Windows, make sure your console's code page is 65001 (UTF-8) before calling your program:

    • from cmd.exe:
      • chcp 65001
    • from PowerShell:
      • $OutputEncoding = [Console]::InputEncoding = [Console]::OutputEncoding = [Text.UTF8Encoding]::new()

    Unless you happen to have used the still-in-beta Windows 10 feature that activates system-wide UTF-8 support (see this answer), your default OEM (console) code page is a fixed, single-byte encoding such as 437 on US-English systems, which is limited to 256 characters and therefore cannot represent Unicode characters such as (BULLET, U+2022).

    Seemingly, with code page 437 in effect, degrades to an invisible ASCII-range control character (ALERT, U+0007).

    Note:

    • This behavior is not specific to the Colorful.Console package; it equally applies to the built-in Console.WriteLine().

    • Changing a console's code page can affect other programs, so you may have to restore the original one.