From 84a1d38e7dc37f32a1ea678d004da0feb6b432bb Mon Sep 17 00:00:00 2001 From: RogueMacro <51059464+RogueMacro@users.noreply.github.com> Date: Tue, 5 May 2020 15:30:43 +0200 Subject: [PATCH 1/3] Update Console.bf --- BeefLibs/corlib/src/Console.bf | 51 ++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/BeefLibs/corlib/src/Console.bf b/BeefLibs/corlib/src/Console.bf index 99a038bd..5d275d8f 100644 --- a/BeefLibs/corlib/src/Console.bf +++ b/BeefLibs/corlib/src/Console.bf @@ -9,6 +9,34 @@ namespace System public static Encoding InputEncoding = Encoding.ASCII; public static Encoding OutputEncoding = Encoding.ASCII; + private static ConsoleColor mForegroundColor = .White; + private static ConsoleColor mBackgroundColor = .Black; + + private static readonly ConsoleColor mOriginalForegroundColor = mForegroundColor; + private static readonly ConsoleColor mOriginalBackgroundColor = mBackgroundColor; + + public static ConsoleColor ForegroundColor + { + get { return mForegroundColor; } + set { mForegroundColor = value; SetColors(); } + } + + public static ConsoleColor BackgroundColor + { + get { return mBackgroundColor; } + set { mBackgroundColor = 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; + + [Import("kernel32.dll"), CLink] + private static extern bool SetConsoleTextAttribute(void* hConsoleOutput, uint16 wAttributes); + + [Import("kernel32.dll"), CLink] + private static extern void* GetStdHandle(uint32 nStdHandle); + static StreamWriter OpenStreamWriter(Platform.BfpFileStdKind stdKind, ref StreamWriter outStreamWriter) { if (outStreamWriter == null) @@ -131,5 +159,28 @@ namespace System obj.ToString(str); WriteLine(str); } + + public static void ResetColor() + { + mForegroundColor = mOriginalForegroundColor; + mBackgroundColor = mOriginalBackgroundColor; + +#if !BF_PLATFORM_WINDOWS + Write("\x1B[0m"); +#endif + } + + private static void SetColors() + { +#if BF_PLATFORM_WINDOWS + let handle = GetStdHandle(STD_OUTPUT_HANDLE); + let fgColor = ForegroundColor.ToConsoleTextAttribute(); + let bgColor = BackgroundColor.ToConsoleTextAttribute(); + SetConsoleTextAttribute(handle, bgColor * 16 + fgColor); +#else + Write("\x1B[{}m", ForegroundColor.ToAnsi()); + Write("\x1B[{}m", BackgroundColor.ToAnsi() + 10); +#endif + } } } From 15f2d652833d8e0a34e361e6723e5bdbb18e4c4e Mon Sep 17 00:00:00 2001 From: RogueMacro <51059464+RogueMacro@users.noreply.github.com> Date: Tue, 5 May 2020 15:31:12 +0200 Subject: [PATCH 2/3] Create ConsoleColor.bf --- BeefLibs/corlib/src/ConsoleColor.bf | 105 ++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 BeefLibs/corlib/src/ConsoleColor.bf diff --git a/BeefLibs/corlib/src/ConsoleColor.bf b/BeefLibs/corlib/src/ConsoleColor.bf new file mode 100644 index 00000000..20489c45 --- /dev/null +++ b/BeefLibs/corlib/src/ConsoleColor.bf @@ -0,0 +1,105 @@ +using System; + +namespace ColorSteak +{ + public enum ConsoleColor + { + Black, + DarkBlue, + DarkGreen, + DarkCyan, + DarkRed, + DarkMagenta, + DarkYellow, + DarkGray, + Gray, + Blue, + Green, + Cyan, + Red, + Magenta, + Yellow, + White + } + + extension ConsoleColor + { + public uint8 ToConsoleTextAttribute() + { + switch (this) + { + 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; + } + } + + public uint8 ToAnsi() + { + 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; + } + } + } +} From c367c94a758aa704b343fef409e718f3c372e789 Mon Sep 17 00:00:00 2001 From: RogueMacro <51059464+RogueMacro@users.noreply.github.com> Date: Tue, 5 May 2020 15:41:33 +0200 Subject: [PATCH 3/3] Update ConsoleColor.bf --- BeefLibs/corlib/src/ConsoleColor.bf | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/BeefLibs/corlib/src/ConsoleColor.bf b/BeefLibs/corlib/src/ConsoleColor.bf index 20489c45..10678dfd 100644 --- a/BeefLibs/corlib/src/ConsoleColor.bf +++ b/BeefLibs/corlib/src/ConsoleColor.bf @@ -1,6 +1,4 @@ -using System; - -namespace ColorSteak +namespace System { public enum ConsoleColor {