Skip to content

Commit

Permalink
only change outputencoding to utf8 if not using rasterfont and not al…
Browse files Browse the repository at this point in the history
…ready using utf8
  • Loading branch information
Steve Lee (POWERSHELL) committed Oct 2, 2018
1 parent f1758c3 commit 015abde
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
35 changes: 35 additions & 0 deletions PSReadLine/PlatformWindows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,41 @@ private static bool OnBreak(ConsoleBreakSignal signal)
return false;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct CONSOLE_FONT_INFO_EX
{
internal int cbSize;
internal int nFont;
internal short FontWidth;
internal short FontHeight;
internal FontFamily FontFamily;
internal uint FontWeight;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
internal string FontFace;
}

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool GetCurrentConsoleFontEx(IntPtr consoleOutput, bool bMaximumWindow, ref CONSOLE_FONT_INFO_EX consoleFontInfo);

[Flags()]
internal enum FontFamily : uint
{
TMPF_FIXED_PITCH = 0x01
}

internal static bool IsUsingRasterFont()
{
CONSOLE_FONT_INFO_EX fontInfo = new CONSOLE_FONT_INFO_EX();
fontInfo.cbSize = Marshal.SizeOf(fontInfo);
var handle = _outputHandle.Value.DangerousGetHandle();
bool result = GetCurrentConsoleFontEx(handle, false, ref fontInfo);
// If this bit is set the font is a variable pitch font.
// If this bit is clear the font is a fixed pitch font.
// Note very carefully that those meanings are the opposite of what the constant name implies.
return !fontInfo.FontFamily.HasFlag(FontFamily.TMPF_FIXED_PITCH);
}


private static PSConsoleReadLine _singleton;
internal static IConsole OneTimeInit(PSConsoleReadLine singleton)
{
Expand Down
22 changes: 19 additions & 3 deletions PSReadLine/ReadLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/

using ConsoleHandle = Microsoft.Win32.SafeHandles.SafeFileHandle;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -44,7 +45,7 @@ public partial class PSConsoleReadLine : IPSConsoleReadLineMockableMethods
private IConsole _console;
private ICharMap _charMap;
private Encoding _initialOutputEncoding;

private bool _changeOutputEncoding;
private EngineIntrinsics _engineIntrinsics;
private Thread _readKeyThread;
private AutoResetEvent _readKeyWaitHandle;
Expand Down Expand Up @@ -531,7 +532,10 @@ T CallPossibleExternalApplication<T>(Func<T> func)
}
finally
{
_console.OutputEncoding = Encoding.UTF8;
if (_changeOutputEncoding)
{
_console.OutputEncoding = Encoding.UTF8;
}
}
}

Expand Down Expand Up @@ -647,7 +651,19 @@ private void Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
_statusIsErrorMessage = false;

_initialOutputEncoding = _console.OutputEncoding;
_console.OutputEncoding = Encoding.UTF8;

// Don't change the OutputEncoding if already UTF8 or using raster font on Windows
_changeOutputEncoding = _initialOutputEncoding != Encoding.UTF8;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && PlatformWindows.IsUsingRasterFont())
{
_changeOutputEncoding = false;
}

if (_changeOutputEncoding)
{
_console.OutputEncoding = Encoding.UTF8;
}

_lastRenderTime = Stopwatch.StartNew();

_killCommandCount = 0;
Expand Down

0 comments on commit 015abde

Please sign in to comment.