From ebc4e321c429262063fea4f6f7a21b27249e1536 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Wed, 24 Sep 2025 18:11:05 -0300 Subject: [PATCH] Ensure when running from VS, output supports UTF-8 The console started by VS (even if it's the terminal one) by default does not seem to be set up to support UTF-8. To be on the conservative side, we just set up the encoding in DEBUG builds so things Just Work from VS by default. This problem doesn't exist when running from a regular terminal. --- src/Demo/Hello.cs | 5 +++- src/SmallSharp/ConsoleEncodingInitializer.cs | 24 ++++++++++++++++++++ src/SmallSharp/Sdk.props | 5 ++++ src/SmallSharp/SmallSharp.csproj | 1 + src/SmallSharp/SmallSharp.targets | 1 + 5 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/SmallSharp/ConsoleEncodingInitializer.cs diff --git a/src/Demo/Hello.cs b/src/Demo/Hello.cs index 95cd9f1..61c152a 100644 --- a/src/Demo/Hello.cs +++ b/src/Demo/Hello.cs @@ -1,3 +1,6 @@ #:property ImplicitUsings=true +#:package Spectre.Console@0.51.* -Console.WriteLine("Hello, World!"); \ No newline at end of file +using Spectre.Console; + +AnsiConsole.MarkupLine(":globe_showing_americas: Hello, World!"); \ No newline at end of file diff --git a/src/SmallSharp/ConsoleEncodingInitializer.cs b/src/SmallSharp/ConsoleEncodingInitializer.cs new file mode 100644 index 0000000..446c74f --- /dev/null +++ b/src/SmallSharp/ConsoleEncodingInitializer.cs @@ -0,0 +1,24 @@ +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; + +namespace System; + +/// +/// Ensures that when running from Visual Studio on Windows, the console encoding is set to UTF-8 +/// to support full Unicode and emoji output. +/// +class ConsoleEncodingInitializer +{ +#pragma warning disable CA2255 // The 'ModuleInitializer' attribute should not be used in libraries + [ModuleInitializer] +#pragma warning restore CA2255 // The 'ModuleInitializer' attribute should not be used in libraries + public static void Init() + { + +#if DEBUG + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + Console.InputEncoding = Console.OutputEncoding = Encoding.UTF8; +#endif + } +} diff --git a/src/SmallSharp/Sdk.props b/src/SmallSharp/Sdk.props index 1b59562..4545ff2 100644 --- a/src/SmallSharp/Sdk.props +++ b/src/SmallSharp/Sdk.props @@ -25,4 +25,9 @@ + + + + \ No newline at end of file diff --git a/src/SmallSharp/SmallSharp.csproj b/src/SmallSharp/SmallSharp.csproj index d00d143..b63c071 100644 --- a/src/SmallSharp/SmallSharp.csproj +++ b/src/SmallSharp/SmallSharp.csproj @@ -21,6 +21,7 @@ + diff --git a/src/SmallSharp/SmallSharp.targets b/src/SmallSharp/SmallSharp.targets index f49a053..b1781c4 100644 --- a/src/SmallSharp/SmallSharp.targets +++ b/src/SmallSharp/SmallSharp.targets @@ -28,6 +28,7 @@ +