Skip to content

Commit 6e11187

Browse files
committed
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.
1 parent c4211c0 commit 6e11187

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

src/Demo/Hello.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#:property ImplicitUsings=true
2+
#:package Spectre.Console@0.51.*
23

3-
Console.WriteLine("Hello, World!");
4+
using Spectre.Console;
5+
6+
AnsiConsole.MarkupLine(":globe_showing_americas: Hello, World!");
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Runtime.CompilerServices;
2+
using System.Runtime.InteropServices;
3+
using System.Text;
4+
5+
namespace System;
6+
7+
/// <summary>
8+
/// Ensures that when running from Visual Studio on Windows, the console encoding is set to UTF-8
9+
/// to support full Unicode and emoji output.
10+
/// </summary>
11+
class ConsoleEncodingInitializer
12+
{
13+
#pragma warning disable CA2255 // The 'ModuleInitializer' attribute should not be used in libraries
14+
[ModuleInitializer]
15+
#pragma warning restore CA2255 // The 'ModuleInitializer' attribute should not be used in libraries
16+
public static void Init()
17+
{
18+
19+
#if DEBUG
20+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
21+
Console.InputEncoding = Console.OutputEncoding = Encoding.UTF8;
22+
#endif
23+
}
24+
}

src/SmallSharp/Sdk.props

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@
2525

2626
<Import Project="..\build\SmallSharp.props" />
2727

28+
<!-- In SDK mode, we don't get the content files like we do in package mode, so we add it explicitly here -->
29+
<ItemGroup>
30+
<Compile Include="$(MSBuildThisFileDirectory)..\contentFiles\cs\netstandard2.0\ConsoleEncodingInitializer.cs"
31+
NuGetPackageId="SmallSharp" />
32+
</ItemGroup>
2833
</Project>

src/SmallSharp/SmallSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
<ItemGroup>
2323
<None Include="..\..\osmfeula.txt" Link="osmfeula.txt" PackagePath="OSMFEULA.txt" />
24+
<Compile Update="ConsoleEncodingInitializer.cs" Pack="true" />
2425
</ItemGroup>
2526

2627
<ItemGroup>

src/SmallSharp/SmallSharp.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<!-- Ensure changes we make to this file trigger a new DTB -->
2929
<UpToDateCheckBuilt Include="Properties\launchSettings.json" />
3030
<UpToDateCheckBuilt Include="$(SmallSharpPackagesProps);$(SmallSharpPackagesTargets)" />
31+
<Compile Update="@(Compile -> WithMetadataValue('NuGetPackageId', 'SmallSharp'))" Visible="false" />
3132
</ItemGroup>
3233

3334
<!-- When restoring, if we include the source files, we'd get duplicate references. -->

0 commit comments

Comments
 (0)