From e9a1d6990e3d3e2cccf6aa91540bb43002500d7a Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Tue, 5 May 2020 08:18:32 -0700 Subject: [PATCH] Correctly set original console colors. Minor code reworking. --- BeefLibs/corlib/src/Console.bf | 72 ++++++++----- BeefLibs/corlib/src/ConsoleColor.bf | 150 ++++++++++++---------------- 2 files changed, 111 insertions(+), 111 deletions(-) diff --git a/BeefLibs/corlib/src/Console.bf b/BeefLibs/corlib/src/Console.bf index 5d275d8f..ac5ece20 100644 --- a/BeefLibs/corlib/src/Console.bf +++ b/BeefLibs/corlib/src/Console.bf @@ -6,37 +6,63 @@ namespace System { public static class Console { - public static Encoding InputEncoding = Encoding.ASCII; - public static Encoding OutputEncoding = Encoding.ASCII; + static Encoding InputEncoding = Encoding.ASCII; + static Encoding OutputEncoding = Encoding.ASCII; - private static ConsoleColor mForegroundColor = .White; - private static ConsoleColor mBackgroundColor = .Black; + static ConsoleColor sForegroundColor = .White; + static ConsoleColor sBackgroundColor = .Black; - private static readonly ConsoleColor mOriginalForegroundColor = mForegroundColor; - private static readonly ConsoleColor mOriginalBackgroundColor = mBackgroundColor; + static readonly ConsoleColor sOriginalForegroundColor = sForegroundColor; + static readonly ConsoleColor sOriginalBackgroundColor = sBackgroundColor; public static ConsoleColor ForegroundColor { - get { return mForegroundColor; } - set { mForegroundColor = value; SetColors(); } + get { return sForegroundColor; } + set { sForegroundColor = value; SetColors(); } } public static ConsoleColor BackgroundColor { - get { return mBackgroundColor; } - set { mBackgroundColor = value; SetColors(); } + get { return sBackgroundColor; } + set { sBackgroundColor = value; SetColors(); } } - private const uint32 STD_INPUT_HANDLE = (uint32) - 10; - private const uint32 STD_OUTPUT_HANDLE = (uint32) - 11; - private const uint32 STD_ERROR_HANDLE = (uint32) - 12; + const uint32 STD_INPUT_HANDLE = (uint32)-10; + const uint32 STD_OUTPUT_HANDLE = (uint32)-11; + const uint32 STD_ERROR_HANDLE = (uint32)-12; - [Import("kernel32.dll"), CLink] - private static extern bool SetConsoleTextAttribute(void* hConsoleOutput, uint16 wAttributes); + [CRepr] + struct CONSOLE_SCREEN_BUFFER_INFO + { + public uint16[2] mSize; + public uint16[2] mCursorPosition; + public uint16 mAttributes; + public uint16[4] mWindow; + public uint16[2] mMaximumWindowSize; + } + + [CLink] + static extern int SetConsoleTextAttribute(void* hConsoleOutput, uint16 wAttributes); + + [CLink] + static extern int GetConsoleScreenBufferInfo(void* hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo); + + [CLink] + static extern void* GetStdHandle(uint32 nStdHandle); + +#if BF_PLATFORM_WINDOWS + public static this() + { + let handle = GetStdHandle(STD_OUTPUT_HANDLE); + CONSOLE_SCREEN_BUFFER_INFO consoleInfo = .(); + if (GetConsoleScreenBufferInfo(handle, out consoleInfo) != 0) + { + sOriginalForegroundColor.ConsoleTextAttribute = (uint8)(consoleInfo.mAttributes & 0xF); + sOriginalBackgroundColor.ConsoleTextAttribute = (uint8)(consoleInfo.mAttributes >> 4); + } + } +#endif - [Import("kernel32.dll"), CLink] - private static extern void* GetStdHandle(uint32 nStdHandle); - static StreamWriter OpenStreamWriter(Platform.BfpFileStdKind stdKind, ref StreamWriter outStreamWriter) { if (outStreamWriter == null) @@ -162,20 +188,20 @@ namespace System public static void ResetColor() { - mForegroundColor = mOriginalForegroundColor; - mBackgroundColor = mOriginalBackgroundColor; + sForegroundColor = sOriginalForegroundColor; + sBackgroundColor = sOriginalBackgroundColor; #if !BF_PLATFORM_WINDOWS Write("\x1B[0m"); #endif } - private static void SetColors() + static void SetColors() { #if BF_PLATFORM_WINDOWS let handle = GetStdHandle(STD_OUTPUT_HANDLE); - let fgColor = ForegroundColor.ToConsoleTextAttribute(); - let bgColor = BackgroundColor.ToConsoleTextAttribute(); + let fgColor = ForegroundColor.ConsoleTextAttribute; + let bgColor = BackgroundColor.ConsoleTextAttribute; SetConsoleTextAttribute(handle, bgColor * 16 + fgColor); #else Write("\x1B[{}m", ForegroundColor.ToAnsi()); diff --git a/BeefLibs/corlib/src/ConsoleColor.bf b/BeefLibs/corlib/src/ConsoleColor.bf index 10678dfd..d9b1b7f0 100644 --- a/BeefLibs/corlib/src/ConsoleColor.bf +++ b/BeefLibs/corlib/src/ConsoleColor.bf @@ -2,101 +2,75 @@ namespace System { public enum ConsoleColor { - Black, - DarkBlue, - DarkGreen, - DarkCyan, - DarkRed, - DarkMagenta, - DarkYellow, - DarkGray, - Gray, - Blue, - Green, - Cyan, - Red, - Magenta, - Yellow, - White - } - - extension ConsoleColor - { - public uint8 ToConsoleTextAttribute() + case Black; + case DarkBlue; + case DarkGreen; + case DarkCyan; + case DarkRed; + case DarkMagenta; + case DarkYellow; + case DarkGray; + case Gray; + case Blue; + case Green; + case Cyan; + case Red; + case Magenta; + case Yellow; + case White; + + public uint8 ConsoleTextAttribute { - switch (this) + get { - case .Black: - return 0; - case .DarkBlue: - return 1; - case .DarkGreen: - return 2; - case .DarkCyan: - return 3; - case .DarkRed: - return 4; - case .DarkMagenta: - return 5; - case .DarkYellow: - return 6; - case .DarkGray: - return 7; - case .Gray: - return 8; - case .Blue: - return 9; - case .Green: - return 10; - case .Cyan: - return 11; - case .Red: - return 12; - case .Magenta: - return 13; - case .Yellow: - return 14; - case .White: - return 15; + return (.)this; + } + + set mut + { + this = (.)value; } } - public uint8 ToAnsi() + public uint8 AnsiCode { - switch (this) + get { - case .Black: - return 30; - case .DarkRed: - return 31; - case .DarkGreen: - return 32; - case .DarkYellow: - return 33; - case .DarkBlue: - return 34; - case .DarkMagenta: - return 35; - case .DarkCyan: - return 36; - case .Gray: - return 37; - case .DarkGray: - return 90; - case .Red: - return 91; - case .Green: - return 92; - case .Yellow: - return 93; - case .Blue: - return 94; - case .Magenta: - return 95; - case .Cyan: - return 96; - case .White: - return 97; + switch (this) + { + case .Black: + return 30; + case .DarkRed: + return 31; + case .DarkGreen: + return 32; + case .DarkYellow: + return 33; + case .DarkBlue: + return 34; + case .DarkMagenta: + return 35; + case .DarkCyan: + return 36; + case .Gray: + return 37; + case .DarkGray: + return 90; + case .Red: + return 91; + case .Green: + return 92; + case .Yellow: + return 93; + case .Blue: + return 94; + case .Magenta: + return 95; + case .Cyan: + return 96; + case .White: + return 97; + } } } }