-
-
Notifications
You must be signed in to change notification settings - Fork 521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Instructions to get emojis to work (on Windows) #113
Comments
@toburger Thanks for opening this! Will update the documentation with this when I have some spare time. |
I found that this affected the display of the line graphics as well, and not just the emojis. For example, having looked at the Progress sample, I tried implementing my own, but I was confused when the line styles and spinners did not match up with the ones in the animations. Setting the output encoding to utf-8 and running in Windows Terminal improved things. |
It's worth pointing out that if you set the Console's output encoding, you should do so as early as possible or you might get artifacts with the terminal's width where Spectre.Console can fallback to a 80-character default. Using For example, the following is fine assuming that it's done at the very beginning of a console application: Console.WindowWidth = 110;
Console.BufferWidth = 110;
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine($"Width: {AnsiConsole.Width}"); // 110 However, if instead you access any functionality in AnsiConsole beforehand (even accessing the width itself), you will get something unexpected: Console.WindowWidth = 110;
Console.BufferWidth = 110;
Console.WriteLine($"Width (before OutputEncoding): {AnsiConsole.Width}"); // 110
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine($"Width (after OutputEncoding): {AnsiConsole.Width}"); // 80 (Constants.DefaultTerminalWidth) This is because the standard .NET Console will flush and reset the internal Output writer - if public static Encoding OutputEncoding
{
// [...]
set
{
// [...]
if (Volatile.Read(ref s_out) != null && !s_isOutTextWriterRedirected)
{
s_out.Flush();
Volatile.Write(ref s_out, null);
}
// [...]
}
}
// [...]
public static TextWriter Out => EnsureInitialized(ref s_out, () => CreateOutputWriter(OpenStandardOutput()));
// [...] Another way to trigger this behaviour is to actually create a standard output redirection by setting a new Console.WindowWidth = 110;
Console.BufferWidth = 110;
Console.WriteLine($"Width (before SetOut): {AnsiConsole.Width}"); // 110
var originalOut = Console.Out;
Console.SetOut(new StringWriter());
var newWidth = AnsiConsole.Width;
Console.SetOut(originalOut);
Console.WriteLine($"Width (after SetOut): {newWidth}"); // 80 (Constants.DefaultTerminalWidth) If for some reason, you need to change the console's output encoding in the middle of your application, one alternative is to explicitly re-create the Console.WindowWidth = 110;
Console.BufferWidth = 110;
var oldAnsiConsole = AnsiConsole.Create(new AnsiConsoleSettings
{
Ansi = AnsiSupport.Detect,
ColorSystem = ColorSystemSupport.Detect,
Out = Console.Out,
});
Console.OutputEncoding = Encoding.UTF8;
var newAnsiConsole = AnsiConsole.Create(new AnsiConsoleSettings
{
Ansi = AnsiSupport.Detect,
ColorSystem = ColorSystemSupport.Detect,
Out = Console.Out,
});
Console.WriteLine($"Old AnsiConsole: {oldAnsiConsole.Width}"); // 80 (Constants.DefaultTerminalWidth)
Console.WriteLine($"New AnsiConsole: {newAnsiConsole.Width}"); // 110 |
@toburger @patriksvensson I cant get this to work on the default visual studio debug cmd window. Seems like the unicode does not work as expected, here i use the "Fira Code" font on all terminals but it only works on Windows Terminal. How do you do when you develop Spectre.Console @patriksvensson |
I also never got it working in vs debug window! Please share if you know how :) |
Two things determine whether or not Unicode characters can be shown:
If the encoding isn't Unicode, writing Unicode characters to the terminal will show as garbage regardless of the font you're using. To test this out in CMD.exe, try setting the code page to 65001 (UTF-8) using the That said, I don't think you should set the encoding explicitly as part of your application. The fallback mechanism to ASCII exists for a reason and will give the consumer of your application a much better experience. To answer what I use when I develop Spectre.Console: I use Windows Terminal if I need to test Unicode stuff and cmd.exe for non-Unicode stuff. On macOS, I use iTerm2 and Alacrity. I have no idea of how to get VS output window to behave correctly. (Sorry for potential grammar issues and typos. Writing this on my phone) |
I face this issue currently (with the new Windows Terminal app).
I'm not sure though what else we can/should do to fix this or whether this is an issue of Spectre.Console. If so I'm interested what is going wrong there. |
Documentation added in #744 |
Console.OutputEncoding isn't required unless using emojis, spinners, etc: spectreconsole/spectre.console#113
I am using Emojis for a project and everything worked perfectly fine on my machine.
After distributing the self executable to the customer he reported me that the emojis didn't get printed out and instead two question marks (??) are displayed.
This was quite surprising because in the documentation there exists no section of when emojis get printed and when not.
So this is an effort of collecting solutions on how to get emojis to work on Windows.
Set the OutputEncoding manually:
This is an easy fix to do, just add
System.Console.OutputEncoding = System.Text.Encoding.UTF8
as the first line of the main function.Altough this is a quick fix I don't think it is the best solution because basically you override the Encoding settings of the parent console.
Set the Encoding of the PowerShell (Core) console on a per session basis:
You can set the input and output encoding of the console by executing the following line
[console]::InputEncoding = [console]::OutputEncoding = [System.Text.Encoding]::UTF8
This commaand can be put in the profile file so the command gets executed automatically.
There exists also an issue to set UTF-8 as the default encoding on PowerShell Core: Make console windows fully UTF-8 by default on Windows, in line with the behavior on Unix-like platforms - character encoding, code page PowerShell/PowerShell#7233
Set the UTF-8 worldwide language support:
intl.cpl
I hope this helps troubleshooting the issues I had with the output encoding (and emojis) and maybe some explanation could be added to the documentation.
The text was updated successfully, but these errors were encountered: