-
Notifications
You must be signed in to change notification settings - Fork 129
Windows 10 Anniversary Update Virtual Terminal 256 Color functionality #29
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
Open
AlexeiScherbakov
wants to merge
1
commit into
tomakita:master
Choose a base branch
from
AlexeiScherbakov:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
|
|
||
| namespace Colorful.Interop | ||
| { | ||
| internal static class NativeMethods | ||
| { | ||
| private const string Kernel32 = "kernel32.dll"; | ||
|
|
||
| #region kernel32.dll | ||
| [DllImport(Kernel32, EntryPoint = "GetVersion", SetLastError = true)] | ||
| internal static extern int GetVersion(); | ||
| [DllImport(Kernel32, EntryPoint = "SetConsoleMode", SetLastError = true)] | ||
| internal static extern bool SetConsoleMode(IntPtr hConsoleHandle, int mode); | ||
| [DllImport(Kernel32, EntryPoint = "GetConsoleMode", SetLastError = true)] | ||
| internal static extern bool GetConsoleMode(IntPtr handle, out int mode); | ||
| [DllImport(Kernel32, EntryPoint = "GetStdHandle", SetLastError = true)] | ||
| internal static extern IntPtr GetStdHandle(int handle); | ||
| #endregion | ||
|
|
||
| private const string WinrtString = "api-ms-win-core-winrt-string-l1-1-0.dll"; | ||
|
|
||
| #region api-ms-win-core-winrt-string-l1-1-0.dll | ||
| [DllImport(WinrtString, EntryPoint = "WindowsCreateString")] | ||
| internal static extern int WindowsCreateString([MarshalAs(UnmanagedType.LPWStr)]string sourceString, int stringLength, out IntPtr hstring); | ||
| [DllImport(WinrtString, EntryPoint = "WindowsDeleteString")] | ||
| internal static extern int WindowsDeleteString(IntPtr hstring); | ||
| #endregion | ||
|
|
||
| [DllImport("api-ms-win-core-winrt-l1-1-0.dll", EntryPoint = "RoGetActivationFactory")] | ||
| internal static extern int RoGetActivationFactory(IntPtr className, ref Guid guid, out IntPtr instance); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| using Colorful.Interop; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
|
|
||
| namespace Colorful | ||
| { | ||
| internal static class OperationSystemDetector | ||
| { | ||
| private static readonly bool _isAnniversaryUpdate; | ||
|
|
||
| static OperationSystemDetector() | ||
| { | ||
| // OS Detection for all CLR implementations (.NET Framework,Mono,.NET Core) | ||
| var windir = Environment.GetEnvironmentVariable("windir"); | ||
|
|
||
| if ((!string.IsNullOrWhiteSpace(windir)) && windir.Contains(@"\") && Directory.Exists(windir)) | ||
| { | ||
| // windows | ||
| int version=NativeMethods.GetVersion(); | ||
| int major = version & 0xFF; | ||
| int minor = (version >> 8) & 0xFF; | ||
| if ((major < 6) || (minor < 2)) | ||
| { | ||
| _isAnniversaryUpdate = false; | ||
| } | ||
| else | ||
| { | ||
| // Windows 6.2 is Windows 8 or later - first which supports Windows RT | ||
| _isAnniversaryUpdate = DetectAnniversaryUpdate(); | ||
| } | ||
|
|
||
| } | ||
| else if (File.Exists("/proc/sys/kernel/ostype")) | ||
| { | ||
| string osType = File.ReadAllText(@"/proc/sys/kernel/ostype"); | ||
| if (osType.StartsWith("Linux", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| // linux | ||
| } | ||
| } | ||
| else if (File.Exists(@"/System/Library/CoreServices/SystemVersion.plist")) | ||
| { | ||
| // Mac OS | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public static bool IsAnniversaryUpdate | ||
| { | ||
| get { return _isAnniversaryUpdate; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// ALL version info functions are DEPRECATED since Windows 10 | ||
| /// https://msdn.microsoft.com/en-ca/library/windows/desktop/dn481241(v=vs.85).aspx | ||
| /// this code modified version of code: | ||
| /// http://answers.unity3d.com/questions/1249727/detect-if-windows-10-anniversary-version-number.html | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| private static bool DetectAnniversaryUpdate() | ||
| { | ||
| const string kAppExtensionClassName = "Windows.ApplicationModel.AppExtensions.AppExtensionCatalog"; | ||
| var classNameHString = IntPtr.Zero; | ||
| bool ok = false; | ||
| try | ||
| { | ||
| if (NativeMethods.WindowsCreateString(kAppExtensionClassName, kAppExtensionClassName.Length, out classNameHString) == 0) | ||
| { | ||
| IntPtr appExtensionCatalogStatics; | ||
| var IID_IAppExtensionCatalogStatics = new Guid(1010198154, 24344, 20235, 156, 229, 202, 182, 29, 25, 111, 17); | ||
|
|
||
| if (NativeMethods.RoGetActivationFactory(classNameHString, ref IID_IAppExtensionCatalogStatics, out appExtensionCatalogStatics) == 0) | ||
| { | ||
| if (appExtensionCatalogStatics != IntPtr.Zero) | ||
| { | ||
| Marshal.Release(appExtensionCatalogStatics); | ||
| ok = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| if (IntPtr.Zero != classNameHString) | ||
| { | ||
| NativeMethods.WindowsDeleteString(classNameHString); | ||
| } | ||
| } | ||
| return ok; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| using System; | ||
| using System.Drawing; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| using Colorful.Interop; | ||
|
|
||
| namespace Colorful | ||
| { | ||
| /// <summary> | ||
| /// Virtual Terminal Helper class for Windows 10 after Anniversary Update | ||
| /// <see href="https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx"/> | ||
| /// </summary> | ||
| public static class VirtualTerminalConsole | ||
| { | ||
| private static readonly bool _isVirtualTerminalProcessingActive; | ||
| static VirtualTerminalConsole() | ||
| { | ||
| if (OperationSystemDetector.IsAnniversaryUpdate) | ||
| { | ||
| _isVirtualTerminalProcessingActive = EnableVirtualTerminalProcessing(); | ||
| } | ||
| } | ||
|
|
||
| public static bool IsActive | ||
| { | ||
| get { return _isVirtualTerminalProcessingActive; } | ||
| } | ||
|
|
||
| public static bool EnableVirtualTerminalProcessing() | ||
| { | ||
| var handle = NativeMethods.GetStdHandle(-11); | ||
| int mode; | ||
| NativeMethods.GetConsoleMode(handle, out mode); | ||
| NativeMethods.SetConsoleMode(handle, mode | 0x4); | ||
| NativeMethods.GetConsoleMode(handle, out mode); | ||
| return (mode & 0x4) == 0x4; | ||
| } | ||
|
|
||
| public static void RestoreForeground() | ||
| { | ||
| System.Console.Write(VirtualTerminalSequences.RestoreForeground); | ||
| } | ||
|
|
||
| public static void RestoreBackground() | ||
| { | ||
| System.Console.Write(VirtualTerminalSequences.RestoreBackground); | ||
| } | ||
|
|
||
| public static void SetBold() | ||
| { | ||
| System.Console.Write(VirtualTerminalSequences.Bold); | ||
| } | ||
|
|
||
| public static void AddUnderline() | ||
| { | ||
| System.Console.Write(VirtualTerminalSequences.Underline); | ||
| } | ||
|
|
||
| public static void RemoveUnderline() | ||
| { | ||
| System.Console.Write(VirtualTerminalSequences.NoUnderline); | ||
| } | ||
|
|
||
| public static void SetForegroundColor(Color color) | ||
| { | ||
| System.Console.Write(VirtualTerminalSequences.ForegroundRgb(color)); | ||
| } | ||
|
|
||
| public static void SetBackgroundColor(Color color) | ||
| { | ||
| System.Console.Write(VirtualTerminalSequences.BackgroundRgb(color)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| using System.Drawing; | ||
| using System.Text; | ||
|
|
||
| namespace Colorful | ||
| { | ||
| internal static class VirtualTerminalSequences | ||
| { | ||
| private const char Escape = '\x1b'; | ||
|
|
||
| internal static readonly string RestoreForeground= SgrCommand(39); | ||
| internal static readonly string RestoreBackground = SgrCommand(49); | ||
| internal static readonly string Bold = SgrCommand(1); | ||
| internal static readonly string Underline = SgrCommand(4); | ||
| internal static readonly string NoUnderline = SgrCommand(24); | ||
|
|
||
|
|
||
| private static readonly string SgrStart = "\x1b["; | ||
|
|
||
| private static StringBuilder SgrCommandBegin(int num) | ||
| { | ||
| StringBuilder ret = new StringBuilder(SgrStart, 20); | ||
| ret.Append(num); | ||
| return ret; | ||
| } | ||
|
|
||
| private static string SgrCommand(int num) | ||
| { | ||
| var ret = SgrCommandBegin(num); | ||
| ret.Append('m'); | ||
| return ret.ToString(); | ||
| } | ||
|
|
||
| private static void AppendRgb(StringBuilder builder, Color color) | ||
| { | ||
| builder.Append(';'); | ||
| builder.Append(color.R); | ||
| builder.Append(';'); | ||
| builder.Append(color.G); | ||
| builder.Append(';'); | ||
| builder.Append(color.B); | ||
| } | ||
|
|
||
| private static string AdvancedColor(int num,Color color) | ||
| { | ||
| StringBuilder ret = SgrCommandBegin(num); | ||
| ret.Append(';'); | ||
| ret.Append(2); | ||
| AppendRgb(ret, color); | ||
| ret.Append('m'); | ||
| return ret.ToString(); | ||
| } | ||
|
|
||
| public static string ForegroundRgb(Color color) | ||
| { | ||
| return AdvancedColor(38, color); | ||
| } | ||
|
|
||
| public static string BackgroundRgb(Color color) | ||
| { | ||
| return AdvancedColor(48,color); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wtf?
Please, use
RuntimeInformation.IsOSPlatform()